Browse Source

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

This reverts commit fcbef2f039.
1.0.0
Georg Hopp 11 years ago
parent
commit
205ead2576
  1. 3
      include/Makefile.am
  2. 13
      include/tr/event_dispatcher.h
  3. 44
      include/tr/event_thread.h
  4. 1
      include/trevent.h
  5. 5
      src/Makefile.am
  6. 9
      src/event_dispatcher.c
  7. 26
      src/event_dispatcher_start.c
  8. 21
      src/event_handler_handle_event.c
  9. 9
      src/event_handler_issue_event.c
  10. 56
      src/event_thread.c
  11. 65
      src/event_thread_join.c
  12. 57
      src/event_thread_start.c

3
include/Makefile.am

@ -2,5 +2,4 @@ nobase_include_HEADERS = trevent.h \
tr/event.h \
tr/event_handler.h \
tr/event_subject.h \
tr/event_dispatcher.h \
tr/event_thread.h
tr/event_dispatcher.h

13
include/tr/event_dispatcher.h

@ -25,7 +25,6 @@
#include <time.h>
#include <stdint.h>
#include <pthread.h>
#include "trbase.h"
#include "trdata.h"
@ -50,10 +49,6 @@ TR_CLASS(TR_EventDispatcher) {
TR_EXTENDS(TR_EventSubject);
TR_Queue events;
pthread_mutex_t events_lock;
pthread_cond_t events_cond;
pthread_t events_wait;
TR_Hash handler;
TR_EventHandler default_handler;
int running;
@ -83,12 +78,8 @@ TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher);
void TR_eventDispatcherStart(TR_EventDispatcher);
void TR_eventDispatcherShutdown(TR_EventDispatcher);
#define TR_eventDispatcherEnqueueEvent(disp,ev) \
pthread_mutex_lock(&((disp)->events_lock)); \
TR_queuePut((disp)->events, (ev)); \
pthread_cond_broadcast(&((disp)->events_cond)); \
pthread_mutex_unlock(&((disp)->events_lock))
#define TR_eventDispatcherEnqueueEvent(disp,ev) \
(TR_queuePut((disp)->events, (ev)))
#define TR_eventDispatcherStop(disp) \
(((TR_EventDispatcher)disp)->running = 0)
#define TR_eventDispatcherSetHeartbeat(disp, beat) \

44
include/tr/event_thread.h

@ -1,44 +0,0 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TR_EVENT_THREAD_H__
#define __TR_EVENT_THREAD_H__
#include <pthread.h>
#include "trbase.h"
#include "event_dispatcher.h"
TR_CLASS(TR_EventThread) {
TR_EventDispatcher dispatcher;
pthread_t handle;
};
TR_INSTANCE_INIT(TR_EventThread);
TR_CLASSVARS_DECL(TR_EventThread) {};
void TR_eventThreadStart(TR_EventThread);
void TR_eventThreadJoin(TR_EventThread);
#endif // __TR_EVENT_THREAD_H__
// vim: set ts=4 sw=4:

1
include/trevent.h

@ -5,7 +5,6 @@
#include "tr/event_handler.h"
#include "tr/event_subject.h"
#include "tr/event_dispatcher.h"
#include "tr/event_thread.h"
#endif // __TR_EVENT_H__

5
src/Makefile.am

@ -18,10 +18,7 @@ TREVENT = event.c \
event_handler_class_cleanup.c \
event_subject.c \
event_subject_emit.c \
event_subject_id.c \
event_thread.c \
event_thread_start.c \
event_thread_join.c
event_subject_id.c
lib_LTLIBRARIES = libtrevent.la

9
src/event_dispatcher.c

@ -24,7 +24,6 @@
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include "trbase.h"
#include "trdata.h"
@ -66,10 +65,7 @@ int
eventDispatcherCtor(void * _this, va_list * params) {
TR_EventDispatcher this = _this;
this->events = TR_new(TR_Queue);
pthread_mutex_init(&(this->events_lock), NULL);
pthread_cond_init(&(this->events_cond), NULL);
this->events = TR_new(TR_Queue);
this->handler = TR_new(TR_Hash);
this->heartbeat = TR_new(TR_Timer, TR_TBASE_MIL, 1000);
this->mode = va_arg(*params, TR_EventDispatcherMode);
@ -102,9 +98,6 @@ eventDispatcherDtor(void * _this) {
TR_delete(this->heartbeat);
TR_delete(this->handler);
TR_delete(this->events);
pthread_mutex_destroy(&(this->events_lock));
pthread_cond_destroy(&(this->events_cond));
}
static

26
src/event_dispatcher_start.c

@ -40,8 +40,6 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
TR_Queue handler_queue;
TR_HashValue handler_queue_hv;
pthread_mutex_lock(&(this->events_lock));
TR_eventDispatcherGetBeatTime(this);
if (this->n_beats) {
@ -50,27 +48,15 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
TR_DISPATCHER_EVENT_HEARTBEAT,
NULL);
} 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;
int evtid = TR_EVD_CLIENT == this->mode
? TR_DISPATCHER_EVENT_USER_WAIT
: TR_DISPATCHER_EVENT_DATA_WAIT;
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;
}
event = TR_eventSubjectEmit((TR_EventSubject)this, evtid, NULL);
} else {
event = TR_queueGet(this->events);
}
pthread_mutex_unlock(&(this->events_lock));
if (! event) {
continue;
}
handler_queue_hv = TR_hashGetByVal(
this->handler,
TR_sdbm(
@ -103,10 +89,6 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
} else {
TR_delete(event);
}
if (pthread_equal(this->events_wait, pthread_self())) {
this->events_wait = FALSE;
}
}
}

21
src/event_handler_handle_event.c

@ -32,29 +32,26 @@
TR_EventDone
TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event)
{
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,
TR_sdbm((unsigned char *)&event->id, sizeof(event->id)));
if (! handle_func_hv) {
return 0;
}
event_func = *(TR_EventMethod_fptr *)handle_func_hv->value;
retval = event_func(this, event);
TR_loggerLog(TR_logger, TR_LOGGER_DEBUG,
"[%ld] - HANDLE(%zd): %s event on %p with no. %d",
pthread_self(),
"%zd - HANDLE(%zd): %s event on %p with no. %d",
this->dispatcher[0]->events->nmsg,
event->subject->emitted,
TR_getEventString(event),
event->subject,
event->serial);
return retval;
if (! handle_func_hv) {
return 0;
}
event_func = *(TR_EventMethod_fptr *)handle_func_hv->value;
return event_func(this, event);
}
// vim: set ts=4 sw=4:

9
src/event_handler_issue_event.c

@ -21,7 +21,6 @@
*/
#include <stdio.h>
#include <pthread.h>
#include "trbase.h"
@ -35,15 +34,15 @@ TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event)
int i;
for (i=0; i<this->ndispatcher; i++) {
TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event);
TR_loggerLog(TR_logger, TR_LOGGER_DEBUG,
"[%ld] - ISSUE(%zd): %s event on %p with no. %d",
pthread_self(),
"%zd - ISSUE(%zd): %s event on %p with no. %d",
this->dispatcher[i]->events->nmsg,
event->subject->emitted,
TR_getEventString(event),
event->subject,
event->serial);
TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event);
}
return TRUE;

56
src/event_thread.c

@ -1,56 +0,0 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include "trbase.h"
#include "tr/event_dispatcher.h"
#include "tr/event_thread.h"
static
int
eventThreadCtor(void * _this, va_list * params)
{
TR_EventThread this = _this;
this->dispatcher = va_arg(*params, TR_EventDispatcher);
return 0;
}
static
void
eventThreadDtor(void * _this)
{
TR_EventThread this = _this;
if (this->handle) {
pthread_cancel(this->handle);
pthread_join(this->handle, NULL);
}
}
TR_INIT_IFACE(TR_Class, eventThreadCtor, eventThreadDtor, NULL);
TR_CREATE_CLASS(TR_EventThread, NULL, NULL, TR_IF(TR_Class));
// vim: set ts=4 sw=4:

65
src/event_thread_join.c

@ -1,65 +0,0 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <errno.h>
#include "trbase.h"
#include "tr/event_thread.h"
void
TR_eventThreadJoin(TR_EventThread this)
{
int error = pthread_join(this->handle, NULL);
/*
* AFAIC there is no error condition from this function that
* should lead to a retry. Additionally pthread_join is
* continued after a signal, so all I do is log error and
* continue with the next.
*/
switch (error) {
case EDEADLK:
TR_loggerLog(
TR_logger,
TR_LOGGER_WARNING,
"Thread deadlock detected");
break;
case EINVAL:
TR_loggerLog(
TR_logger,
TR_LOGGER_WARNING,
"Tried to join a non joinable thread");
break;
case ESRCH:
TR_loggerLog(
TR_logger,
TR_LOGGER_WARNING,
"Tried to join non existent thread");
break;
}
}
// vim: set ts=4 sw=4:

57
src/event_thread_start.c

@ -1,57 +0,0 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include "trbase.h"
#include "tr/event_dispatcher.h"
#include "tr/event_thread.h"
static
void *
TR_eventStreadRun(void * message)
{
TR_EventThread this = message;
TR_eventDispatcherStart(this->dispatcher);
return NULL;
}
void
TR_eventThreadStart(TR_EventThread this)
{
int error = pthread_create(
&this->handle,
NULL,
TR_eventStreadRun,
(void *)this);
if (error) {
TR_loggerLog(
TR_logger,
TR_LOGGER_ERR,
"Thread creation failed with error code: %d",
error);
}
}
// vim: set ts=4 sw=4:
Loading…
Cancel
Save