diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2008-01-03 10:42:46 +0000 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2008-01-03 10:42:46 +0000 |
commit | 48cb0a980f657fe1d6484a1322db26c753835f03 (patch) | |
tree | ac9c969f85b51d1f7dbef1765ec641090e3e17d5 | |
parent | 29d9729292d44d2827054a1aef27278f3dadd57e (diff) | |
download | rsyslog-48cb0a980f657fe1d6484a1322db26c753835f03.tar.gz rsyslog-48cb0a980f657fe1d6484a1322db26c753835f03.tar.xz rsyslog-48cb0a980f657fe1d6484a1322db26c753835f03.zip |
restructured queue interface to use rsRetVal and instances, removed
dependency on globals - now more like a real class
-rw-r--r-- | queue.c | 130 | ||||
-rw-r--r-- | queue.h | 30 | ||||
-rw-r--r-- | syslogd.c | 18 |
3 files changed, 97 insertions, 81 deletions
@@ -34,50 +34,72 @@ #include "queue.h" /* static data */ -int iMainMsgQueueSize; -msgQueue *pMsgQueue = NULL; /* methods */ -/* queue functions (may be migrated to some other file...) - */ +/* Constructor for the queue object */ +rsRetVal queueConstruct(queue_t **ppThis, queueType_t qType, int iMaxQueueSize) +{ + DEFiRet; + queue_t *pThis; + assert(ppThis != NULL); +dbgprintf("queueConstruct in \n"); -msgQueue *queueInit (void) -{ - msgQueue *q; + if((pThis = (queue_t *)malloc(sizeof(queue_t))) == NULL) { + ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); + } - q = (msgQueue *)malloc(sizeof(msgQueue)); - if (q == NULL) return (NULL); - if((q->pbuf = malloc(sizeof(void *) * iMainMsgQueueSize)) == NULL) { - free(q); - return NULL; + /* we have an object, so let's fill the properties */ + pThis->iMaxQueueSize = iMaxQueueSize; + pThis->empty = 1; + pThis->full = 0; + pThis->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t)); + pthread_mutex_init (pThis->mut, NULL); + pThis->notFull = (pthread_cond_t *) malloc (sizeof (pthread_cond_t)); + pthread_cond_init (pThis->notFull, NULL); + pThis->notEmpty = (pthread_cond_t *) malloc (sizeof (pthread_cond_t)); + pthread_cond_init (pThis->notEmpty, NULL); + pThis->qType = qType; + + /* type-specific initialization */ + if((pThis->tVars.farray.pBuf = malloc(sizeof(void *) * pThis->iMaxQueueSize)) == NULL) { + ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); } - q->empty = 1; - q->full = 0; - q->head = 0; - q->tail = 0; - q->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t)); - pthread_mutex_init (q->mut, NULL); - q->notFull = (pthread_cond_t *) malloc (sizeof (pthread_cond_t)); - pthread_cond_init (q->notFull, NULL); - q->notEmpty = (pthread_cond_t *) malloc (sizeof (pthread_cond_t)); - pthread_cond_init (q->notEmpty, NULL); + pThis->tVars.farray.head = 0; + pThis->tVars.farray.tail = 0; - return (q); +finalize_it: + if(iRet == RS_RET_OK) { + *ppThis = pThis; + } else { + if(pThis != NULL) + free(pThis); + } + + return iRet; } -void queueDelete (msgQueue *q) + +/* destructor for the queue object */ +rsRetVal queueDestruct(queue_t *pThis) { - pthread_mutex_destroy (q->mut); - free (q->mut); - pthread_cond_destroy (q->notFull); - free (q->notFull); - pthread_cond_destroy (q->notEmpty); - free (q->notEmpty); - free(q->pbuf); - free (q); + DEFiRet; + +dbgprintf("queueDestruct\n"); + assert(pThis != NULL); + pthread_mutex_destroy (pThis->mut); + free (pThis->mut); + pthread_cond_destroy (pThis->notFull); + free (pThis->notFull); + pthread_cond_destroy (pThis->notEmpty); + free (pThis->notEmpty); + /* type-specific destructor */ + free(pThis->tVars.farray.pBuf); + free (pThis); + + return iRet; } @@ -96,31 +118,37 @@ void queueDelete (msgQueue *q) * NOTE: this comment does not really apply - the callers handle the mutex, so it * *is* guarded. */ -void queueAdd (msgQueue *q, void* in) +rsRetVal queueAdd(queue_t *pThis, void* in) { - q->pbuf[q->tail] = in; - q->tail++; - if (q->tail == iMainMsgQueueSize) - q->tail = 0; - if (q->tail == q->head) - q->full = 1; - q->empty = 0; - - return; + DEFiRet; + +dbgprintf("queueAdd\n"); + pThis->tVars.farray.pBuf[pThis->tVars.farray.tail] = in; + pThis->tVars.farray.tail++; + if (pThis->tVars.farray.tail == pThis->iMaxQueueSize) + pThis->tVars.farray.tail = 0; + if (pThis->tVars.farray.tail == pThis->tVars.farray.head) + pThis->full = 1; + pThis->empty = 0; + + return iRet; } -void queueDel(msgQueue *q, void **out) +rsRetVal queueDel(queue_t *pThis, void **out) { - *out = (void*) q->pbuf[q->head]; + DEFiRet; + +dbgprintf("queueDel\n"); + *out = (void*) pThis->tVars.farray.pBuf[pThis->tVars.farray.head]; - q->head++; - if (q->head == iMainMsgQueueSize) - q->head = 0; - if (q->head == q->tail) - q->empty = 1; - q->full = 0; + pThis->tVars.farray.head++; + if (pThis->tVars.farray.head == pThis->iMaxQueueSize) + pThis->tVars.farray.head = 0; + if (pThis->tVars.farray.head == pThis->tVars.farray.tail) + pThis->empty = 1; + pThis->full = 0; - return; + return iRet; } /* @@ -28,13 +28,12 @@ typedef enum { QUEUETYPE_FIXED_ARRAY, /* a simple queue made out of a fixed (initially malloced) array fast but memoryhog */ QUEUETYPE_LINKEDLIST, /* linked list used as buffer, lower fixed memory overhead but slower */ -} queueTypes_t; +} queueType_t; /* the queue object */ typedef struct { - queueTypes_t qType; - int iMaxQueSize; /* how large can the queue grow? */ - void** pUsr; /* the queued user data structure */ + queueType_t qType; + int iMaxQueueSize; /* how large can the queue grow? */ /* synchronization variables */ pthread_mutex_t *mut; pthread_cond_t *notFull, *notEmpty; @@ -43,29 +42,16 @@ typedef struct { union { /* different data elements based on queue type (qType) */ struct { long head, tail; + void** pBuf; /* the queued user data structure */ } farray; } tVars; } queue_t; -/* this is the first approach to a queue, this time with static - * memory. - */ -typedef struct { - void** pbuf; - long head, tail; - int full, empty; - pthread_mutex_t *mut; - pthread_cond_t *notFull, *notEmpty; -} msgQueue; /* prototypes */ -msgQueue *queueInit (void); -void queueDelete (msgQueue *q); -void queueAdd (msgQueue *q, void* in); -void queueDel (msgQueue *q, void **out); - -/* go-away's */ -extern int iMainMsgQueueSize; -extern msgQueue *pMsgQueue; +rsRetVal queueConstruct(queue_t **ppThis, queueType_t qType, int iMaxQueueSize); +rsRetVal queueDestruct(queue_t *pThis); +rsRetVal queueAdd(queue_t *pThis, void* in); +rsRetVal queueDel(queue_t *pThis, void **out); #endif /* #ifndef QUEUE_H_INCLUDED */ @@ -447,6 +447,9 @@ static int Initialized = 0; /* set when we have initialized ourselves extern int errno; +queue_t *pMsgQueue = NULL; /* the main message queue */ +int iMainMsgQueueSize; /* size of the main message queue above */ + /* This structure represents the files that will have log * copies printed. @@ -1930,7 +1933,7 @@ static void startWorker(void) static void * singleWorker() { - msgQueue *fifo = pMsgQueue; + queue_t *fifo = pMsgQueue; msg_t *pMsg; sigset_t sigSet; @@ -1980,7 +1983,7 @@ singleWorker() static void enqueueMsg(msg_t *pMsg) { int iRet; - msgQueue *fifo = pMsgQueue; + queue_t *fifo = pMsgQueue; struct timespec t; assert(pMsg != NULL); @@ -2767,7 +2770,7 @@ die(int sig) freeSelectors(); /* Worker threads are stopped by freeSelectors() */ - queueDelete(pMsgQueue); /* delete fifo here! */ + queueDestruct(pMsgQueue); /* delete fifo here! */ pMsgQueue = NULL; /* rger 2005-02-22 @@ -3459,7 +3462,7 @@ init(void) if(pMsgQueue != NULL) { dbgprintf("deleting message queue\n"); - queueDelete(pMsgQueue); /* delete fifo here! */ + queueDestruct(pMsgQueue); /* delete fifo here! */ pMsgQueue = NULL; } @@ -3507,10 +3510,9 @@ init(void) } /* create message queue */ - pMsgQueue = queueInit(); - if(pMsgQueue == NULL) { - errno = 0; /* no queue is fatal, we need to give up in that case... */ - fprintf(stderr, "fatal error: could not create message queue - rsyslogd can not run!\n"); + CHKiRet_Hdlr(queueConstruct(&pMsgQueue, QUEUETYPE_FIXED_ARRAY, iMainMsgQueueSize)) { + /* no queue is fatal, we need to give up in that case... */ + fprintf(stderr, "fatal error %d: could not create message queue - rsyslogd can not run!\n", iRet); exit(1); } |