forked from espressif/arduino-esp32
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxos_mutex.h
executable file
·205 lines (176 loc) · 7.76 KB
/
xos_mutex.h
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
/** @file */
// xos_mutex.h - XOS Mutex API interface and data structures.
// Copyright (c) 2003-2015 Cadence Design Systems, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// NOTE: Do not include this file directly in your application. Including
// xos.h will automatically include this file.
#ifndef __XOS_MUTEX_H__
#define __XOS_MUTEX_H__
#include "xos_types.h"
#ifdef __cplusplus
extern "C" {
#endif
//-----------------------------------------------------------------------------
// Mutex flags.
//-----------------------------------------------------------------------------
#define XOS_MUTEX_WAIT_PRIORITY 0x0000 ///< Wake waiters in priority order (default)
#define XOS_MUTEX_WAIT_FIFO 0x0001 ///< Wake waiters in FIFO order
#define XOS_MUTEX_PRIORITY_CLG 0x0004 // Use priority ceiling
#define XOS_MUTEX_PRIORITY_INV 0x0008 // Protect against priority inversion
//-----------------------------------------------------------------------------
///
/// XosMutex object.
///
//-----------------------------------------------------------------------------
typedef struct XosMutex {
XosThread * owner; ///< Owning thread (null if unlocked).
XosThreadQueue waitq; ///< Queue of waiters.
uint32_t flags; ///< Properties.
uint32_t priority;
int32_t lock_count; ///< For recursive locking.
#if XOS_MUTEX_DEBUG
uint32_t sig; // Valid signature indicates inited.
#endif
} XosMutex;
//-----------------------------------------------------------------------------
///
/// Initialize a mutex object before first use.
///
/// \param mutex Pointer to mutex object.
///
/// \param flags Creation flags:
/// - XOS_MUTEX_WAIT_FIFO -- Queue waiting threads
/// in fifo order.
/// - XOS_MUTEX_WAIT_PRIORITY -- Queue waiting threads
/// by priority. This is the default.
/// - XOS_MUTEX_PRIORITY_CLG -- Use specified priority
/// value as the mutex's priority ceiling. If the
/// owning thread has a priority lower than the mutex's
/// priority, then the thread will have its priority
/// raised to the higher value as long as it owns the
/// mutex.
/// - XOS_MUTEX_PRIORITY_INV -- Protect against priority
/// inversion. If there is a waiting thread with a
/// higher priority than the current owner thread,
/// then the owner thread's priority is raised to the
/// higher value for as long as it owns the mutex.
///
/// \param priority Mutex's priority ceiling. This is used only if the
/// XOS_MUTEX_PRIORITY_CLG flag is set.
///
/// \return Returns XOS_OK on success, else error code.
///
/// NOTE: XOS_MUTEX_PRIORITY_CLG and XOS_MUTEX_PRIORITY_INV are NOT supported
/// in the current release. They will be supported in a future release.
///
//-----------------------------------------------------------------------------
int32_t
xos_mutex_create(XosMutex * mutex, uint32_t flags, uint8_t priority);
//-----------------------------------------------------------------------------
///
/// Destroy a mutex object. Must have been previously initialized by calling
/// xos_mutex_create().
///
/// \param mutex Pointer to mutex object.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_mutex_delete(XosMutex * mutex);
//-----------------------------------------------------------------------------
///
/// Take ownership of the mutex: block until the mutex is owned.
/// The mutex must have been initialized.
///
/// \param mutex Pointer to mutex object.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_mutex_lock(XosMutex * mutex);
//-----------------------------------------------------------------------------
///
/// Take ownership of the mutex: block until the mutex is owned or the timeout
/// expires. The mutex must have been initialized.
///
/// \param mutex Pointer to mutex object.
///
/// \param to_cycles Timeout in cycles. Convert from time to cycles
/// using the helper functions provided in xos_timer.
/// A value of zero indicates no timeout.
///
/// \return Returns XOS_OK on success, XOS_ERR_TIMEOUT on timeout, else error code.
///
/// NOTE: If XOS_OPT_WAIT_TIMEOUT is not enabled, then the timeout value is
/// ignored, and no timeout will occur.
///
//-----------------------------------------------------------------------------
int32_t
xos_mutex_lock_timeout(XosMutex * mutex, uint64_t to_cycles);
//-----------------------------------------------------------------------------
///
/// Release ownership of the mutex. The mutex must have been initialized and
/// must be owned by the calling thread.
///
/// \param mutex Pointer to mutex object.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_mutex_unlock(XosMutex * mutex);
//-----------------------------------------------------------------------------
///
/// Try to take ownership of the mutex, but do not block if the mutex is taken.
/// Return immediately. The mutex must have been initialized.
///
/// \param mutex Pointer to mutex object.
///
/// \return Returns XOS_OK on success (mutex owned), else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_mutex_trylock(XosMutex * mutex);
//-----------------------------------------------------------------------------
///
/// Return the state of the mutex (locked or unlocked) but do not attempt to
/// take ownership. The mutex must have been initialized.
///
/// \param mutex Pointer to mutex object.
///
/// \return Returns 0 if the mutex is unlocked, 1 if it is locked, -1 on error.
///
//-----------------------------------------------------------------------------
static inline int32_t
xos_mutex_test(XosMutex * mutex)
{
XOS_ASSERT(mutex);
if (mutex != XOS_NULL) {
return (mutex->owner != XOS_NULL) ? 1 : 0;
}
return -1;
}
#ifdef __cplusplus
}
#endif
#endif // __XOS_MUTEX_H__