summaryrefslogtreecommitdiff
path: root/client/process_loop.h
blob: 33ca2d13bbeecf0e9bd924fa58f81dc380f089e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _H_PROCESS_LOOP
#define _H_PROCESS_LOOP

#include "common.h"

#include <set>

#include "event_sources.h"
#include "threads.h"

class AbstractProcessLoop {
public:
    virtual ~AbstractProcessLoop() {}
    virtual int run() = 0;
    virtual void do_quit(int error_code) = 0;
    virtual void* get_owner() = 0;
    virtual bool is_same_thread(pthread_t thread) = 0;
};

class EventBase {
public:
    EventBase() : _refs (1) {}

    virtual void response(AbstractProcessLoop& events_loop) = 0;

    EventBase* ref() { ++_refs; return this;}
    void unref() {if (--_refs == 0) delete this;}

protected:
    virtual ~EventBase() {}

private:
    AtomicCount _refs;
};

class EventsQueue;

class Event: public EventBase {
#ifdef RED_DEBUG
public:
    Event() : _process_loop (NULL) {}

private:
    void set_process_loop(AbstractProcessLoop* process_loop) { _process_loop = process_loop;}

protected:
    AbstractProcessLoop* _process_loop;
#endif

private:
    void set_generation(uint32_t gen) { _generation = gen;}
    uint32_t get_generation() { return _generation;}

private:
    uint32_t _generation;

    friend class EventsQueue;
};

class EventsQueue {
public:
    EventsQueue(AbstractProcessLoop& owner);
    virtual ~EventsQueue();
    /* return the size of the queue (post-push) */
    int push_event(Event* event);
    void process_events();
    bool is_empty();

private:
    void clear_queue();

private:
    std::list<Event*> _events;
    Mutex _events_lock;
    uint32_t _events_gen;

    AbstractProcessLoop& _owner;
};

class SyncEvent: public Event {
public:
    SyncEvent();

    void wait();
    bool success() { return !_err;}

    virtual void do_response(AbstractProcessLoop& events_loop) {}

protected:
    virtual ~SyncEvent();

private:
    virtual void response(AbstractProcessLoop& events_loop);

private:
    Mutex _mutex;
    Condition _condition;
    bool _err;
    bool _ready;
};

class TimersQueue;

class Timer: public EventBase {
public:
    Timer();
    bool is_armed() {return _is_armed;}

protected:
    virtual ~Timer();

private:
    void arm(uint32_t msec);
    void disarm();
    uint64_t get_expiration() const { return _expiration;}
    void calc_next_expiration_time() { _expiration += _interval;}
    void calc_next_expiration_time(uint64_t now);

    static uint64_t get_now();

private:
    bool _is_armed;
    uint32_t _interval;
    uint64_t _expiration;

    class Compare {
    public:
        bool operator () (const Timer* timer1, const Timer* timer2) const
        {
            if (timer1->get_expiration() < timer2->get_expiration()) {
                return true;
            } else if (timer1->get_expiration() > timer2->get_expiration()) {
                return false;
            } else { // elements must be unique (for insertion into set)
                return timer1 < timer2;
            }
        }
    };

    friend class TimersQueue;
};

class TimersQueue {
public:
    TimersQueue(AbstractProcessLoop& owner);
    virtual ~TimersQueue();

    void activate_interval_timer(Timer* timer, unsigned int millisec);
    void deactivate_interval_timer(Timer* timer);

    unsigned int get_soonest_timeout();
    void timers_action();

private:
    void clear_queue();

private:
    typedef std::set<Timer*, Timer::Compare> TimersSet;
    TimersSet _armed_timers;
    RecurciveMutex _timers_lock;
    AbstractProcessLoop& _owner;
};

class ProcessLoop: public AbstractProcessLoop {
public:
    class QuitEvent;
    ProcessLoop(void* owner);
    virtual ~ProcessLoop();
    int run();

    void quit(int error_code);

    /* Event sources to track. Note: the following methods are not thread safe, thus,
       they mustn't be called from other thread than the process loop thread. */
    void add_trigger(EventSources::Trigger& trigger);
    void remove_trigger(EventSources::Trigger& trigger);
    void add_socket(EventSources::Socket& socket);
    void remove_socket(EventSources::Socket& socket);
    void add_file(EventSources::File& file);
    void remove_file(EventSources::File& file);
    void add_handle(EventSources::Handle& handle);
    void remove_handle(EventSources::Handle& handle);

    /* events queue */
    void push_event(Event* event);

    void activate_interval_timer(Timer* timer, unsigned int millisec);
    void deactivate_interval_timer(Timer* timer);

    void process_events_queue();
    /* can be used for handling timers in modal loop state in Windows (mainly,
       for updating the screen) */
    unsigned int get_soonest_timeout();
    void timers_action();

    void* get_owner() { return _owner;}

    bool is_same_thread(pthread_t thread) { return _started && pthread_equal(_thread, thread);}

protected:
    class WakeupTrigger: public EventSources::Trigger {
    public:
        virtual void on_event() {}
    };

    virtual void on_start_running() {}
    void wakeup();
    void do_quit(int error_code);

    friend class QuitEvent; // allowing access to quit

private:
    EventSources _event_sources;
    EventsQueue _events_queue;
    TimersQueue _timers_queue;

    WakeupTrigger _wakeup_trigger;

    void* _owner;

    bool _quitting;
    int _exit_code;
    bool _started;
    pthread_t _thread;
};

#endif