-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_queue.c
75 lines (64 loc) · 1.96 KB
/
event_queue.c
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
/*
* File name: event_queue.c
* Date: 2017/04/15 12:42
* Author: Jan Faigl
*/
#include <stdlib.h>
#include <pthread.h>
#include "event_queue.h"
#ifndef QUEUE_CAPACITY
#define QUEUE_CAPACITY 16 // queue for 16 messages
#endif
typedef struct {
event queue[QUEUE_CAPACITY]; // message queue
volatile int in; // pointer to the circular queue
volatile int out; // pointer to the circular queue
pthread_mutex_t mtx;
pthread_cond_t cond;
} queue;
queue q = { .in = 0, .out = 0 };
// - function -----------------------------------------------------------------
void queue_init(void)
{
pthread_mutex_init(&q.mtx, NULL);
pthread_cond_init(&q.cond, NULL);
}
// - function -----------------------------------------------------------------
void queue_cleanup(void)
{
while (q.in != q.out) {
event ev = queue_pop();
if (ev.type == EV_SERIAL && ev.data.msg) {
free(ev.data.msg);
}
}
}
// - function -----------------------------------------------------------------
event queue_pop(void)
{
event ret = { .source = EV_NUM, .type = EV_TYPE_NUM };
pthread_mutex_lock(&(q.mtx));
while (q.in == q.out) {
pthread_cond_wait(&(q.cond), &(q.mtx)); // wait for next event
}
if (q.in != q.out) {
ret = q.queue[q.out];
q.out = (q.out + 1) % QUEUE_CAPACITY;
pthread_cond_broadcast(&(q.cond)); // notify if some thread is waitting in push
}
pthread_mutex_unlock(&(q.mtx));
return ret;
}
// - function -----------------------------------------------------------------
void queue_push(event ev)
{
pthread_mutex_lock(&(q.mtx));
while (((q.in + 1) % QUEUE_CAPACITY) == q.out) { // queue is full wait for pop
pthread_cond_wait(&(q.cond), &(q.mtx)); // wait for some event is popped
}
q.queue[q.in] = ev;
q.in = (q.in + 1) % QUEUE_CAPACITY;
pthread_cond_broadcast(&(q.cond)); // notify if some thread is waitting on pop
pthread_mutex_unlock(&(q.mtx));
}
/* end of event_queue.c */