File: threaded_queue.h

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (164 lines) | stat: -rw-r--r-- 6,064 bytes parent folder | download | duplicates (2)
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
/*
  Copyright 2024 Northern.tech AS

  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  To the extent this program is licensed as part of the Enterprise
  versions of CFEngine, the applicable Commercial Open Source License
  (COSL) may apply to this file if you as a licensee so wish it. See
  included file COSL.txt.
*/

#ifndef CFENGINE_THREADED_QUEUE_H
#define CFENGINE_THREADED_QUEUE_H

#include <stdbool.h>

typedef struct ThreadedQueue_ ThreadedQueue;

/**
  @brief Creates a new thread safe queue with specified capacity.
  @param [in] initial_capacity Initial capacity, defaults to 1.
  @param [in] ItemDestroy Function used to destroy data elements.
  */
ThreadedQueue *ThreadedQueueNew(size_t initial_capacity,
                                void (ItemDestroy) (void *item));

/**
  @brief Destroys the queue and frees the memory it occupies.
  @warning ThreadedQueue should only be destroyed if all threads are joined
  @param [in] queue The queue to destroy.
  */
void ThreadedQueueDestroy(ThreadedQueue *queue);

/**
  @brief Frees the memory allocated for the data pointer and the struct itself.
  @param [in] queue The queue to free.
  */
void ThreadedQueueSoftDestroy(ThreadedQueue *queue);

/**
  @brief Returns and removes the first element of the queue.
  @note If queue is empty, blocks for `timeout` seconds or until signalled.
        If THREAD_WAIT_INDEFINITELY is specified, waits forever until signal
        is given. If it times out, it returns false.
  @param [in] queue The queue to pop from.
  @param [out] item The item at the first poisition in the queue.
  @param [in] timeout Timeout for blocking in seconds.
  @return true on success, false if timed out or queue was empty.
  */
bool ThreadedQueuePop(ThreadedQueue *queue, void **item, int timeout);

/**
  @brief Pops num elements from the queue into data_array, returns amount.
  @note If queue is empty, blocks for `timeout` seconds or until signalled.
        If THREAD_WAIT_INDEFINITELY is specified, waits forever until
        signalled. If it times out, it returns 0 and sets *data_array to NULL.
  @warning The pointer array will have to be freed manually.
  @param [in] queue The queue to pop from.
  @param [out] data_array Pointer to location to put popped elements.
  @param [in] timeout Timeout for blocking in seconds.
  @return Amount of elements popped.
  */
size_t ThreadedQueuePopN(ThreadedQueue *queue,
                         void ***data_array,
                         size_t num,
                         int timeout);

/**
 * @brief Same as ThreadedQueuePopN() above, but pops into a given array.
 * @warning The caller is responsible for making sure that up to #num items fit
 *          into the given array.
 * @see ThreadedQueuePopN()
 */
size_t ThreadedQueuePopNIntoArray(ThreadedQueue *queue,
                                  void **data_array,
                                  size_t num,
                                  int timeout);

/**
  @brief Pushes a new item on top of the queue, returns current size.
  @param [in] queue The queue to push to.
  @param [in] item The item to push.
  @return Current amount of elements in the queue.
  */
size_t ThreadedQueuePush(ThreadedQueue *queue, void *item);

/**
  @brief Pushes new items on top of the queue, returns current size.
  @param [in] queue The queue to push to.
  @param [in] items The items to push.
  @param [in] n_items Number of items from #items to push.
  @return Current amount of elements in the queue.
  */
size_t ThreadedQueuePushN(ThreadedQueue *queue, void **items, size_t n_items);

/**
  @brief Get current number of items in queue.
  @note On NULL queue, returns 0.
  @param [in] queue The queue.
  @return The amount of elements in the queue.
  */
size_t ThreadedQueueCount(ThreadedQueue const *queue);

/**
  @brief Get current capacity of queue.
  @note On NULL queue, returns 0.
  @param [in] queue The queue.
  @return The current capacity of the queue.
  */
size_t ThreadedQueueCapacity(ThreadedQueue const *queue);

/**
  @brief Checks if a queue is empty.
  @param [in] queue The queue.
  @return Returns true if queue is empty, false otherwise.
  */
bool ThreadedQueueIsEmpty(ThreadedQueue const *queue);

/**
  @brief Waits until queue is empty.
  @note Useful for situations where you want to wait before populating the
        queue. Timeout can be set to THREAD_BLOCK_INDEFINITELY to wait
        forever. Otherwise waits the amount of seconds specified.
  @param [in] queue The queue.
  @param [in] timeout Amount of seconds to wait before timing out.
  @return True if it successfully waited, false if it timed out.
  */
bool ThreadedQueueWaitEmpty(ThreadedQueue const *queue, int timeout);

/**
  @brief Create a shallow copy of a given queue.
  @note This makes a new queue pointing to the same memory as the old queue.
  @note Is only thread safe if original queue was also thread safe.
  @param [in] queue The queue.
  @return A new queue pointing to the same data.
  */
ThreadedQueue *ThreadedQueueCopy(ThreadedQueue *queue);

/**
 * @brief Clear (empty) the queue.
 */
void ThreadedQueueClear(ThreadedQueue *queue);

/**
 * @brief Clear (empty) the queue and push one item into it.
 * @note This is an atomic version of ThreadedQueueClear(); ThreadedQueuePush()
 *       useful for replacing contents of the queue with a new single item.
 * @return Current amount of elements in the queue.
 */
size_t ThreadedQueueClearAndPush(ThreadedQueue *queue, void *item);

#endif