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. 10
      src/event_dispatcher_start.c
  4. 2
      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"
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:

8
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);

10
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(
event = TR_eventSubjectEmit(
(TR_EventSubject)this,
TR_DISPATCHER_EVENT_HEARTBEAT,
NULL));
}
if (TR_queueEmpty(this->events) || this->nextpoll <= now) {
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;

2
src/event_handler_issue_event.c

@ -30,9 +30,11 @@ TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event)
{
int i;
if (event) {
for (i=0; i<this->ndispatcher; i++) {
TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event);
}
}
}
// vim: set ts=4 sw=4:

6
src/event_subject_emit.c

@ -20,6 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include "tr/event.h"
@ -33,12 +34,11 @@ 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++;
}
return event;
}

Loading…
Cancel
Save