1 /*
2 * SysDB - src/utils/channel.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "utils/channel.h"
30 #include <assert.h>
32 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include <time.h>
39 #include <pthread.h>
41 /*
42 * private data types
43 */
45 struct sdb_channel {
46 pthread_mutex_t lock;
48 /* signaling for select() operation */
49 pthread_cond_t cond;
51 /* maybe TODO: add support for 'nil' values using a boolean area */
53 void *data;
54 size_t data_len;
55 size_t elem_size;
57 size_t head;
58 size_t tail;
59 _Bool full;
60 };
62 /*
63 * private helper functions
64 */
66 #define NEXT_WRITE(chan) (((chan)->tail + 1) % (chan)->data_len)
67 #define NEXT_READ(chan) (((chan)->head + 1) % (chan)->data_len)
69 #define ELEM(chan, i) \
70 (void *)((char *)(chan)->data + (i) * (chan)->elem_size)
71 #define TAIL(chan) ELEM(chan, (chan)->tail)
72 #define HEAD(chan) ELEM(chan, (chan)->head)
74 /* Insert a new element at the end; chan->lock must be held.
75 * Returns 0 if data has been written or if data may be written
76 * if 'data' is NULL. */
77 static int
78 channel_write(sdb_channel_t *chan, const void *data)
79 {
80 assert(chan);
82 if (chan->full)
83 return -1;
84 else if (! data)
85 return 0;
87 memcpy(TAIL(chan), data, chan->elem_size);
88 chan->tail = NEXT_WRITE(chan);
90 chan->full = chan->tail == chan->head;
91 pthread_cond_broadcast(&chan->cond);
92 return 0;
93 } /* channel_write */
95 /* Retrieve the first element; chan->lock must be held.
96 * Returns 0 if data has been read or if data is available
97 * if 'data' is NULL. */
98 static int
99 channel_read(sdb_channel_t *chan, void *data)
100 {
101 assert(chan);
103 if ((chan->head == chan->tail) && (! chan->full))
104 return -1;
105 else if (! data)
106 return 0;
108 memcpy(data, HEAD(chan), chan->elem_size);
109 chan->head = NEXT_READ(chan);
111 chan->full = 0;
112 pthread_cond_broadcast(&chan->cond);
113 return 0;
114 } /* channel_read */
116 /*
117 * public API
118 */
120 sdb_channel_t *
121 sdb_channel_create(size_t size, size_t elem_size)
122 {
123 sdb_channel_t *chan;
125 if (! elem_size)
126 return NULL;
127 if (! size)
128 size = 1;
130 chan = calloc(1, sizeof(*chan));
131 if (! chan)
132 return NULL;
134 chan->data = calloc(size, elem_size);
135 if (! chan->data) {
136 sdb_channel_destroy(chan);
137 return NULL;
138 }
140 chan->data_len = size;
141 chan->elem_size = elem_size;
143 pthread_mutex_init(&chan->lock, /* attr = */ NULL);
144 pthread_cond_init(&chan->cond, /* attr = */ NULL);
146 chan->head = chan->tail = 0;
147 return chan;
148 } /* sdb_channel_create */
150 void
151 sdb_channel_destroy(sdb_channel_t *chan)
152 {
153 if (! chan)
154 return;
156 pthread_mutex_lock(&chan->lock);
157 free(chan->data);
158 chan->data = NULL;
159 chan->data_len = 0;
161 pthread_cond_destroy(&chan->cond);
163 pthread_mutex_unlock(&chan->lock);
164 pthread_mutex_destroy(&chan->lock);
165 free(chan);
166 } /* sdb_channel_destroy */
168 int
169 sdb_channel_select(sdb_channel_t *chan, int *wantread, void *read_data,
170 int *wantwrite, void *write_data, const struct timespec *timeout)
171 {
172 int status = 0;
174 if (! chan) {
175 errno = EINVAL;
176 return -1;
177 }
179 if ((! wantread) && (! read_data) && (! wantwrite) && (! write_data)) {
180 errno = EINVAL;
181 return -1;
182 }
184 pthread_mutex_lock(&chan->lock);
185 while (! status) {
186 int read_status, write_status;
188 read_status = channel_read(chan, read_data);
189 write_status = channel_write(chan, write_data);
191 if ((! read_status) || (! write_status)) {
192 if (wantread)
193 *wantread = read_status == 0;
194 if (wantwrite)
195 *wantwrite = write_status == 0;
197 if (((wantread || read_data) && (! read_status))
198 || ((wantwrite || write_data) && (! write_status)))
199 break;
200 }
202 if (timeout) {
203 struct timespec abstime;
205 if (clock_gettime(CLOCK_REALTIME, &abstime)) {
206 pthread_mutex_unlock(&chan->lock);
207 return -1;
208 }
210 abstime.tv_sec += timeout->tv_sec;
211 abstime.tv_nsec += timeout->tv_nsec;
213 status = pthread_cond_timedwait(&chan->cond, &chan->lock,
214 &abstime);
215 }
216 else
217 status = pthread_cond_wait(&chan->cond, &chan->lock);
218 }
219 pthread_mutex_unlock(&chan->lock);
221 if (status) {
222 errno = status;
223 return -1;
224 }
225 return 0;
226 } /* sdb_channel_select */
228 int
229 sdb_channel_write(sdb_channel_t *chan, const void *data)
230 {
231 int status;
233 if ((! chan) || (! data))
234 return -1;
236 pthread_mutex_lock(&chan->lock);
237 status = channel_write(chan, data);
238 pthread_mutex_unlock(&chan->lock);
239 return status;
240 } /* sdb_channel_write */
242 int
243 sdb_channel_read(sdb_channel_t *chan, void *data)
244 {
245 int status;
247 if ((! chan) || (! data))
248 return -1;
250 pthread_mutex_lock(&chan->lock);
251 status = channel_read(chan, data);
252 pthread_mutex_unlock(&chan->lock);
253 return status;
254 } /* sdb_channel_read */
256 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */