aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/concurrent/parc_Lock.h
blob: ecba1c19b80b571b8a227cdc49e2b49bc9af500a (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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.
 */

/**
 * @file parc_Lock.h
 * @ingroup threading
 * @brief <#Brief Description#>
 *
 * <#Detailed Description#>
 *
 */
#ifndef PARCLibrary_parc_Lock
#define PARCLibrary_parc_Lock
#include <stdbool.h>

#include <parc/algol/parc_HashCode.h>
#include <parc/algol/parc_JSON.h>

struct PARCLock;
typedef struct PARCLock PARCLock;

/**
 * Increase the number of references to a `PARCLock` instance.
 *
 * Note that new `PARCLock` is not created,
 * only that the given `PARCLock` reference count is incremented.
 * Discard the reference by invoking `parcLock_Release`.
 *
 * @param [in] instance A pointer to a valid PARCLock instance.
 *
 * @return The same value as @p instance.
 *
 * Example:
 * @code
 * {
 *     PARCLock *a = parcLock_Create();
 *
 *     PARCLock *b = parcLock_Acquire();
 *
 *     parcLock_Release(&a);
 *     parcLock_Release(&b);
 * }
 * @endcode
 */
PARCLock *parcLock_Acquire(const PARCLock *instance);

#ifdef PARCLibrary_DISABLE_VALIDATION
#  define parcLock_OptionalAssertValid(_instance_)
#else
#  define parcLock_OptionalAssertValid(_instance_) parcLock_AssertValid(_instance_)
#endif

/**
 * Assert that the given `PARCLock` instance is valid.
 *
 * @param [in] instance A pointer to a valid PARCLock instance.
 *
 * Example:
 * @code
 * {
 *     PARCLock *a = parcLock_Create();
 *
 *     parcLock_AssertValid(a);
 *
 *     printf("Instance is valid.\n");
 *
 *     parcLock_Release(&b);
 * }
 * @endcode
 */
void parcLock_AssertValid(const PARCLock *instance);

/**
 * Create an instance of PARCLock
 *
 * <#Paragraphs Of Explanation#>
 *
 * @return non-NULL A pointer to a valid PARCLock instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCLock *a = parcLock_Create();
 *
 *     parcLock_Release(&a);
 * }
 * @endcode
 */
PARCLock *parcLock_Create(void);

/**
 * Compares @p instance with @p other for order.
 *
 * Returns a negative integer, zero, or a positive integer as @p instance
 * is less than, equal to, or greater than @p other.
 *
 * @param [in] instance A pointer to a valid PARCLock instance.
 * @param [in] other A pointer to a valid PARCLock instance.
 *
 * @return <0 Instance is less than @p other.
 * @return 0 Instance a and instance b compare the same.
 * @return >0 Instance a is greater than instance b.
 *
 * Example:
 * @code
 * {
 *     PARCLock *a = parcLock_Create();
 *     PARCLock *b = parcLock_Create();
 *
 *     if (parcLock_Compare(a, b) == 0) {
 *         printf("Instances are equal.\n");
 *     }
 *
 *     parcLock_Release(&a);
 *     parcLock_Release(&b);
 * }
 * @endcode
 *
 * @see parcLock_Equals
 */
int parcLock_Compare(const PARCLock *instance, const PARCLock *other);

/**
 * Print a human readable representation of the given `PARCLock`.
 *
 * @param [in] instance A pointer to a valid PARCLock instance.
 * @param [in] indentation The indentation level to use for printing.
 *
 * Example:
 * @code
 * {
 *     PARCLock *a = parcLock_Create();
 *
 *     parcLock_Display(a, 0);
 *
 *     parcLock_Release(&a);
 * }
 * @endcode
 */
void parcLock_Display(const PARCLock *instance, int indentation);

/**
 * Determine if an instance of `PARCLock` is valid.
 *
 * Valid means the internal state of the type is consistent with its required current or future behaviour.
 * This may include the validation of internal instances of types.
 *
 * @param [in] instance A pointer to a valid PARCLock instance.
 *
 * @return true The instance is valid.
 * @return false The instance is not valid.
 *
 * Example:
 * @code
 * {
 *     PARCLock *a = parcLock_Create();
 *
 *     if (parcLock_IsValid(a)) {
 *         printf("Instance is valid.\n");
 *     }
 *
 *     parcLock_Release(&a);
 * }
 * @endcode
 *
 */
bool parcLock_IsValid(const PARCLock *instance);

/**
 * Release a previously acquired reference to the given `PARCLock` instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in,out] instancePtr A pointer to a pointer to the instance to release.
 *
 * Example:
 * @code
 * {
 *     PARCLock *a = parcLock_Create();
 *
 *     parcLock_Release(&a);
 * }
 * @endcode
 */
void parcLock_Release(PARCLock **instancePtr);

/**
 * Append a representation of the specified `PARCLock` instance to the given `PARCBufferComposer`.
 *
 * @param [in] name A pointer to a `PARCLock` instance whose representation should be appended to the @p composer.
 * @param [in,out] composer A pointer to a `PARCBufferComposer` instance to be modified.
 *
 * @return NULL Cannot allocate memory.
 * @return non-NULL The @p composer.
 *
 * Example:
 * @code
 * {
 *     PARCBufferComposer *result = parcBufferComposer_Create();
 *
 *     parcLock_BuildString(instance, result);
 *
 *     char *string = parcBufferComposer_ToString(result);
 *     printf("Hello: %s\n", string);
 *     parcMemory_Deallocate(string);
 *
 *     parcBufferComposer_Release(&result);
 * }
 * @endcode
 */
PARCBufferComposer *parcLock_BuildString(const PARCLock *name, PARCBufferComposer *composer);

/**
 * Produce a null-terminated string representation of the specified `PARCLock`.
 *
 * The result must be freed by the caller via {@link parcMemory_Deallocate}.
 *
 * @param [in] instance A pointer to a valid PARCLock instance.
 *
 * @return NULL Cannot allocate memory.
 * @return non-NULL A pointer to an allocated, null-terminated C string that must be deallocated via {@link parcMemory_Deallocate}.
 *
 * Example:
 * @code
 * {
 *     PARCLock *a = parcLock_Create();
 *
 *     char *string = parcLock_ToString(a);
 *
 *     parcLock_Release(&a);
 *
 *     parcMemory_Deallocate(&string);
 * }
 * @endcode
 *
 * @see parcLock_Display
 */
char *parcLock_ToString(const PARCLock *instance);

/**
 * Obtain the lock on the given `PARCLock` instance.
 *
 * If the lock is already held by another thread, this function will block.
 * If the lock is aleady held by the current thread, this function will return `false`.
 *
 * Implementors must avoid deadlock by attempting to lock the object a second time within the same calling thread.
 *
 * @param [in] lock A pointer to a valid `PARCLock` instance.
 *
 * @return true The lock was obtained successfully.
 * @return false The lock is already held by the current thread, or the `PARCLock` is invalid.
 *
 * Example:
 * @code
 * {
 *     if (parcLock_Lock(lock)) {
 *
 *     }
 * }
 * @endcode
 */
bool parcLock_Lock(PARCLock *lock);

/**
 * Try to obtain the advisory lock on the given `PARCObject` instance.
 *
 * Once the lock is obtained, the caller must release the lock via `parcObject_Unlock`.
 *
 * @param [in] lock A pointer to a valid `PARCObject` instance.
 *
 * @return true The `PARCObject` is locked.
 * @return false The `PARCObject` is unlocked.
 *
 * Example:
 * @code
 * {
 *     while (parcLock_TryLock(object))
 *         ;
 * }
 * @endcode
 */
bool parcLock_TryLock(PARCLock *lock);

/**
 * Try to unlock the advisory lock on the given PARCObject instance.
 *
 * @param [in] lock A pointer to a valid PARCObject instance.
 *
 * @return true The PARCObject was locked and now is unlocked.
 * @return false The PARCObject was not locked and remains unlocked.
 *
 * Example:
 * @code
 * {
 *     parcLock_Unlock(object);
 * }
 * @endcode
 */
bool parcLock_Unlock(PARCLock *lock);

/**
 * Determine if the advisory lock on the given PARCObject instance is locked.
 *
 * @param [in] lock A pointer to a valid PARCObject instance.
 *
 * @return true The PARCObject is locked.
 * @return false The PARCObject is unlocked.
 * Example:
 * @code
 * {
 *     <#example#>
 * }
 * @endcode
 */
bool parcLock_IsLocked(const PARCLock *lock);

/**
 * Causes the calling thread to wait until either another thread invokes the parcObject_Notify() function on the same object.
 *
 * The calling thread must own this object's lock.
 * The calling thread will release ownership of this lock and wait until another thread invokes `parcObject_Notify`
 * on the same object. The original calling thread then re-obtains ownership of the lock and resumes execution.
 *
 * This function must only be called by a thread that is the owner of this object's lock.
 *
 * @param [in] lock A pointer to a valid PARCObject instance.
 *
 * Example:
 * @code
 * {
 *
 *     parcObject_Wait(object);
 * }
 * @endcode
 */
void parcLock_Wait(PARCLock *lock);

/**
 * Wakes up a single thread that is waiting on this object (see `parcObject_Wait)`.
 * If any threads are waiting on this object, one of them is chosen to be awakened.
 * The choice is arbitrary and occurs at the discretion of the underlying implementation.
 *
 * The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.
 * The awakened thread will compete in the usual manner with any other threads that might be actively
 * competing to synchronize on this object;
 * for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
 *
 * @param [in] lock A pointer to a valid `PARCObject` instance.
 *
 * Example:
 * @code
 * {
 *
 *     parcObject_Notify(object);
 * }
 * @endcode
 */
void parcLock_Notify(PARCLock *lock);
#endif