Commit 01463d7b28a6666919ab432071ab332df5ccc82d
1 parent
2c2cb317
add finalize for event subjects. This results in an delete of the subject as soo…
…n as the last event is done and prevents emitting new events. Force heartbeat to be done and don't queue it.
Showing
5 changed files
with
22 additions
and
15 deletions
... | ... | @@ -29,6 +29,7 @@ |
29 | 29 | #include "trbase.h" |
30 | 30 | |
31 | 31 | TR_CLASS(TR_EventSubject) { |
32 | + int fin; | |
32 | 33 | size_t emitted; |
33 | 34 | }; |
34 | 35 | TR_INSTANCE_INIT(TR_EventSubject); |
... | ... | @@ -55,6 +56,8 @@ TR_CLASSVARS_DECL(TR_EventSubject) { |
55 | 56 | intptr_t TR__eventSubjectId(TR_class_ptr, size_t); |
56 | 57 | TR_Event TR_eventSubjectEmit(TR_EventSubject, int, void *); |
57 | 58 | |
59 | +#define TR_eventSubjectFinalize(es) ((es)->fin = TRUE) | |
60 | + | |
58 | 61 | #endif // __TR_EVENT_SUBJECT_H__ |
59 | 62 | |
60 | 63 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -45,7 +45,13 @@ static void eventDtor(void * _this) |
45 | 45 | { |
46 | 46 | TR_Event this = _this; |
47 | 47 | |
48 | - this->subject->emitted--; | |
48 | + this->subject->emitted = | |
49 | + this->subject->emitted == 0 | |
50 | + ? 0 : this->subject->emitted - 1; | |
51 | + | |
52 | + if (0 == this->subject->emitted && this->subject->fin) { | |
53 | + TR_delete(this->subject); | |
54 | + } | |
49 | 55 | } |
50 | 56 | |
51 | 57 | TR_INIT_IFACE(TR_Class, eventCtor, eventDtor, NULL); | ... | ... |
... | ... | @@ -49,15 +49,11 @@ TR_eventDispatcherStart(TR_EventDispatcher this) |
49 | 49 | |
50 | 50 | if (this->nextbeat && this->nextbeat <= now) { |
51 | 51 | this->nextbeat += this->heartbeat; |
52 | - TR_eventDispatcherEnqueueEvent( | |
53 | - this, | |
54 | - TR_eventSubjectEmit( | |
55 | - (TR_EventSubject)this, | |
56 | - TR_DISPATCHER_EVENT_HEARTBEAT, | |
57 | - NULL)); | |
58 | - } | |
59 | - | |
60 | - if (TR_queueEmpty(this->events) || this->nextpoll <= now) { | |
52 | + event = TR_eventSubjectEmit( | |
53 | + (TR_EventSubject)this, | |
54 | + TR_DISPATCHER_EVENT_HEARTBEAT, | |
55 | + NULL); | |
56 | + } else if (TR_queueEmpty(this->events) || this->nextpoll <= now) { | |
61 | 57 | int evtid = TR_EVD_CLIENT == this->mode |
62 | 58 | ? TR_DISPATCHER_EVENT_USER_WAIT |
63 | 59 | : TR_DISPATCHER_EVENT_DATA_WAIT; | ... | ... |
... | ... | @@ -30,8 +30,10 @@ TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event) |
30 | 30 | { |
31 | 31 | int i; |
32 | 32 | |
33 | - for (i=0; i<this->ndispatcher; i++) { | |
34 | - TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event); | |
33 | + if (event) { | |
34 | + for (i=0; i<this->ndispatcher; i++) { | |
35 | + TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event); | |
36 | + } | |
35 | 37 | } |
36 | 38 | } |
37 | 39 | ... | ... |
... | ... | @@ -20,6 +20,7 @@ |
20 | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | 21 | */ |
22 | 22 | |
23 | +#include <stdio.h> | |
23 | 24 | #include <stdint.h> |
24 | 25 | |
25 | 26 | #include "tr/event.h" |
... | ... | @@ -33,13 +34,12 @@ TR_eventSubjectEmit(TR_EventSubject this, int idx, void * data) |
33 | 34 | intptr_t id = TR_eventSubjectGetId(this, idx); |
34 | 35 | TR_Event event = NULL; |
35 | 36 | |
36 | - if (id) { | |
37 | + if (id && ! this->fin) { | |
37 | 38 | event = TR_new(TR_Event, id, this); |
38 | 39 | TR_eventSetData(event, data); |
40 | + this->emitted++; | |
39 | 41 | } |
40 | 42 | |
41 | - this->emitted++; | |
42 | - | |
43 | 43 | return event; |
44 | 44 | } |
45 | 45 | ... | ... |
Please
register
or
login
to post a comment