-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
53 lines (37 loc) · 1.19 KB
/
queue.h
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
#ifndef QUEUE_H
#define QUEUE_H
#include <pthread.h> /* pthread_* */
#include <semaphore.h> /* sem_* */
#include <stdlib.h> /* size_t */
#include "request.h"
typedef request_t queue_type_t;
typedef struct queue {
size_t buffer_size; /* max number elements buffer can hold */
size_t size; /* current queue size */
sem_t full; /* block adding when full */
sem_t empty; /* block consuming when empty */
pthread_mutex_t lock; /* lock the queue when modifying */
int in_idx, out_idx; /* indexes for last in and out */
queue_type_t *buffer; /* internal storage */
} queue_t;
void queue_put(queue_t *q, queue_type_t item);
queue_type_t queue_get(queue_t *q);
void queue_init(queue_t *q, size_t qsize);
void queue_destroy(queue_t *q);
static inline void queue_wait_if_empty(queue_t* q)
{
sem_wait(&q->empty);
}
static inline void queue_wait_if_full(queue_t *q)
{
sem_wait(&q->full);
}
static inline void queue_signal_available(queue_t *q)
{
sem_post(&q->empty);
}
static inline void queue_signal_consumed(queue_t *q)
{
sem_post(&q->full);
}
#endif /* QUEUE_H */