From 48a57a14f5e76a7f79c4eb318cd2d2571f3aac82 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sat, 4 Oct 2014 10:36:08 +0100 Subject: [PATCH] Revert "more thread syncs." This reverts commit 5cd826f91a9ca56eda5cb2abcf073381a618fb56. --- include/tr/event_dispatcher.h | 3 +-- include/tr/event_handler.h | 7 ------ include/tr/event_subject.h | 16 ++----------- include/tr/event_thread.h | 5 ++-- src/event.c | 8 +++---- src/event_dispatcher.c | 1 - src/event_dispatcher_start.c | 40 ++++++++++---------------------- src/event_handler_handle_event.c | 12 ++++------ src/event_handler_issue_event.c | 11 +++------ src/event_subject.c | 17 ++------------ src/event_subject_emit.c | 3 --- src/event_thread.c | 8 +------ src/event_thread_start.c | 3 --- 13 files changed, 32 insertions(+), 102 deletions(-) diff --git a/include/tr/event_dispatcher.h b/include/tr/event_dispatcher.h index 39cd70f..20f38c4 100644 --- a/include/tr/event_dispatcher.h +++ b/include/tr/event_dispatcher.h @@ -52,8 +52,7 @@ TR_CLASS(TR_EventDispatcher) { TR_Queue events; pthread_mutex_t events_lock; pthread_cond_t events_cond; - pthread_t events_poll; - size_t events_handling; + pthread_t events_wait; TR_Hash handler; TR_EventHandler default_handler; diff --git a/include/tr/event_handler.h b/include/tr/event_handler.h index ad26d8a..e0ad39b 100644 --- a/include/tr/event_handler.h +++ b/include/tr/event_handler.h @@ -24,7 +24,6 @@ #define __TR_EVENT_HANDLER_H__ #include -#include #include "trbase.h" #include "trdata.h" @@ -69,12 +68,6 @@ void TR__eventHandlerClassCleanup(TR_class_ptr); sizeof(TR_EventMethod_fptr))); \ } while(0) -#define TR_INIT_HANDLER(cname) \ - TR_INSTANCE(TR_Tree, cname##EventMethodsTree, NULL, PTHREAD_MUTEX_INITIALIZER); \ - TR_INSTANCE(TR_Hash, cname##EventMethods, &(_##cname##EventMethodsTree.data), 0) - -#define TR_HANDLER_CVARS(cname) &(_##cname##EventMethods.data) - #endif // __TR_EVENT_HANDLER_H__ // vim: set ts=4 sw=4: diff --git a/include/tr/event_subject.h b/include/tr/event_subject.h index afb637b..83f2e40 100644 --- a/include/tr/event_subject.h +++ b/include/tr/event_subject.h @@ -25,14 +25,12 @@ #include #include -#include #include "trbase.h" TR_CLASS(TR_EventSubject) { - int fin; - size_t emitted; - pthread_mutex_t lock; + int fin; + size_t emitted; }; TR_INSTANCE_INIT(TR_EventSubject); TR_CLASSVARS_DECL(TR_EventSubject) { @@ -59,16 +57,6 @@ TR_CLASSVARS_DECL(TR_EventSubject) { intptr_t TR__eventSubjectId(TR_class_ptr, size_t); TR_Event TR_eventSubjectEmit(TR_EventSubject, int, void *); -static -inline -void -TR_eventSubjectAbsorb(TR_EventSubject this) -{ - pthread_mutex_lock(&this->lock); - this->emitted = this->emitted == 0 ? 0 : this->emitted - 1; - pthread_mutex_unlock(&this->lock); -} - #define TR_eventSubjectFinalize(es) ((es)->fin = TRUE) #endif // __TR_EVENT_SUBJECT_H__ diff --git a/include/tr/event_thread.h b/include/tr/event_thread.h index 88efabb..896fb1c 100644 --- a/include/tr/event_thread.h +++ b/include/tr/event_thread.h @@ -29,9 +29,8 @@ #include "event_dispatcher.h" TR_CLASS(TR_EventThread) { - TR_EventDispatcher dispatcher; - pthread_t handle; - char * name; + TR_EventDispatcher dispatcher; + pthread_t handle; }; TR_INSTANCE_INIT(TR_EventThread); TR_CLASSVARS_DECL(TR_EventThread) {}; diff --git a/src/event.c b/src/event.c index abbe6ab..458b452 100644 --- a/src/event.c +++ b/src/event.c @@ -41,13 +41,13 @@ eventCtor(void * _this, va_list * params) return 0; } -static -void -eventDtor(void * _this) +static void eventDtor(void * _this) { TR_Event this = _this; - TR_eventSubjectAbsorb(this->subject); + this->subject->emitted = + this->subject->emitted == 0 + ? 0 : this->subject->emitted - 1; if (0 == this->subject->emitted && this->subject->fin) { TR_delete(this->subject); diff --git a/src/event_dispatcher.c b/src/event_dispatcher.c index 7d6ab80..9c2ae00 100644 --- a/src/event_dispatcher.c +++ b/src/event_dispatcher.c @@ -57,7 +57,6 @@ init_signals(void) signal(SIGABRT, terminate); signal(SIGALRM, SIG_IGN); signal(SIGURG, SIG_IGN); - signal(SIGUSR1, SIG_IGN); signal(SIGPIPE, SIG_IGN); } diff --git a/src/event_dispatcher_start.c b/src/event_dispatcher_start.c index c616c1f..7e38517 100644 --- a/src/event_dispatcher_start.c +++ b/src/event_dispatcher_start.c @@ -20,8 +20,6 @@ * along with this program. If not, see . */ -#define _GNU_SOURCE - #include "trbase.h" #include "trdata.h" #include "trhash.h" @@ -51,39 +49,28 @@ TR_eventDispatcherStart(TR_EventDispatcher this) (TR_EventSubject)this, TR_DISPATCHER_EVENT_HEARTBEAT, NULL); - } else { - event = TR_queueGet(this->events); - - if (! (event || this->events_poll || this->events_handling)) { + } else if (TR_queueEmpty(this->events)) { + if (! this->events_wait) { int evtid = TR_EVD_CLIENT == this->mode ? TR_DISPATCHER_EVENT_USER_WAIT : TR_DISPATCHER_EVENT_DATA_WAIT; - this->events_poll = pthread_self(); + this->events_wait = pthread_self(); event = TR_eventSubjectEmit((TR_EventSubject)this, evtid, NULL); + } else { + pthread_cond_wait(&(this->events_cond), &(this->events_lock)); + event = NULL; } + } else { + event = TR_queueGet(this->events); } + pthread_mutex_unlock(&(this->events_lock)); + if (! event) { - char buffer[17]; - - pthread_getname_np(pthread_self(), buffer, 17); - TR_loggerLog(TR_logger, TR_LOGGER_DEBUG, - "[%s] - enter cond wait", - buffer); - pthread_cond_wait(&(this->events_cond), &(this->events_lock)); - TR_loggerLog(TR_logger, TR_LOGGER_DEBUG, - "[%s] - leave cond wait", - buffer); - event = NULL; - pthread_mutex_unlock(&(this->events_lock)); continue; - } else { - this->events_handling++; } - pthread_mutex_unlock(&(this->events_lock)); - handler_queue_hv = TR_hashGetByVal( this->handler, TR_sdbm( @@ -117,12 +104,9 @@ TR_eventDispatcherStart(TR_EventDispatcher this) TR_delete(event); } - pthread_mutex_lock(&(this->events_lock)); - this->events_handling--; - if (pthread_equal(this->events_poll, pthread_self())) { - this->events_poll = FALSE; + if (pthread_equal(this->events_wait, pthread_self())) { + this->events_wait = FALSE; } - pthread_mutex_unlock(&(this->events_lock)); } } diff --git a/src/event_handler_handle_event.c b/src/event_handler_handle_event.c index c463d4e..23f5a30 100644 --- a/src/event_handler_handle_event.c +++ b/src/event_handler_handle_event.c @@ -20,8 +20,6 @@ * along with this program. If not, see . */ -#define _GNU_SOURCE - #include #include "trbase.h" @@ -34,7 +32,7 @@ TR_EventDone TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event) { - char buffer[17]; + TR_EventDone retval; TR_EventMethod_fptr event_func = NULL; TR_HashValue handle_func_hv = TR_hashGetByVal( TR_CLASSVARS(TR_EventHandler, TR_GET_CLASS(this))->event_methods, @@ -46,17 +44,17 @@ TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event) event_func = *(TR_EventMethod_fptr *)handle_func_hv->value; - pthread_getname_np(pthread_self(), buffer, 17); + retval = event_func(this, event); TR_loggerLog(TR_logger, TR_LOGGER_DEBUG, - "[%s] - HANDLE(%zd): %s event on %p with no. %d", - buffer, + "[%ld] - HANDLE(%zd): %s event on %p with no. %d", + pthread_self(), event->subject->emitted, TR_getEventString(event), event->subject, event->serial); - return event_func(this, event); + return retval; } // vim: set ts=4 sw=4: diff --git a/src/event_handler_issue_event.c b/src/event_handler_issue_event.c index 9bd4882..e339c0b 100644 --- a/src/event_handler_issue_event.c +++ b/src/event_handler_issue_event.c @@ -20,8 +20,6 @@ * along with this program. If not, see . */ -#define _GNU_SOURCE - #include #include @@ -34,15 +32,12 @@ int TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event) { if (event) { - int i; - char buffer[17]; - - pthread_getname_np(pthread_self(), buffer, 17); + int i; for (i=0; indispatcher; i++) { TR_loggerLog(TR_logger, TR_LOGGER_DEBUG, - "[%s] - ISSUE(%zd): %s event on %p with no. %d", - buffer, + "[%ld] - ISSUE(%zd): %s event on %p with no. %d", + pthread_self(), event->subject->emitted, TR_getEventString(event), event->subject, diff --git a/src/event_subject.c b/src/event_subject.c index c049e99..7e3d0f8 100644 --- a/src/event_subject.c +++ b/src/event_subject.c @@ -27,21 +27,8 @@ #include "tr/logger.h" #include "trbase.h" -static -int -eventSubjectCtor(void * _this, va_list * params) -{ - pthread_mutex_init(&((TR_EventSubject)_this)->lock, NULL); - - return 0; -} - -static -void -eventSubjectDtor(void * _this) -{ - pthread_mutex_destroy(&((TR_EventSubject)_this)->lock); -} +static int eventSubjectCtor(void * _this, va_list * params) { return 0; } +static void eventSubjectDtor(void * _this) {} TR_INIT_IFACE(TR_Class, eventSubjectCtor, eventSubjectDtor, NULL); TR_CREATE_CLASS(TR_EventSubject, NULL, NULL, TR_IF(TR_Class)) = { diff --git a/src/event_subject_emit.c b/src/event_subject_emit.c index 99da60c..4405380 100644 --- a/src/event_subject_emit.c +++ b/src/event_subject_emit.c @@ -22,7 +22,6 @@ #include #include -#include #include "tr/event.h" #include "tr/event_subject.h" @@ -38,9 +37,7 @@ TR_eventSubjectEmit(TR_EventSubject this, int idx, void * data) if (id && ! this->fin) { event = TR_new(TR_Event, id, this); TR_eventSetData(event, data); - pthread_mutex_lock(&this->lock); this->emitted++; - pthread_mutex_unlock(&this->lock); } return event; diff --git a/src/event_thread.c b/src/event_thread.c index 756ed25..4b3062e 100644 --- a/src/event_thread.c +++ b/src/event_thread.c @@ -31,13 +31,9 @@ static int eventThreadCtor(void * _this, va_list * params) { - TR_EventThread this = _this; - char * name; + TR_EventThread this = _this; this->dispatcher = va_arg(*params, TR_EventDispatcher); - name = va_arg(*params, char *); - - if (name) this->name = TR_strdup(name); return 0; } @@ -52,8 +48,6 @@ eventThreadDtor(void * _this) pthread_cancel(this->handle); pthread_join(this->handle, NULL); } - - TR_MEM_FREE(this->name); } TR_INIT_IFACE(TR_Class, eventThreadCtor, eventThreadDtor, NULL); diff --git a/src/event_thread_start.c b/src/event_thread_start.c index c7bedea..deca49f 100644 --- a/src/event_thread_start.c +++ b/src/event_thread_start.c @@ -20,8 +20,6 @@ * along with this program. If not, see . */ -#define _GNU_SOURCE - #include #include "trbase.h" @@ -34,7 +32,6 @@ void * TR_eventStreadRun(void * message) { TR_EventThread this = message; - if (this->name) pthread_setname_np(pthread_self(), this->name); TR_eventDispatcherStart(this->dispatcher); return NULL; }