Commit 205ead257646e43440cd867af30b602ab620d3f6

Authored by Georg Hopp
1 parent bce8f9ea

Revert "first try for a threaded event dispatcher, but this is not correctly working right now."

This reverts commit fcbef2f0.
... ... @@ -2,5 +2,4 @@ nobase_include_HEADERS = trevent.h \
2 2 tr/event.h \
3 3 tr/event_handler.h \
4 4 tr/event_subject.h \
5   - tr/event_dispatcher.h \
6   - tr/event_thread.h
  5 + tr/event_dispatcher.h
... ...
... ... @@ -25,7 +25,6 @@
25 25
26 26 #include <time.h>
27 27 #include <stdint.h>
28   -#include <pthread.h>
29 28
30 29 #include "trbase.h"
31 30 #include "trdata.h"
... ... @@ -50,10 +49,6 @@ TR_CLASS(TR_EventDispatcher) {
50 49 TR_EXTENDS(TR_EventSubject);
51 50
52 51 TR_Queue events;
53   - pthread_mutex_t events_lock;
54   - pthread_cond_t events_cond;
55   - pthread_t events_wait;
56   -
57 52 TR_Hash handler;
58 53 TR_EventHandler default_handler;
59 54 int running;
... ... @@ -83,12 +78,8 @@ TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher);
83 78 void TR_eventDispatcherStart(TR_EventDispatcher);
84 79 void TR_eventDispatcherShutdown(TR_EventDispatcher);
85 80
86   -#define TR_eventDispatcherEnqueueEvent(disp,ev) \
87   - pthread_mutex_lock(&((disp)->events_lock)); \
88   - TR_queuePut((disp)->events, (ev)); \
89   - pthread_cond_broadcast(&((disp)->events_cond)); \
90   - pthread_mutex_unlock(&((disp)->events_lock))
91   -
  81 +#define TR_eventDispatcherEnqueueEvent(disp,ev) \
  82 + (TR_queuePut((disp)->events, (ev)))
92 83 #define TR_eventDispatcherStop(disp) \
93 84 (((TR_EventDispatcher)disp)->running = 0)
94 85 #define TR_eventDispatcherSetHeartbeat(disp, beat) \
... ...
1   -/**
2   - * \file
3   - *
4   - * \author Georg Hopp
5   - *
6   - * \copyright
7   - * Copyright © 2014 Georg Hopp
8   - *
9   - * This program is free software: you can redistribute it and/or modify
10   - * it under the terms of the GNU General Public License as published by
11   - * the Free Software Foundation, either version 3 of the License, or
12   - * (at your option) any later version.
13   - *
14   - * This program is distributed in the hope that it will be useful,
15   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   - * GNU General Public License for more details.
18   - *
19   - * You should have received a copy of the GNU General Public License
20   - * along with this program. If not, see <http://www.gnu.org/licenses/>.
21   - */
22   -
23   -#ifndef __TR_EVENT_THREAD_H__
24   -#define __TR_EVENT_THREAD_H__
25   -
26   -#include <pthread.h>
27   -
28   -#include "trbase.h"
29   -#include "event_dispatcher.h"
30   -
31   -TR_CLASS(TR_EventThread) {
32   - TR_EventDispatcher dispatcher;
33   - pthread_t handle;
34   -};
35   -TR_INSTANCE_INIT(TR_EventThread);
36   -TR_CLASSVARS_DECL(TR_EventThread) {};
37   -
38   -void TR_eventThreadStart(TR_EventThread);
39   -void TR_eventThreadJoin(TR_EventThread);
40   -
41   -#endif // __TR_EVENT_THREAD_H__
42   -
43   -// vim: set ts=4 sw=4:
44   -
... ... @@ -5,7 +5,6 @@
5 5 #include "tr/event_handler.h"
6 6 #include "tr/event_subject.h"
7 7 #include "tr/event_dispatcher.h"
8   -#include "tr/event_thread.h"
9 8
10 9 #endif // __TR_EVENT_H__
11 10
... ...
... ... @@ -18,10 +18,7 @@ TREVENT = event.c \
18 18 event_handler_class_cleanup.c \
19 19 event_subject.c \
20 20 event_subject_emit.c \
21   - event_subject_id.c \
22   - event_thread.c \
23   - event_thread_start.c \
24   - event_thread_join.c
  21 + event_subject_id.c
25 22
26 23 lib_LTLIBRARIES = libtrevent.la
27 24
... ...
... ... @@ -24,7 +24,6 @@
24 24 #include <signal.h>
25 25 #include <stdio.h>
26 26 #include <time.h>
27   -#include <pthread.h>
28 27
29 28 #include "trbase.h"
30 29 #include "trdata.h"
... ... @@ -66,10 +65,7 @@ int
66 65 eventDispatcherCtor(void * _this, va_list * params) {
67 66 TR_EventDispatcher this = _this;
68 67
69   - this->events = TR_new(TR_Queue);
70   - pthread_mutex_init(&(this->events_lock), NULL);
71   - pthread_cond_init(&(this->events_cond), NULL);
72   -
  68 + this->events = TR_new(TR_Queue);
73 69 this->handler = TR_new(TR_Hash);
74 70 this->heartbeat = TR_new(TR_Timer, TR_TBASE_MIL, 1000);
75 71 this->mode = va_arg(*params, TR_EventDispatcherMode);
... ... @@ -102,9 +98,6 @@ eventDispatcherDtor(void * _this) {
102 98 TR_delete(this->heartbeat);
103 99 TR_delete(this->handler);
104 100 TR_delete(this->events);
105   -
106   - pthread_mutex_destroy(&(this->events_lock));
107   - pthread_cond_destroy(&(this->events_cond));
108 101 }
109 102
110 103 static
... ...
... ... @@ -40,8 +40,6 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
40 40 TR_Queue handler_queue;
41 41 TR_HashValue handler_queue_hv;
42 42
43   - pthread_mutex_lock(&(this->events_lock));
44   -
45 43 TR_eventDispatcherGetBeatTime(this);
46 44
47 45 if (this->n_beats) {
... ... @@ -50,27 +48,15 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
50 48 TR_DISPATCHER_EVENT_HEARTBEAT,
51 49 NULL);
52 50 } else if (TR_queueEmpty(this->events)) {
53   - if (! this->events_wait) {
54   - int evtid = TR_EVD_CLIENT == this->mode
55   - ? TR_DISPATCHER_EVENT_USER_WAIT
56   - : TR_DISPATCHER_EVENT_DATA_WAIT;
  51 + int evtid = TR_EVD_CLIENT == this->mode
  52 + ? TR_DISPATCHER_EVENT_USER_WAIT
  53 + : TR_DISPATCHER_EVENT_DATA_WAIT;
57 54
58   - this->events_wait = pthread_self();
59   - event = TR_eventSubjectEmit((TR_EventSubject)this, evtid, NULL);
60   - } else {
61   - pthread_cond_wait(&(this->events_cond), &(this->events_lock));
62   - event = NULL;
63   - }
  55 + event = TR_eventSubjectEmit((TR_EventSubject)this, evtid, NULL);
64 56 } else {
65 57 event = TR_queueGet(this->events);
66 58 }
67 59
68   - pthread_mutex_unlock(&(this->events_lock));
69   -
70   - if (! event) {
71   - continue;
72   - }
73   -
74 60 handler_queue_hv = TR_hashGetByVal(
75 61 this->handler,
76 62 TR_sdbm(
... ... @@ -103,10 +89,6 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
103 89 } else {
104 90 TR_delete(event);
105 91 }
106   -
107   - if (pthread_equal(this->events_wait, pthread_self())) {
108   - this->events_wait = FALSE;
109   - }
110 92 }
111 93 }
112 94
... ...
... ... @@ -32,29 +32,26 @@
32 32 TR_EventDone
33 33 TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event)
34 34 {
35   - TR_EventDone retval;
36 35 TR_EventMethod_fptr event_func = NULL;
37 36 TR_HashValue handle_func_hv = TR_hashGetByVal(
38 37 TR_CLASSVARS(TR_EventHandler, TR_GET_CLASS(this))->event_methods,
39 38 TR_sdbm((unsigned char *)&event->id, sizeof(event->id)));
40 39
41   - if (! handle_func_hv) {
42   - return 0;
43   - }
44   -
45   - event_func = *(TR_EventMethod_fptr *)handle_func_hv->value;
46   -
47   - retval = event_func(this, event);
48   -
49 40 TR_loggerLog(TR_logger, TR_LOGGER_DEBUG,
50   - "[%ld] - HANDLE(%zd): %s event on %p with no. %d",
51   - pthread_self(),
  41 + "%zd - HANDLE(%zd): %s event on %p with no. %d",
  42 + this->dispatcher[0]->events->nmsg,
52 43 event->subject->emitted,
53 44 TR_getEventString(event),
54 45 event->subject,
55 46 event->serial);
56 47
57   - return retval;
  48 + if (! handle_func_hv) {
  49 + return 0;
  50 + }
  51 +
  52 + event_func = *(TR_EventMethod_fptr *)handle_func_hv->value;
  53 +
  54 + return event_func(this, event);
58 55 }
59 56
60 57 // vim: set ts=4 sw=4:
... ...
... ... @@ -21,7 +21,6 @@
21 21 */
22 22
23 23 #include <stdio.h>
24   -#include <pthread.h>
25 24
26 25 #include "trbase.h"
27 26
... ... @@ -35,15 +34,15 @@ TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event)
35 34 int i;
36 35
37 36 for (i=0; i<this->ndispatcher; i++) {
  37 + TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event);
  38 +
38 39 TR_loggerLog(TR_logger, TR_LOGGER_DEBUG,
39   - "[%ld] - ISSUE(%zd): %s event on %p with no. %d",
40   - pthread_self(),
  40 + "%zd - ISSUE(%zd): %s event on %p with no. %d",
  41 + this->dispatcher[i]->events->nmsg,
41 42 event->subject->emitted,
42 43 TR_getEventString(event),
43 44 event->subject,
44 45 event->serial);
45   -
46   - TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event);
47 46 }
48 47
49 48 return TRUE;
... ...
1   -/**
2   - * \file
3   - *
4   - * \author Georg Hopp
5   - *
6   - * \copyright
7   - * Copyright © 2014 Georg Hopp
8   - *
9   - * This program is free software: you can redistribute it and/or modify
10   - * it under the terms of the GNU General Public License as published by
11   - * the Free Software Foundation, either version 3 of the License, or
12   - * (at your option) any later version.
13   - *
14   - * This program is distributed in the hope that it will be useful,
15   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   - * GNU General Public License for more details.
18   - *
19   - * You should have received a copy of the GNU General Public License
20   - * along with this program. If not, see <http://www.gnu.org/licenses/>.
21   - */
22   -
23   -#include <pthread.h>
24   -
25   -#include "trbase.h"
26   -
27   -#include "tr/event_dispatcher.h"
28   -#include "tr/event_thread.h"
29   -
30   -static
31   -int
32   -eventThreadCtor(void * _this, va_list * params)
33   -{
34   - TR_EventThread this = _this;
35   -
36   - this->dispatcher = va_arg(*params, TR_EventDispatcher);
37   -
38   - return 0;
39   -}
40   -
41   -static
42   -void
43   -eventThreadDtor(void * _this)
44   -{
45   - TR_EventThread this = _this;
46   -
47   - if (this->handle) {
48   - pthread_cancel(this->handle);
49   - pthread_join(this->handle, NULL);
50   - }
51   -}
52   -
53   -TR_INIT_IFACE(TR_Class, eventThreadCtor, eventThreadDtor, NULL);
54   -TR_CREATE_CLASS(TR_EventThread, NULL, NULL, TR_IF(TR_Class));
55   -
56   -// vim: set ts=4 sw=4:
1   -/**
2   - * \file
3   - *
4   - * \author Georg Hopp
5   - *
6   - * \copyright
7   - * Copyright © 2014 Georg Hopp
8   - *
9   - * This program is free software: you can redistribute it and/or modify
10   - * it under the terms of the GNU General Public License as published by
11   - * the Free Software Foundation, either version 3 of the License, or
12   - * (at your option) any later version.
13   - *
14   - * This program is distributed in the hope that it will be useful,
15   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   - * GNU General Public License for more details.
18   - *
19   - * You should have received a copy of the GNU General Public License
20   - * along with this program. If not, see <http://www.gnu.org/licenses/>.
21   - */
22   -
23   -#include <pthread.h>
24   -#include <errno.h>
25   -
26   -#include "trbase.h"
27   -
28   -#include "tr/event_thread.h"
29   -
30   -void
31   -TR_eventThreadJoin(TR_EventThread this)
32   -{
33   - int error = pthread_join(this->handle, NULL);
34   -
35   - /*
36   - * AFAIC there is no error condition from this function that
37   - * should lead to a retry. Additionally pthread_join is
38   - * continued after a signal, so all I do is log error and
39   - * continue with the next.
40   - */
41   - switch (error) {
42   - case EDEADLK:
43   - TR_loggerLog(
44   - TR_logger,
45   - TR_LOGGER_WARNING,
46   - "Thread deadlock detected");
47   - break;
48   -
49   - case EINVAL:
50   - TR_loggerLog(
51   - TR_logger,
52   - TR_LOGGER_WARNING,
53   - "Tried to join a non joinable thread");
54   - break;
55   -
56   - case ESRCH:
57   - TR_loggerLog(
58   - TR_logger,
59   - TR_LOGGER_WARNING,
60   - "Tried to join non existent thread");
61   - break;
62   - }
63   -}
64   -
65   -// vim: set ts=4 sw=4:
1   -/**
2   - * \file
3   - *
4   - * \author Georg Hopp
5   - *
6   - * \copyright
7   - * Copyright © 2014 Georg Hopp
8   - *
9   - * This program is free software: you can redistribute it and/or modify
10   - * it under the terms of the GNU General Public License as published by
11   - * the Free Software Foundation, either version 3 of the License, or
12   - * (at your option) any later version.
13   - *
14   - * This program is distributed in the hope that it will be useful,
15   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   - * GNU General Public License for more details.
18   - *
19   - * You should have received a copy of the GNU General Public License
20   - * along with this program. If not, see <http://www.gnu.org/licenses/>.
21   - */
22   -
23   -#include <pthread.h>
24   -
25   -#include "trbase.h"
26   -
27   -#include "tr/event_dispatcher.h"
28   -#include "tr/event_thread.h"
29   -
30   -static
31   -void *
32   -TR_eventStreadRun(void * message)
33   -{
34   - TR_EventThread this = message;
35   - TR_eventDispatcherStart(this->dispatcher);
36   - return NULL;
37   -}
38   -
39   -void
40   -TR_eventThreadStart(TR_EventThread this)
41   -{
42   - int error = pthread_create(
43   - &this->handle,
44   - NULL,
45   - TR_eventStreadRun,
46   - (void *)this);
47   -
48   - if (error) {
49   - TR_loggerLog(
50   - TR_logger,
51   - TR_LOGGER_ERR,
52   - "Thread creation failed with error code: %d",
53   - error);
54   - }
55   -}
56   -
57   -// vim: set ts=4 sw=4:
Please register or login to post a comment