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
|
#define _POSIX_C_SOURCE 199309L
#include <malloc.h>
#include <time.h>
#include <errno.h>
#include "lib_ksi_queue.h"
RingBuffer* RingBuffer_new(size_t size) {
RingBuffer* p = calloc(1, sizeof(RingBuffer));
if (!p) return NULL;
p->buffer = calloc(size, sizeof(void*));
p->size = size;
return p;
}
void RingBuffer_free(RingBuffer* this) {
if (this->buffer != NULL) free(this->buffer);
free(this);
}
static bool RingBuffer_grow(RingBuffer* this) {
void** pTmp = calloc(this->size * RB_GROW_FACTOR, sizeof(void*));
void* pTmpItem = NULL;
if (!pTmp) return false;
for (size_t i = 0; i < this->size; ++i) {
RingBuffer_popFront(this, &pTmpItem);
pTmp[i] = pTmpItem;
}
free(this->buffer);
this->buffer = pTmp;
this->head = 0;
this->tail = this->size;
this->count = this->size;
this->size = this->size * RB_GROW_FACTOR;
return true;
}
bool RingBuffer_pushBack(RingBuffer* this, void* item) {
if (this->size == this->count && !RingBuffer_grow(this)) return false;
if (this->size == 0) return false;
this->buffer[this->tail] = item;
this->tail = (this->tail + 1) % this->size;
this->count += 1;
return true;
}
bool RingBuffer_popFront(RingBuffer* this, void** item) {
if (this->count == 0) return false;
*item = this->buffer[this->head];
this->buffer[this->head] = NULL;
this->count -= 1;
this->head = (this->head + 1) % this->size;
return true;
}
bool RingBuffer_peekFront(RingBuffer* this, void** item) {
if (this->count == 0) return false;
*item = this->buffer[this->head];
return true;
}
size_t RingBuffer_count(RingBuffer* this) {
return this->count;
}
bool RingBuffer_getItem(RingBuffer* this, size_t index, void** item) {
if (this->count == 0 || index >= this->count) return false;
*item = this->buffer[(this->head + index) % this->size];
return true;
}
ProtectedQueue* ProtectedQueue_new(size_t queueSize) {
ProtectedQueue* p = calloc(1, sizeof(ProtectedQueue));
if (!p) return NULL;
pthread_mutex_init(&p->mutex, 0);
p->bStop = false;
p->workItems = RingBuffer_new(queueSize);
return p;
}
void ProtectedQueue_free(ProtectedQueue* this) {
pthread_mutex_destroy(&this->mutex);
pthread_cond_destroy(&this->condition);
this->bStop = true;
RingBuffer_free(this->workItems);
free(this);
}
/// Signal stop. All threads waiting in FetchItme will be returned false from FetchItem
void ProtectedQueue_stop(ProtectedQueue* this) {
this->bStop = true;
pthread_cond_broadcast(&this->condition);
}
/// Atomically adds an item into work item queue and releases a thread waiting
/// in FetchItem
bool ProtectedQueue_addItem(ProtectedQueue* this, void* item) {
bool ret = false;
if (this->bStop) return false;
pthread_mutex_lock(&this->mutex);
if ((ret = RingBuffer_pushBack(this->workItems, item)) == true) pthread_cond_signal(&this->condition);
pthread_mutex_unlock(&this->mutex);
return ret;
}
bool ProtectedQueue_peekFront(ProtectedQueue* this, void** item) {
bool ret;
pthread_mutex_lock(&this->mutex);
ret = RingBuffer_peekFront(this->workItems, item);
pthread_mutex_unlock(&this->mutex);
return ret;
}
bool ProtectedQueue_popFront(ProtectedQueue* this, void** item) {
bool ret;
pthread_mutex_lock(&this->mutex);
ret = RingBuffer_popFront(this->workItems, item);
pthread_mutex_unlock(&this->mutex);
return ret;
}
size_t ProtectedQueue_popFrontBatch(ProtectedQueue* this, void** items, size_t bufSize) {
size_t i;
pthread_mutex_lock(&this->mutex);
for (i = 0; RingBuffer_count(this->workItems) > 0 && i < bufSize; ++i)
RingBuffer_popFront(this->workItems, items[i]);
pthread_mutex_unlock(&this->mutex);
return i;
}
bool ProtectedQueue_getItem(ProtectedQueue* this, size_t index, void** item) {
bool ret = false;
pthread_mutex_lock(&this->mutex);
ret = RingBuffer_getItem(this->workItems, index, item);
pthread_mutex_unlock(&this->mutex);
return ret;
}
/* Waits for a new work item or timeout (if specified). Returns 0 in case of exit
* condition, 1 if item became available and ETIMEDOUT in case of timeout. */
int ProtectedQueue_waitForItem(ProtectedQueue* this, void** item, uint64_t timeout) {
struct timespec ts;
pthread_mutex_lock(&this->mutex);
if (timeout > 0) {
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += timeout / 1000LL;
ts.tv_nsec += (timeout % 1000LL) * 1000LL;
}
if (timeout) {
if (pthread_cond_timedwait(&this->condition, &this->mutex, &ts) == ETIMEDOUT) {
pthread_mutex_unlock(&this->mutex);
return ETIMEDOUT;
}
} else
pthread_cond_wait(&this->condition, &this->mutex);
if (this->bStop) {
pthread_mutex_unlock(&this->mutex);
return 0;
}
if (RingBuffer_count(this->workItems) != 0 && item != NULL) RingBuffer_popFront(this->workItems, item);
pthread_mutex_unlock(&this->mutex);
return 1;
}
size_t ProtectedQueue_count(ProtectedQueue* this) {
size_t nCount;
pthread_mutex_lock(&this->mutex);
nCount = RingBuffer_count(this->workItems);
pthread_mutex_unlock(&this->mutex);
return nCount;
}
void* worker_thread_main(void* arg) {
int res;
void* item;
WorkerThreadContext* tc = (WorkerThreadContext*)arg;
while (1) {
item = NULL;
res = ProtectedQueue_waitForItem(tc->queue, &item, tc->timeout);
if (tc->queue->bStop) return NULL;
if (res == ETIMEDOUT) {
if (!tc->timeoutFunc()) return NULL;
} else if (item != NULL && !tc->workerFunc(item))
return NULL;
}
}
|