| 1 |
#ifndef INCLUDED_LIBRSKSI_QUEUE_H |
| 2 |
#define INCLUDED_LIBRSKSI_QUEUE_H |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <pthread.h> |
| 7 |
|
| 8 |
#define RB_GROW_FACTOR 2 |
| 9 |
|
| 10 |
typedef struct RingBuffer_st { |
| 11 |
void** buffer; |
| 12 |
size_t size; |
| 13 |
size_t count; |
| 14 |
size_t head; |
| 15 |
size_t tail; |
| 16 |
} RingBuffer; |
| 17 |
|
| 18 |
RingBuffer* RingBuffer_new(size_t size); |
| 19 |
void RingBuffer_free(RingBuffer* this); |
| 20 |
bool RingBuffer_pushBack(RingBuffer* this, void* item); |
| 21 |
bool RingBuffer_popFront(RingBuffer* this, void** item); |
| 22 |
bool RingBuffer_peekFront(RingBuffer* this, void** item); |
| 23 |
bool RingBuffer_getItem(RingBuffer* this, size_t index, void** item); |
| 24 |
size_t RingBuffer_count(RingBuffer* this); |
| 25 |
|
| 26 |
typedef struct ProtectedQueue_st { |
| 27 |
bool bStop; |
| 28 |
RingBuffer* workItems; |
| 29 |
pthread_mutex_t mutex; |
| 30 |
pthread_cond_t condition; |
| 31 |
} ProtectedQueue; |
| 32 |
|
| 33 |
ProtectedQueue* ProtectedQueue_new(size_t queueSize); |
| 34 |
void ProtectedQueue_free(ProtectedQueue* this); |
| 35 |
void ProtectedQueue_stop(ProtectedQueue* this); |
| 36 |
bool ProtectedQueue_addItem(ProtectedQueue* this, void* item); |
| 37 |
bool ProtectedQueue_peekFront(ProtectedQueue* this, void** item); |
| 38 |
bool ProtectedQueue_popFront(ProtectedQueue* this, void** item); |
| 39 |
size_t ProtectedQueue_popFrontBatch(ProtectedQueue* this, void** items, size_t bufSize); |
| 40 |
int ProtectedQueue_waitForItem(ProtectedQueue* this, void** item, uint64_t timeout); |
| 41 |
size_t ProtectedQueue_count(ProtectedQueue* this); |
| 42 |
bool ProtectedQueue_getItem(ProtectedQueue* this, size_t index, void** item); |
| 43 |
|
| 44 |
typedef struct WorkerThreadContext_st { |
| 45 |
bool (*workerFunc)(void*); |
| 46 |
bool (*timeoutFunc)(void); |
| 47 |
ProtectedQueue* queue; |
| 48 |
unsigned timeout; |
| 49 |
} WorkerThreadContext; |
| 50 |
|
| 51 |
void* worker_thread_main(void* arg); |
| 52 |
|
| 53 |
#endif // INCLUDED_LIBRSKSI_QUEUE_H |