diff --git a/include/tr/event_subject.h b/include/tr/event_subject.h index 1b1c9bb..2b8e65c 100644 --- a/include/tr/event_subject.h +++ b/include/tr/event_subject.h @@ -29,6 +29,7 @@ #include "trbase.h" TR_CLASS(TR_EventSubject) { + int fin; size_t emitted; }; TR_INSTANCE_INIT(TR_EventSubject); @@ -55,6 +56,8 @@ TR_CLASSVARS_DECL(TR_EventSubject) { intptr_t TR__eventSubjectId(TR_class_ptr, size_t); TR_Event TR_eventSubjectEmit(TR_EventSubject, int, void *); +#define TR_eventSubjectFinalize(es) ((es)->fin = TRUE) + #endif // __TR_EVENT_SUBJECT_H__ // vim: set ts=4 sw=4: diff --git a/src/event.c b/src/event.c index c140c52..458b452 100644 --- a/src/event.c +++ b/src/event.c @@ -45,7 +45,13 @@ static void eventDtor(void * _this) { TR_Event this = _this; - this->subject->emitted--; + this->subject->emitted = + this->subject->emitted == 0 + ? 0 : this->subject->emitted - 1; + + if (0 == this->subject->emitted && this->subject->fin) { + TR_delete(this->subject); + } } TR_INIT_IFACE(TR_Class, eventCtor, eventDtor, NULL); diff --git a/src/event_dispatcher_start.c b/src/event_dispatcher_start.c index 0ad631d..8a1f927 100644 --- a/src/event_dispatcher_start.c +++ b/src/event_dispatcher_start.c @@ -49,15 +49,11 @@ TR_eventDispatcherStart(TR_EventDispatcher this) if (this->nextbeat && this->nextbeat <= now) { this->nextbeat += this->heartbeat; - TR_eventDispatcherEnqueueEvent( - this, - TR_eventSubjectEmit( - (TR_EventSubject)this, - TR_DISPATCHER_EVENT_HEARTBEAT, - NULL)); - } - - if (TR_queueEmpty(this->events) || this->nextpoll <= now) { + event = TR_eventSubjectEmit( + (TR_EventSubject)this, + TR_DISPATCHER_EVENT_HEARTBEAT, + NULL); + } else if (TR_queueEmpty(this->events) || this->nextpoll <= now) { int evtid = TR_EVD_CLIENT == this->mode ? TR_DISPATCHER_EVENT_USER_WAIT : TR_DISPATCHER_EVENT_DATA_WAIT; diff --git a/src/event_handler_issue_event.c b/src/event_handler_issue_event.c index 59afbee..30eef98 100644 --- a/src/event_handler_issue_event.c +++ b/src/event_handler_issue_event.c @@ -30,8 +30,10 @@ TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event) { int i; - for (i=0; indispatcher; i++) { - TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event); + if (event) { + for (i=0; indispatcher; i++) { + TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event); + } } } diff --git a/src/event_subject_emit.c b/src/event_subject_emit.c index 283699f..4405380 100644 --- a/src/event_subject_emit.c +++ b/src/event_subject_emit.c @@ -20,6 +20,7 @@ * along with this program. If not, see . */ +#include #include #include "tr/event.h" @@ -33,13 +34,12 @@ TR_eventSubjectEmit(TR_EventSubject this, int idx, void * data) intptr_t id = TR_eventSubjectGetId(this, idx); TR_Event event = NULL; - if (id) { + if (id && ! this->fin) { event = TR_new(TR_Event, id, this); TR_eventSetData(event, data); + this->emitted++; } - this->emitted++; - return event; }