Debian Codegraph @

runtime/lib_ksi_queue.c

raw
1 #define _POSIX_C_SOURCE 199309L
2
3 #include <malloc.h>
4 #include <time.h>
5 #include <errno.h>
6 #include "lib_ksi_queue.h"
7
8 RingBuffer* RingBuffer_new(size_t size) {
9 RingBuffer* p = calloc(1, sizeof(RingBuffer));
10 if (!p) return NULL;
11
12 p->buffer = calloc(size, sizeof(void*));
13 p->size = size;
14 return p;
15 }
16
17 void RingBuffer_free(RingBuffer* this) {
18 if (this->buffer != NULL) free(this->buffer);
19 free(this);
20 }
21
22 static bool RingBuffer_grow(RingBuffer* this) {
23 void** pTmp = calloc(this->size * RB_GROW_FACTOR, sizeof(void*));
24 void* pTmpItem = NULL;
25 if (!pTmp) return false;
26
27 for (size_t i = 0; i < this->size; ++i) {
28 RingBuffer_popFront(this, &pTmpItem);
29 pTmp[i] = pTmpItem;
30 }
31
32 free(this->buffer);
33 this->buffer = pTmp;
34 this->head = 0;
35 this->tail = this->size;
36 this->count = this->size;
37 this->size = this->size * RB_GROW_FACTOR;
38 return true;
39 }
40
41 bool RingBuffer_pushBack(RingBuffer* this, void* item) {
42 if (this->size == this->count && !RingBuffer_grow(this)) return false;
43
44 if (this->size == 0) return false;
45
46 this->buffer[this->tail] = item;
47 this->tail = (this->tail + 1) % this->size;
48 this->count += 1;
49 return true;
50 }
51
52 bool RingBuffer_popFront(RingBuffer* this, void** item) {
53 if (this->count == 0) return false;
54
55 *item = this->buffer[this->head];
56 this->buffer[this->head] = NULL;
57 this->count -= 1;
58 this->head = (this->head + 1) % this->size;
59 return true;
60 }
61
62 bool RingBuffer_peekFront(RingBuffer* this, void** item) {
63 if (this->count == 0) return false;
64
65 *item = this->buffer[this->head];
66 return true;
67 }
68
69 size_t RingBuffer_count(RingBuffer* this) {
70 return this->count;
71 }
72
73 bool RingBuffer_getItem(RingBuffer* this, size_t index, void** item) {
74 if (this->count == 0 || index >= this->count) return false;
75
76 *item = this->buffer[(this->head + index) % this->size];
77 return true;
78 }
79
80
81 ProtectedQueue* ProtectedQueue_new(size_t queueSize) {
82 ProtectedQueue* p = calloc(1, sizeof(ProtectedQueue));
83 if (!p) return NULL;
84
85 pthread_mutex_init(&p->mutex, 0);
86 p->bStop = false;
87 p->workItems = RingBuffer_new(queueSize);
88 return p;
89 }
90
91 void ProtectedQueue_free(ProtectedQueue* this) {
92 pthread_mutex_destroy(&this->mutex);
93 pthread_cond_destroy(&this->condition);
94 this->bStop = true;
95 RingBuffer_free(this->workItems);
96 free(this);
97 }
98
99 /// Signal stop. All threads waiting in FetchItme will be returned false from FetchItem
100
101 void ProtectedQueue_stop(ProtectedQueue* this) {
102 this->bStop = true;
103 pthread_cond_broadcast(&this->condition);
104 }
105
106 /// Atomically adds an item into work item queue and releases a thread waiting
107 /// in FetchItem
108
109 bool ProtectedQueue_addItem(ProtectedQueue* this, void* item) {
110 bool ret = false;
111
112 if (this->bStop) return false;
113
114 pthread_mutex_lock(&this->mutex);
115 if ((ret = RingBuffer_pushBack(this->workItems, item)) == true) pthread_cond_signal(&this->condition);
116 pthread_mutex_unlock(&this->mutex);
117 return ret;
118 }
119
120 bool ProtectedQueue_peekFront(ProtectedQueue* this, void** item) {
121 bool ret;
122 pthread_mutex_lock(&this->mutex);
123 ret = RingBuffer_peekFront(this->workItems, item);
124 pthread_mutex_unlock(&this->mutex);
125 return ret;
126 }
127
128 bool ProtectedQueue_popFront(ProtectedQueue* this, void** item) {
129 bool ret;
130 pthread_mutex_lock(&this->mutex);
131 ret = RingBuffer_popFront(this->workItems, item);
132 pthread_mutex_unlock(&this->mutex);
133 return ret;
134 }
135
136 size_t ProtectedQueue_popFrontBatch(ProtectedQueue* this, void** items, size_t bufSize) {
137 size_t i;
138 pthread_mutex_lock(&this->mutex);
139 for (i = 0; RingBuffer_count(this->workItems) > 0 && i < bufSize; ++i)
140 RingBuffer_popFront(this->workItems, items[i]);
141 pthread_mutex_unlock(&this->mutex);
142 return i;
143 }
144
145 bool ProtectedQueue_getItem(ProtectedQueue* this, size_t index, void** item) {
146 bool ret = false;
147 pthread_mutex_lock(&this->mutex);
148 ret = RingBuffer_getItem(this->workItems, index, item);
149 pthread_mutex_unlock(&this->mutex);
150 return ret;
151 }
152
153 /* Waits for a new work item or timeout (if specified). Returns 0 in case of exit
154 * condition, 1 if item became available and ETIMEDOUT in case of timeout. */
155 int ProtectedQueue_waitForItem(ProtectedQueue* this, void** item, uint64_t timeout) {
156 struct timespec ts;
157 pthread_mutex_lock(&this->mutex);
158
159 if (timeout > 0) {
160 clock_gettime(CLOCK_REALTIME, &ts);
161 ts.tv_sec += timeout / 1000LL;
162 ts.tv_nsec += (timeout % 1000LL) * 1000LL;
163 }
164
165 if (timeout) {
166 if (pthread_cond_timedwait(&this->condition, &this->mutex, &ts) == ETIMEDOUT) {
167 pthread_mutex_unlock(&this->mutex);
168 return ETIMEDOUT;
169 }
170 } else
171 pthread_cond_wait(&this->condition, &this->mutex);
172 if (this->bStop) {
173 pthread_mutex_unlock(&this->mutex);
174 return 0;
175 }
176
177 if (RingBuffer_count(this->workItems) != 0 && item != NULL) RingBuffer_popFront(this->workItems, item);
178
179 pthread_mutex_unlock(&this->mutex);
180
181 return 1;
182 }
183
184 size_t ProtectedQueue_count(ProtectedQueue* this) {
185 size_t nCount;
186 pthread_mutex_lock(&this->mutex);
187 nCount = RingBuffer_count(this->workItems);
188 pthread_mutex_unlock(&this->mutex);
189 return nCount;
190 }
191
192 void* worker_thread_main(void* arg) {
193 int res;
194 void* item;
195 WorkerThreadContext* tc = (WorkerThreadContext*)arg;
196
197 while (1) {
198 item = NULL;
199 res = ProtectedQueue_waitForItem(tc->queue, &item, tc->timeout);
200 if (tc->queue->bStop) return NULL;
201
202 if (res == ETIMEDOUT) {
203 if (!tc->timeoutFunc()) return NULL;
204 } else if (item != NULL && !tc->workerFunc(item))
205 return NULL;
206 }
207 }