summaryrefslogtreecommitdiffstats
path: root/threads.c
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2007-12-14 16:51:34 +0000
committerRainer Gerhards <rgerhards@adiscon.com>2007-12-14 16:51:34 +0000
commit6c0c26dc96544aa5814d00045b3d559c99fc1b2e (patch)
tree20d558391ba1d881ab3b27f7fbcc636dcfbc67ad /threads.c
parent6a80d9ee504b57e2b815c91698785d4fcd06f62e (diff)
downloadrsyslog-6c0c26dc96544aa5814d00045b3d559c99fc1b2e.tar.gz
rsyslog-6c0c26dc96544aa5814d00045b3d559c99fc1b2e.tar.xz
rsyslog-6c0c26dc96544aa5814d00045b3d559c99fc1b2e.zip
on the way to a real input module interface and threading class...
Diffstat (limited to 'threads.c')
-rw-r--r--threads.c96
1 files changed, 95 insertions, 1 deletions
diff --git a/threads.c b/threads.c
index c3aee4b8..d5168d02 100644
--- a/threads.c
+++ b/threads.c
@@ -27,17 +27,111 @@
#include "config.h"
#include "rsyslog.h"
-#include <pthread.h>
#include <stdlib.h>
#include <string.h>
+#include <signal.h>
+#include <pthread.h>
#include <assert.h>
#include "syslogd.h"
+#include "linkedlist.h"
#include "threads.h"
+/* static data */
int iMainMsgQueueSize;
msgQueue *pMsgQueue = NULL;
+/* linked list of currently-known threads */
+static linkedList_t llThrds;
+
+/* methods */
+
+/* Construct a new thread object
+ */
+static rsRetVal thrdConstruct(thrdInfo_t **pThis)
+{
+ thrdInfo_t *pNew;
+
+ if((pNew = calloc(1, sizeof(thrdInfo_t))) == NULL)
+ return RS_RET_OUT_OF_MEMORY;
+
+ /* OK, we got the element, now initialize members that should
+ * not be zero-filled.
+ */
+
+ *pThis = pNew;
+ return RS_RET_OK;
+}
+
+
+/* Destructs a thread object. The object must not be linked to the
+ * linked list of threads. Please note that the thread should have been
+ * stopped before. If not, we try to do it.
+ */
+static rsRetVal thrdDestruct(thrdInfo_t *pThis)
+{
+ assert(pThis != NULL);
+
+ if(pThis->bIsActive == 1) {
+ thrdTerminate(pThis);
+ }
+ free(pThis);
+
+ return RS_RET_OK;
+}
+
+
+/* terminate a thread gracefully. It's termination sync state is taken into
+ * account.
+ */
+rsRetVal thrdTerminate(thrdInfo_t *pThis)
+{
+ assert(pThis != NULL);
+
+ if(pThis->eTermTool == eTermSync_SIGNAL) {
+ pthread_kill(pThis->thrdID, SIGUSR2);
+ pthread_join(pThis->thrdID, NULL);
+ /* TODO: TIMEOUT! */
+ } else if(pThis->eTermTool == eTermSync_NONE) {
+ pthread_cancel(pThis->thrdID);
+ }
+ pThis->bIsActive = 0;
+
+ return RS_RET_OK;
+}
+
+
+/* initialize the thread-support subsystem
+ * must be called once at the start of the program
+ */
+rsRetVal thrdInit(void)
+{
+ DEFiRet;
+
+ iRet = llInit(&llThrds, thrdDestruct, NULL, NULL);
+
+ return iRet;
+}
+
+
+/* de-initialize the thread subsystem
+ * must be called once at the end of the program
+ */
+rsRetVal thrdExit(void)
+{
+ DEFiRet;
+
+ iRet = llDestroy(&llThrds);
+
+ return iRet;
+}
+
+
+
+/* queue functions (may be migrated to some other file...)
+ */
+
+
msgQueue *queueInit (void)
{
msgQueue *q;