File: pcqueue.c

package info (click to toggle)
libpulp 0.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,976 kB
  • sloc: ansic: 11,792; python: 1,216; sh: 881; makefile: 871; cpp: 582; asm: 387
file content (229 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "pcqueue.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/** @brief Initialize the producer_consumer object.
 *
 * This function is for internal use only.  Call `producer_consumer_new` if
 * you want to create a producer_consumer object.
 *
 * @param queue   The queue to initialize
 * @param n       Maximum number of objects in this queue.
 * @return        0 if success, anything else if error.
 */
static int
producer_consumer_init(producer_consumer_t *queue, int n)
{
  int ret;
  memset(queue, 0, sizeof(*queue));

  queue->n = n;
#ifdef MORE_THAN_ONE_ONE
  ret = pthread_mutex_init(&queue->lock, NULL);
  if (ret != 0) {
    return ret;
  }
#endif
  ret = sem_init(&queue->empty, 0, 0);
  if (ret != 0) {
    return errno;
  }

  ret = sem_init(&queue->full, 0, n);
  if (ret != 0) {
    return errno;
  }

  return 0;
}

/** @brief Create a new producer_consumer object.
 *
 * This function allocates memory and initializes a producer_consumer object.
 * Call `producer_consumer_delete` if you wish to deinitialize and free all
 * the resources allocated there.
 *
 * @param n       Maximum number of objects in this queue.
 *
 * @return        Pointer to the created producer_consumer object.
 */
producer_consumer_t *
producer_consumer_new(int n)
{
  producer_consumer_t *q;
  size_t size = sizeof(producer_consumer_t) + n * sizeof(void *);

  q = (producer_consumer_t *)malloc(size);

  if (q == NULL)
    return NULL;

  if (producer_consumer_init(q, n)) {
    free(q);
    q = NULL;
  }
  return q;
}

/** @brief Initialize the producer_consumer object.
 *
 * This function is for internal use only.  Call `producer_consumer_delete` if
 * you want to release a producer_consumer object.
 *
 * @param queue   The queue to destroy.
 *
 * @return        0 if success, anything else if error.
 */
static int
producer_consumer_destroy(producer_consumer_t *queue)
{
  int ret;

#ifdef MORE_THAN_ONE_ONE
  ret = pthread_mutex_destroy(&queue->lock);
  if (ret != 0) {
    return ret;
  }
#endif

  ret = sem_destroy(&queue->full);
  if (ret != 0) {
    return errno;
  }

  ret = sem_destroy(&queue->empty);
  if (ret != 0) {
    return errno;
  }

  memset(queue, 0, sizeof(*queue));
  return 0;
}

/** @brief Deinitialize and release all resources of `queue`;
 *
 * Call this function if you wish to release all resources allocated to a
 * producer_consumer object.
 *
 * @param queue   The queue to destroy.
 *
 * @return        0 if success, anything else if error.
 */
int
producer_consumer_delete(producer_consumer_t *queue)
{
  int ret = 0;
  if (queue != NULL) {
    ret = producer_consumer_destroy(queue);
    free(queue);
  }

  return ret;
}

/** @brief Enqueue an `elem` to the queue.
 *
 * Call this function if you wish to enqueue the object `elem` to the queue.
 *
 * @param queue   The queue to insert.
 * @param elem    The element to insert.
 *
 * @return        0 if success, anything else if error.
 */
int
producer_consumer_enqueue(producer_consumer_t *queue, void *elem)
{
  int ret;
  /* Block if the queue is full.  */
  ret = sem_wait(&queue->full);
  if (ret != 0) {
    return errno;
  }

#ifdef MORE_THAN_ONE_ONE
  /* Acquire lock of queue.  */
  ret = pthread_mutex_lock(&queue->lock);
  if (ret != 0) {
    return ret;
  }
#endif
  /* ----------------------------- */

  queue->elem[queue->head++] = elem;

  /* Wraps around if end of buffer.  */
  if (queue->head >= queue->n) {
    queue->head = 0;
  }

  /* ----------------------------- */

#ifdef MORE_THAN_ONE_ONE
  /* Release lock of queue.  */
  ret = pthread_mutex_unlock(&queue->lock);
  if (ret != 0) {
    return ret;
  }
#endif

  /* Alert other threads that we inserted something.  */
  ret = sem_post(&queue->empty);
  if (ret != 0) {
    return errno;
  }

  return 0;
}

/** @brief Dequeue an element from producer_consumer `queue`.
 *
 * Call this function if you wish to dequeue an element from the queue.
 *
 * @param queue   The queue to get an element from.
 * @return        The dequeued element.
 */
void *
producer_consumer_dequeue(producer_consumer_t *queue)
{
  void *ret;

  /* Block if the queue is empty.  */
  if (sem_wait(&queue->empty) != 0) {
    return NULL;
  }

#ifdef MORE_THAN_ONE_ONE
  /* Acquire lock of queue.  */
  if (pthread_mutex_lock(&queue->lock) != 0) {
    return NULL;
  }
#endif

  /* ----------------------------- */

  ret = queue->elem[queue->tail++];

  /* Wraps around if end of buffer.  */
  if (queue->tail >= queue->n) {
    queue->tail = 0;
  }

  /* ----------------------------- */

#ifdef MORE_THAN_ONE_ONE
  /* Release lock of queue.  */
  if (pthread_mutex_unlock(&queue->lock) != 0) {
    return NULL;
  }
#endif

  /* Alert other threads that we inserted something.  */
  if (sem_post(&queue->full) != 0) {
    return NULL;
  }

  return ret;
}