1 /***************************************************************************
2 * Copyright (C) 2007 International Business Machines Corp. *
3 * All Rights Reserved. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program 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 *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 * Klaus Heinrich Kiwi <klausk@br.ibm.com> *
22 * based on code by Steve Grubb <sgrubb@redhat.com> *
23 ***************************************************************************/
25 #include "zos-remote-queue.h"
30 #include "zos-remote-log.h"
32 static volatile BerElement **q;
33 static pthread_mutex_t queue_lock;
34 static pthread_cond_t queue_nonempty;
35 static unsigned int q_next, q_last, q_depth;
38 int init_queue(unsigned int size)
45 q = malloc(q_depth * sizeof(BerElement *));
49 for (i=0; i<q_depth; i++)
52 /* Setup IPC mechanisms */
53 pthread_mutex_init(&queue_lock, NULL);
54 pthread_cond_init(&queue_nonempty, NULL);
59 void enqueue(BerElement *ber)
61 unsigned int n, retry_cnt = 0;
64 /* We allow 3 retries and then its over */
66 log_err("queue is full - dropping event");
69 pthread_mutex_lock(&queue_lock);
71 /* OK, have lock add event */
75 q_next = (n+1) % q_depth;
76 pthread_cond_signal(&queue_nonempty);
77 pthread_mutex_unlock(&queue_lock);
79 pthread_mutex_unlock(&queue_lock);
80 pthread_yield(); /* Let dequeue thread run to clear queue */
86 BerElement *dequeue(void)
91 /* Wait until its got something in it */
92 pthread_mutex_lock(&queue_lock);
95 pthread_cond_wait(&queue_nonempty, &queue_lock);
99 /* OK, grab the next event */
101 ber = (BerElement *) q[n];
103 q_last = (n+1) % q_depth;
107 pthread_mutex_unlock(&queue_lock);
109 /* Process the event */
113 void nudge_queue(void)
115 pthread_cond_signal(&queue_nonempty);
118 void increase_queue_depth(unsigned int size)
120 pthread_mutex_lock(&queue_lock);
121 if (size > q_depth) {
125 tmp_q = realloc(q, size * sizeof(BerElement *));
127 for (i=q_depth; i<size; i++)
131 pthread_mutex_unlock(&queue_lock);
134 void destroy_queue(void)
138 for (i=0; i<q_depth; i++) {