Commit f6254dfb48838da1409f8fd846c5160065e79332

Authored by Georg Hopp
1 parent a17bd09d

preparation for edge triggerd events

... ... @@ -51,8 +51,10 @@ TR_CLASS(TR_EventDispatcher) {
51 51 TR_Hash handler;
52 52 TR_EventHandler default_handler;
53 53 int running;
54   - int heartbeat; // milliseconds
55   - int nextbeat; // milliseconds
  54 + int pollinterval; // milliseconds
  55 + int nextpoll; // milliseconds
  56 + int heartbeat; // milliseconds
  57 + int nextbeat; // milliseconds
56 58 TR_EventDispatcherMode mode;
57 59 };
58 60 TR_INSTANCE_INIT(TR_EventDispatcher);
... ...
... ... @@ -23,6 +23,7 @@
23 23 #include <stdint.h>
24 24 #include <signal.h>
25 25 #include <stdio.h>
  26 +#include <time.h>
26 27
27 28 #include "trbase.h"
28 29 #include "trdata.h"
... ... @@ -63,6 +64,8 @@ static
63 64 int
64 65 eventDispatcherCtor(void * _this, va_list * params) {
65 66 TR_EventDispatcher this = _this;
  67 + struct timespec tp;
  68 + int now; // milliseconds
66 69
67 70 this->events = TR_new(TR_Queue);
68 71 this->handler = TR_new(TR_Hash);
... ... @@ -71,6 +74,12 @@ eventDispatcherCtor(void * _this, va_list * params) {
71 74 this->running = 0;
72 75 this->heartbeat = 0;
73 76 this->nextbeat = 0;
  77 + this->pollinterval = va_arg(*params, int);
  78 +
  79 + clock_gettime(CLOCK_REALTIME, &tp);
  80 + now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
  81 +
  82 + this->nextpoll = now + this->pollinterval;
74 83
75 84 if (! _TR_controlDispatcher) {
76 85 _TR_controlDispatcher = this;
... ...
... ... @@ -30,6 +30,8 @@
30 30 #include "tr/event_subject.h"
31 31 #include "tr/event_dispatcher.h"
32 32
  33 +int ZERO = 0;
  34 +
33 35 void
34 36 TR_eventDispatcherStart(TR_EventDispatcher this)
35 37 {
... ... @@ -55,18 +57,14 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
55 57 NULL));
56 58 }
57 59
58   - if (TR_queueEmpty(this->events)) {
59   - if (TR_EVD_CLIENT == this->mode) {
60   - event = TR_eventSubjectEmit(
61   - (TR_EventSubject)this,
62   - TR_DISPATCHER_EVENT_USER_WAIT,
63   - NULL);
64   - } else {
65   - event = TR_eventSubjectEmit(
66   - (TR_EventSubject)this,
67   - TR_DISPATCHER_EVENT_DATA_WAIT,
68   - NULL);
69   - }
  60 + if (TR_queueEmpty(this->events) || this->nextpoll <= now) {
  61 + int evtid = TR_EVD_CLIENT == this->mode
  62 + ? TR_DISPATCHER_EVENT_USER_WAIT
  63 + : TR_DISPATCHER_EVENT_DATA_WAIT;
  64 + int * toutptr = TR_queueEmpty(this->events) ? NULL : &ZERO;
  65 +
  66 + this->nextpoll += this->pollinterval;
  67 + event = TR_eventSubjectEmit((TR_EventSubject)this, evtid, toutptr);
70 68 } else {
71 69 event = TR_queueGet(this->events);
72 70 }
... ... @@ -83,7 +81,7 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
83 81
84 82 if (handler_queue && ! TR_queueEmpty(handler_queue)) {
85 83 TR_Queue queue_node = handler_queue->first;
86   - TR_EventDone done;
  84 + TR_EventDone done = TR_EVENT_PENDING;
87 85
88 86 while (queue_node) {
89 87 TR_EventHandler handler = queue_node->msg;
... ...
Please register or login to post a comment