aboutsummaryrefslogtreecommitdiff
path: root/include/odp/api/spec/rwlock_recursive.h
blob: 1c19c7217d08f8a09dbc0c9a4345266523a9cd2e (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
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/**
 * @file
 *
 * ODP recursive read/write lock
 */

#ifndef ODP_API_RWLOCK_RECURSIVE_H_
#define ODP_API_RWLOCK_RECURSIVE_H_
#include <odp/visibility_begin.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @addtogroup odp_locks
 * @details
 * <b> Recursive reader/writer lock (odp_rwlock_recursive_t) </b>
 *
 * This is recursive version of the reader/writer lock.
 * A thread can read- or write-acquire a recursive read-write lock multiple
 * times without a deadlock. To release the lock, the thread must unlock it
 * the same number of times. Recursion is supported only for a pure series of
 * read or write lock calls. Read and write lock calls must not be mixed when
 * recursing.
 *
 * For example, these are supported...
 *   * read_lock(); read_lock(); read_unlock(); read_unlock();
 *   * write_lock(); write_lock(); write_unlock(); write_unlock();
 *
 * ... but this is not supported.
 *   * read_lock(); write_lock(); write_unlock(); read_unlock();
 *
 * The trylock variants can be used to avoid blocking when the lock
 * is not immediately available.
 * @{
 */

/**
 * @typedef odp_rwlock_recursive_t
 * Recursive rwlock
 */

/**
 * Initialize recursive rwlock
 *
 * @param lock    Pointer to a lock
 */
void odp_rwlock_recursive_init(odp_rwlock_recursive_t *lock);

/**
 * Acquire recursive rwlock for reading
 *
 * This call allows the thread to acquire the same lock multiple times for
 * reading. The lock cannot be acquired for writing while holding it
 * for reading.
 *
 * @param lock    Pointer to a lock
 */
void odp_rwlock_recursive_read_lock(odp_rwlock_recursive_t *lock);

/**
 * Try to acquire recursive rwlock for reading
 *
 * @param lock    Pointer to a lock
 *
 * @retval  0     Lock was not available for read access
 * @retval !0     Read access to lock acquired
 */
int odp_rwlock_recursive_read_trylock(odp_rwlock_recursive_t *lock);

/**
 * Release recursive rwlock after reading
 *
 * @param lock    Pointer to a lock
 */
void odp_rwlock_recursive_read_unlock(odp_rwlock_recursive_t *lock);

/**
 * Acquire recursive rwlock for writing
 *
 * This call allows the thread to acquire the same lock multiple times for
 * writing. The lock cannot be acquired for reading while holding it
 * for writing.
 *
 * @param lock    Pointer to a lock
 */
void odp_rwlock_recursive_write_lock(odp_rwlock_recursive_t *lock);

/**
 * Try to acquire recursive rwlock for writing
 *
 * @param lock    Pointer to a lock
 *
 * @retval  0     Lock was not available for write access
 * @retval !0     Write access to lock acquired
 */
int odp_rwlock_recursive_write_trylock(odp_rwlock_recursive_t *lock);

/**
 * Release recursive rwlock after writing
 *
 * @param lock    Pointer to a lock
 */
void odp_rwlock_recursive_write_unlock(odp_rwlock_recursive_t *lock);

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#include <odp/visibility_end.h>
#endif