Browse Source

add finalize for event subjects. This results in an delete of the subject as soon as the last event is done and prevents emitting new events. Force heartbeat to be done and don't queue it.

1.0.0
Georg Hopp 11 years ago
parent
commit
01463d7b28
  1. 3
      include/tr/event_subject.h
  2. 8
      src/event.c
  3. 14
      src/event_dispatcher_start.c
  4. 6
      src/event_handler_issue_event.c
  5. 6
      src/event_subject_emit.c

3
include/tr/event_subject.h

@ -29,6 +29,7 @@
#include "trbase.h" #include "trbase.h"
TR_CLASS(TR_EventSubject) { TR_CLASS(TR_EventSubject) {
int fin;
size_t emitted; size_t emitted;
}; };
TR_INSTANCE_INIT(TR_EventSubject); TR_INSTANCE_INIT(TR_EventSubject);
@ -55,6 +56,8 @@ TR_CLASSVARS_DECL(TR_EventSubject) {
intptr_t TR__eventSubjectId(TR_class_ptr, size_t); intptr_t TR__eventSubjectId(TR_class_ptr, size_t);
TR_Event TR_eventSubjectEmit(TR_EventSubject, int, void *); TR_Event TR_eventSubjectEmit(TR_EventSubject, int, void *);
#define TR_eventSubjectFinalize(es) ((es)->fin = TRUE)
#endif // __TR_EVENT_SUBJECT_H__ #endif // __TR_EVENT_SUBJECT_H__
// vim: set ts=4 sw=4: // vim: set ts=4 sw=4:

8
src/event.c

@ -45,7 +45,13 @@ static void eventDtor(void * _this)
{ {
TR_Event this = _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); TR_INIT_IFACE(TR_Class, eventCtor, eventDtor, NULL);

14
src/event_dispatcher_start.c

@ -49,15 +49,11 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
if (this->nextbeat && this->nextbeat <= now) { if (this->nextbeat && this->nextbeat <= now) {
this->nextbeat += this->heartbeat; 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 int evtid = TR_EVD_CLIENT == this->mode
? TR_DISPATCHER_EVENT_USER_WAIT ? TR_DISPATCHER_EVENT_USER_WAIT
: TR_DISPATCHER_EVENT_DATA_WAIT; : TR_DISPATCHER_EVENT_DATA_WAIT;

6
src/event_handler_issue_event.c

@ -30,8 +30,10 @@ TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event)
{ {
int i; int i;
for (i=0; i<this->ndispatcher; i++) {
TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event);
if (event) {
for (i=0; i<this->ndispatcher; i++) {
TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event);
}
} }
} }

6
src/event_subject_emit.c

@ -20,6 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include "tr/event.h" #include "tr/event.h"
@ -33,13 +34,12 @@ TR_eventSubjectEmit(TR_EventSubject this, int idx, void * data)
intptr_t id = TR_eventSubjectGetId(this, idx); intptr_t id = TR_eventSubjectGetId(this, idx);
TR_Event event = NULL; TR_Event event = NULL;
if (id) {
if (id && ! this->fin) {
event = TR_new(TR_Event, id, this); event = TR_new(TR_Event, id, this);
TR_eventSetData(event, data); TR_eventSetData(event, data);
this->emitted++;
} }
this->emitted++;
return event; return event;
} }

Loading…
Cancel
Save