-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtqueue.h
More file actions
89 lines (79 loc) · 2.85 KB
/
Copy pathtqueue.h
File metadata and controls
89 lines (79 loc) · 2.85 KB
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
#ifndef TQUEUE_H
#define TQUEUE_H
#include "args.h"
#include "utils.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Initializes a bounded, thread-safe batch queue.
*
* Allocates storage for up to `capacity` batches and initializes the mutex and
* condition variables used by producers and consumers. The queue is zeroed
* before initialization.
*
* Returns 0 on success, or -1 if `queue` is NULL, `capacity` is invalid, memory
* allocation fails, or synchronization primitive initialization fails.
*/
int fq_queue_init(fq_batch_queue_t *queue, int capacity);
/*
* Destroys a batch queue and releases all associated resources.
*
* Frees any unconsumed batch data still stored in the queue, destroys the
* condition variables and mutex, releases the queue storage, and resets the
* structure to zero.
*
* The queue must not be in active use by producer or consumer threads when this
* function is called.
*/
void fq_queue_destroy(fq_batch_queue_t *queue);
/*
* Pushes a batch onto the queue.
*
* Blocks while the queue is full, unless the queue has been closed. On success,
* stores `batch` at the tail of the queue and signals one waiting consumer.
*
* Returns 0 on success, or -1 if the queue is closed before the batch can be
* inserted. Ownership of `batch.data` transfers to the queue only on success.
*/
int fq_queue_push(fq_batch_queue_t *queue, fq_batch_t batch);
/*
* Pops a batch from the queue.
*
* Blocks while the queue is empty, unless the queue has been closed. On success,
* writes the removed batch to `out`, clears the queue slot, and signals one
* waiting producer.
*
* Returns 1 when a batch is returned, or 0 when the queue is closed and empty.
* The caller becomes responsible for freeing any data owned by the returned
* batch.
*/
int fq_queue_pop(fq_batch_queue_t *queue, fq_batch_t *out);
/*
* Closes the queue and wakes all waiting threads.
*
* After closing, future push operations fail. Waiting producers and consumers
* are woken so they can observe the closed state. Consumers may continue popping
* already queued batches until the queue becomes empty.
*/
void fq_queue_close(fq_batch_queue_t *queue);
/*
* Releases memory owned by a parallel result.
*
* Frees the result's core array and resets the result structure to zero. This
* function is safe to call with NULL.
*/
void fq_parallel_result_destroy(fq_parallel_result_t *result);
/*
* Ensures that a worker has enough capacity for additional cores.
*
* Grows `worker->cores` when the current capacity cannot hold `needed_extra`
* more entries beyond `worker->count`. Capacity grows from a minimum default
* and then increases by roughly 1.5x plus extra slack.
*
* Returns 0 on success, or -1 if capacity growth overflows or memory
* reallocation fails.
*/
int fq_worker_reserve(fq_worker_t *worker, uint64_t needed_extra);
#endif