2
* Copyright 2007 Red Hat Inc., Durham, North Carolina.
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
* Steve Grubb <sgrubb@redhat.com>
29
static volatile event_t **q;
30
static pthread_mutex_t queue_lock;
31
static pthread_cond_t queue_nonempty;
32
static unsigned int q_next, q_last, q_depth;
35
int init_queue(unsigned size)
42
q = malloc(q_depth * sizeof(event_t *));
46
for (i=0; i<q_depth; i++)
49
/* Setup IPC mechanisms */
50
pthread_mutex_init(&queue_lock, NULL);
51
pthread_cond_init(&queue_nonempty, NULL);
56
void enqueue(event_t *e)
58
unsigned int n, retry_cnt = 0;
61
// We allow 1 retry and then its over
63
syslog(LOG_ERR, "queue is full - dropping event");
66
pthread_mutex_lock(&queue_lock);
68
// OK, have lock add event
72
q_next = (n+1) % q_depth;
73
pthread_mutex_unlock(&queue_lock);
74
pthread_cond_signal(&queue_nonempty);
76
pthread_mutex_unlock(&queue_lock);
77
pthread_yield(); // Let dequeue thread run to clear queue
83
event_t *dequeue(void)
88
// Wait until its got something in it
89
pthread_mutex_lock(&queue_lock);
92
pthread_cond_wait(&queue_nonempty, &queue_lock);
96
// OK, grab the next event
100
q_last = (n+1) % q_depth;
104
pthread_mutex_unlock(&queue_lock);
110
void nudge_queue(void)
112
pthread_cond_signal(&queue_nonempty);
115
void destroy_queue(void)
119
for (i=0; i<q_depth; i++)