File: threaded_deque.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 (185 lines) | stat: -rw-r--r-- 6,807 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
  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_DEQUE_H
#define CFENGINE_THREADED_DEQUE_H

#include <stdbool.h>

typedef struct ThreadedDeque_ ThreadedDeque;

/**
  @brief Creates a new thread safe deque with specified capacity.
  @param [in] initial_capacity  Initial capacity, defaults to 1.
  @param [in] ItemDestroy       Function used to destroy data elements.

  @return A new ThreadedDeque on success, NULL if pthread_mutex_init or
          pthread_cond_init fails.
  */
ThreadedDeque *ThreadedDequeNew(size_t initial_capacity,
                                void (ItemDestroy) (void *item));

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

/**
  @brief Frees the memory allocated for the pointers and the struct.
  @param [in] deque  The deque to free.
  */
void ThreadedDequeSoftDestroy(ThreadedDeque *deque);

/**
  @brief Returns and removes the leftmost element of the deque.
  @note If deque 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]  deque    The deque to pop from.
  @param [out] item     The item at the first poisition in the deque.
  @param [in]  timeout  Timeout for blocking in seconds.

  @return true on success, false if timed out or deque was empty.
  */
bool ThreadedDequePopLeft(ThreadedDeque *deque,
                          void **item,
                          int timeout);

/**
  @brief Returns and removes the rightmost element of the deque.
  @note If deque 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]  deque    The deque to pop from.
  @param [out] item     The item at the first poisition in the deque.
  @param [in]  timeout  Timeout for blocking in seconds.

  @return true on success, false if timed out or deque was empty.
  */
bool ThreadedDequePopRight(ThreadedDeque *deque,
                           void **item,
                           int timeout);

/**
  @brief Pops num elements from the left in deque into data_array.
  @note If deque 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]  deque       The deque 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 ThreadedDequePopLeftN(ThreadedDeque *deque,
                             void ***data_array,
                             size_t num,
                             int timeout);

/**
  @brief Pops num elements from the right in deque into data_array.
  @note If deque 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]  deque       The deque 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 ThreadedDequePopRightN(ThreadedDeque *deque,
                              void ***data_array,
                              size_t num,
                              int timeout);

/**
  @brief Pushes item to left end of the deque, returns current size.
  @param [in] deque    The deque to push to.
  @param [in] item     The item to push.

  @return Current amount of elements in the deque.
  */
size_t ThreadedDequePushLeft(ThreadedDeque *deque, void *item);

/**
  @brief Pushes item to right end of the deque, returns current size.
  @param [in] deque    The deque to push to.
  @param [in] item     The item to push.

  @return Current amount of elements in the deque.
  */
size_t ThreadedDequePushRight(ThreadedDeque *deque, void *item);

/**
  @brief Get current number of items in deque.
  @note On NULL deque, returns 0.
  @param [in] deque  The deque.

  @return The amount of elements in the deque.
  */
size_t ThreadedDequeCount(ThreadedDeque const *deque);

/**
  @brief Get current capacity of deque.
  @note On NULL deque, returns 0.
  @param [in] deque  The deque.

  @return The current capacity of the deque.
  */
size_t ThreadedDequeCapacity(ThreadedDeque const *deque);

/**
  @brief Checks if a deque is empty.
  @param [in] deque  The deque.

  @return Returns true if deque is empty, false otherwise.
  */
bool ThreadedDequeIsEmpty(ThreadedDeque const *deque);

/**
  @brief Waits until deque is empty.
  @note Useful for situations where you want to wait before populating the
        deque. Timeout can be set to THREAD_BLOCK_INDEFINITELY to wait
        forever. Otherwise waits the amount of seconds specified.
  @param [in] deque    The deque.
  @param [in] timeout  Amount of seconds to wait before timing out.

  @return True if it successfully waited, false if it timed out.
  */
bool ThreadedDequeWaitEmpty(ThreadedDeque const *deque, int timeout);

/**
  @brief Create a shallow copy of a given deque.
  @note This makes a new deque pointing to the same memory as the old deque.
  @note Is only thread safe if original deque was also thread safe.
  @param [in] deque  The deque.

  @return A new deque pointing to the same data.
  */
ThreadedDeque *ThreadedDequeCopy(ThreadedDeque *deque);

#endif // CFENGINE_THREADED_DEQUE_H