User | Revision | Line number | New contents of line |
ganlikun |
0:06036f8bee2d
|
1
|
|
ganlikun |
0:06036f8bee2d
|
2
|
/** \addtogroup platform */
|
ganlikun |
0:06036f8bee2d
|
3
|
/** @{*/
|
ganlikun |
0:06036f8bee2d
|
4
|
/*
|
ganlikun |
0:06036f8bee2d
|
5
|
* Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
|
ganlikun |
0:06036f8bee2d
|
6
|
* SPDX-License-Identifier: Apache-2.0
|
ganlikun |
0:06036f8bee2d
|
7
|
*
|
ganlikun |
0:06036f8bee2d
|
8
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
ganlikun |
0:06036f8bee2d
|
9
|
* not use this file except in compliance with the License.
|
ganlikun |
0:06036f8bee2d
|
10
|
* You may obtain a copy of the License at
|
ganlikun |
0:06036f8bee2d
|
11
|
*
|
ganlikun |
0:06036f8bee2d
|
12
|
* http://www.apache.org/licenses/LICENSE-2.0
|
ganlikun |
0:06036f8bee2d
|
13
|
*
|
ganlikun |
0:06036f8bee2d
|
14
|
* Unless required by applicable law or agreed to in writing, software
|
ganlikun |
0:06036f8bee2d
|
15
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
ganlikun |
0:06036f8bee2d
|
16
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
ganlikun |
0:06036f8bee2d
|
17
|
* See the License for the specific language governing permissions and
|
ganlikun |
0:06036f8bee2d
|
18
|
* limitations under the License.
|
ganlikun |
0:06036f8bee2d
|
19
|
*/
|
ganlikun |
0:06036f8bee2d
|
20
|
|
ganlikun |
0:06036f8bee2d
|
21
|
#ifndef __MBED_UTIL_CRITICAL_H__
|
ganlikun |
0:06036f8bee2d
|
22
|
#define __MBED_UTIL_CRITICAL_H__
|
ganlikun |
0:06036f8bee2d
|
23
|
|
ganlikun |
0:06036f8bee2d
|
24
|
#include <stdbool.h>
|
ganlikun |
0:06036f8bee2d
|
25
|
#include <stdint.h>
|
ganlikun |
0:06036f8bee2d
|
26
|
#include <stddef.h>
|
ganlikun |
0:06036f8bee2d
|
27
|
|
ganlikun |
0:06036f8bee2d
|
28
|
#ifdef __cplusplus
|
ganlikun |
0:06036f8bee2d
|
29
|
extern "C" {
|
ganlikun |
0:06036f8bee2d
|
30
|
#endif
|
ganlikun |
0:06036f8bee2d
|
31
|
|
ganlikun |
0:06036f8bee2d
|
32
|
|
ganlikun |
0:06036f8bee2d
|
33
|
/** Determine the current interrupts enabled state
|
ganlikun |
0:06036f8bee2d
|
34
|
*
|
ganlikun |
0:06036f8bee2d
|
35
|
* This function can be called to determine whether or not interrupts are currently enabled.
|
ganlikun |
0:06036f8bee2d
|
36
|
* @note
|
ganlikun |
0:06036f8bee2d
|
37
|
* NOTE:
|
ganlikun |
0:06036f8bee2d
|
38
|
* This function works for both cortex-A and cortex-M, although the underlyng implementation
|
ganlikun |
0:06036f8bee2d
|
39
|
* differs.
|
ganlikun |
0:06036f8bee2d
|
40
|
* @return true if interrupts are enabled, false otherwise
|
ganlikun |
0:06036f8bee2d
|
41
|
*/
|
ganlikun |
0:06036f8bee2d
|
42
|
bool core_util_are_interrupts_enabled(void);
|
ganlikun |
0:06036f8bee2d
|
43
|
|
ganlikun |
0:06036f8bee2d
|
44
|
/** Determine if this code is executing from an interrupt
|
ganlikun |
0:06036f8bee2d
|
45
|
*
|
ganlikun |
0:06036f8bee2d
|
46
|
* This function can be called to determine if the code is running on interrupt context.
|
ganlikun |
0:06036f8bee2d
|
47
|
* @note
|
ganlikun |
0:06036f8bee2d
|
48
|
* NOTE:
|
ganlikun |
0:06036f8bee2d
|
49
|
* This function works for both cortex-A and cortex-M, although the underlyng implementation
|
ganlikun |
0:06036f8bee2d
|
50
|
* differs.
|
ganlikun |
0:06036f8bee2d
|
51
|
* @return true if in an isr, false otherwise
|
ganlikun |
0:06036f8bee2d
|
52
|
*/
|
ganlikun |
0:06036f8bee2d
|
53
|
bool core_util_is_isr_active(void);
|
ganlikun |
0:06036f8bee2d
|
54
|
|
ganlikun |
0:06036f8bee2d
|
55
|
/** Mark the start of a critical section
|
ganlikun |
0:06036f8bee2d
|
56
|
*
|
ganlikun |
0:06036f8bee2d
|
57
|
* This function should be called to mark the start of a critical section of code.
|
ganlikun |
0:06036f8bee2d
|
58
|
* @note
|
ganlikun |
0:06036f8bee2d
|
59
|
* NOTES:
|
ganlikun |
0:06036f8bee2d
|
60
|
* 1) The use of this style of critical section is targetted at C based implementations.
|
ganlikun |
0:06036f8bee2d
|
61
|
* 2) These critical sections can be nested.
|
ganlikun |
0:06036f8bee2d
|
62
|
* 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
|
ganlikun |
0:06036f8bee2d
|
63
|
* section) will be preserved on exit from the section.
|
ganlikun |
0:06036f8bee2d
|
64
|
* 4) This implementation will currently only work on code running in privileged mode.
|
ganlikun |
0:06036f8bee2d
|
65
|
*/
|
ganlikun |
0:06036f8bee2d
|
66
|
void core_util_critical_section_enter(void);
|
ganlikun |
0:06036f8bee2d
|
67
|
|
ganlikun |
0:06036f8bee2d
|
68
|
/** Mark the end of a critical section
|
ganlikun |
0:06036f8bee2d
|
69
|
*
|
ganlikun |
0:06036f8bee2d
|
70
|
* This function should be called to mark the end of a critical section of code.
|
ganlikun |
0:06036f8bee2d
|
71
|
* @note
|
ganlikun |
0:06036f8bee2d
|
72
|
* NOTES:
|
ganlikun |
0:06036f8bee2d
|
73
|
* 1) The use of this style of critical section is targetted at C based implementations.
|
ganlikun |
0:06036f8bee2d
|
74
|
* 2) These critical sections can be nested.
|
ganlikun |
0:06036f8bee2d
|
75
|
* 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
|
ganlikun |
0:06036f8bee2d
|
76
|
* section) will be preserved on exit from the section.
|
ganlikun |
0:06036f8bee2d
|
77
|
* 4) This implementation will currently only work on code running in privileged mode.
|
ganlikun |
0:06036f8bee2d
|
78
|
*/
|
ganlikun |
0:06036f8bee2d
|
79
|
void core_util_critical_section_exit(void);
|
ganlikun |
0:06036f8bee2d
|
80
|
|
ganlikun |
0:06036f8bee2d
|
81
|
/**
|
ganlikun |
0:06036f8bee2d
|
82
|
* Atomic compare and set. It compares the contents of a memory location to a
|
ganlikun |
0:06036f8bee2d
|
83
|
* given value and, only if they are the same, modifies the contents of that
|
ganlikun |
0:06036f8bee2d
|
84
|
* memory location to a given new value. This is done as a single atomic
|
ganlikun |
0:06036f8bee2d
|
85
|
* operation. The atomicity guarantees that the new value is calculated based on
|
ganlikun |
0:06036f8bee2d
|
86
|
* up-to-date information; if the value had been updated by another thread in
|
ganlikun |
0:06036f8bee2d
|
87
|
* the meantime, the write would fail due to a mismatched expectedCurrentValue.
|
ganlikun |
0:06036f8bee2d
|
88
|
*
|
ganlikun |
0:06036f8bee2d
|
89
|
* Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
|
ganlikun |
0:06036f8bee2d
|
90
|
* you to the article on compare-and swap].
|
ganlikun |
0:06036f8bee2d
|
91
|
*
|
ganlikun |
0:06036f8bee2d
|
92
|
* @param ptr The target memory location.
|
ganlikun |
0:06036f8bee2d
|
93
|
* @param[in,out] expectedCurrentValue A pointer to some location holding the
|
ganlikun |
0:06036f8bee2d
|
94
|
* expected current value of the data being set atomically.
|
ganlikun |
0:06036f8bee2d
|
95
|
* The computed 'desiredValue' should be a function of this current value.
|
ganlikun |
0:06036f8bee2d
|
96
|
* @note: This is an in-out parameter. In the
|
ganlikun |
0:06036f8bee2d
|
97
|
* failure case of atomic_cas (where the
|
ganlikun |
0:06036f8bee2d
|
98
|
* destination isn't set), the pointee of expectedCurrentValue is
|
ganlikun |
0:06036f8bee2d
|
99
|
* updated with the current value.
|
ganlikun |
0:06036f8bee2d
|
100
|
* @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
|
ganlikun |
0:06036f8bee2d
|
101
|
*
|
ganlikun |
0:06036f8bee2d
|
102
|
* @return true if the memory location was atomically
|
ganlikun |
0:06036f8bee2d
|
103
|
* updated with the desired value (after verifying
|
ganlikun |
0:06036f8bee2d
|
104
|
* that it contained the expectedCurrentValue),
|
ganlikun |
0:06036f8bee2d
|
105
|
* false otherwise. In the failure case,
|
ganlikun |
0:06036f8bee2d
|
106
|
* exepctedCurrentValue is updated with the new
|
ganlikun |
0:06036f8bee2d
|
107
|
* value of the target memory location.
|
ganlikun |
0:06036f8bee2d
|
108
|
*
|
ganlikun |
0:06036f8bee2d
|
109
|
* pseudocode:
|
ganlikun |
0:06036f8bee2d
|
110
|
* function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
|
ganlikun |
0:06036f8bee2d
|
111
|
* if *p != *old {
|
ganlikun |
0:06036f8bee2d
|
112
|
* *old = *p
|
ganlikun |
0:06036f8bee2d
|
113
|
* return false
|
ganlikun |
0:06036f8bee2d
|
114
|
* }
|
ganlikun |
0:06036f8bee2d
|
115
|
* *p = new
|
ganlikun |
0:06036f8bee2d
|
116
|
* return true
|
ganlikun |
0:06036f8bee2d
|
117
|
* }
|
ganlikun |
0:06036f8bee2d
|
118
|
*
|
ganlikun |
0:06036f8bee2d
|
119
|
* @note: In the failure case (where the destination isn't set), the value
|
ganlikun |
0:06036f8bee2d
|
120
|
* pointed to by expectedCurrentValue is still updated with the current value.
|
ganlikun |
0:06036f8bee2d
|
121
|
* This property helps writing concise code for the following incr:
|
ganlikun |
0:06036f8bee2d
|
122
|
*
|
ganlikun |
0:06036f8bee2d
|
123
|
* function incr(p : pointer to int, a : int) returns int {
|
ganlikun |
0:06036f8bee2d
|
124
|
* done = false
|
ganlikun |
0:06036f8bee2d
|
125
|
* value = *p // This fetch operation need not be atomic.
|
ganlikun |
0:06036f8bee2d
|
126
|
* while not done {
|
ganlikun |
0:06036f8bee2d
|
127
|
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
ganlikun |
0:06036f8bee2d
|
128
|
* }
|
ganlikun |
0:06036f8bee2d
|
129
|
* return value + a
|
ganlikun |
0:06036f8bee2d
|
130
|
* }
|
ganlikun |
0:06036f8bee2d
|
131
|
*/
|
ganlikun |
0:06036f8bee2d
|
132
|
bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue);
|
ganlikun |
0:06036f8bee2d
|
133
|
|
ganlikun |
0:06036f8bee2d
|
134
|
/**
|
ganlikun |
0:06036f8bee2d
|
135
|
* Atomic compare and set. It compares the contents of a memory location to a
|
ganlikun |
0:06036f8bee2d
|
136
|
* given value and, only if they are the same, modifies the contents of that
|
ganlikun |
0:06036f8bee2d
|
137
|
* memory location to a given new value. This is done as a single atomic
|
ganlikun |
0:06036f8bee2d
|
138
|
* operation. The atomicity guarantees that the new value is calculated based on
|
ganlikun |
0:06036f8bee2d
|
139
|
* up-to-date information; if the value had been updated by another thread in
|
ganlikun |
0:06036f8bee2d
|
140
|
* the meantime, the write would fail due to a mismatched expectedCurrentValue.
|
ganlikun |
0:06036f8bee2d
|
141
|
*
|
ganlikun |
0:06036f8bee2d
|
142
|
* Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
|
ganlikun |
0:06036f8bee2d
|
143
|
* you to the article on compare-and swap].
|
ganlikun |
0:06036f8bee2d
|
144
|
*
|
ganlikun |
0:06036f8bee2d
|
145
|
* @param ptr The target memory location.
|
ganlikun |
0:06036f8bee2d
|
146
|
* @param[in,out] expectedCurrentValue A pointer to some location holding the
|
ganlikun |
0:06036f8bee2d
|
147
|
* expected current value of the data being set atomically.
|
ganlikun |
0:06036f8bee2d
|
148
|
* The computed 'desiredValue' should be a function of this current value.
|
ganlikun |
0:06036f8bee2d
|
149
|
* @note: This is an in-out parameter. In the
|
ganlikun |
0:06036f8bee2d
|
150
|
* failure case of atomic_cas (where the
|
ganlikun |
0:06036f8bee2d
|
151
|
* destination isn't set), the pointee of expectedCurrentValue is
|
ganlikun |
0:06036f8bee2d
|
152
|
* updated with the current value.
|
ganlikun |
0:06036f8bee2d
|
153
|
* @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
|
ganlikun |
0:06036f8bee2d
|
154
|
*
|
ganlikun |
0:06036f8bee2d
|
155
|
* @return true if the memory location was atomically
|
ganlikun |
0:06036f8bee2d
|
156
|
* updated with the desired value (after verifying
|
ganlikun |
0:06036f8bee2d
|
157
|
* that it contained the expectedCurrentValue),
|
ganlikun |
0:06036f8bee2d
|
158
|
* false otherwise. In the failure case,
|
ganlikun |
0:06036f8bee2d
|
159
|
* exepctedCurrentValue is updated with the new
|
ganlikun |
0:06036f8bee2d
|
160
|
* value of the target memory location.
|
ganlikun |
0:06036f8bee2d
|
161
|
*
|
ganlikun |
0:06036f8bee2d
|
162
|
* pseudocode:
|
ganlikun |
0:06036f8bee2d
|
163
|
* function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
|
ganlikun |
0:06036f8bee2d
|
164
|
* if *p != *old {
|
ganlikun |
0:06036f8bee2d
|
165
|
* *old = *p
|
ganlikun |
0:06036f8bee2d
|
166
|
* return false
|
ganlikun |
0:06036f8bee2d
|
167
|
* }
|
ganlikun |
0:06036f8bee2d
|
168
|
* *p = new
|
ganlikun |
0:06036f8bee2d
|
169
|
* return true
|
ganlikun |
0:06036f8bee2d
|
170
|
* }
|
ganlikun |
0:06036f8bee2d
|
171
|
*
|
ganlikun |
0:06036f8bee2d
|
172
|
* @note: In the failure case (where the destination isn't set), the value
|
ganlikun |
0:06036f8bee2d
|
173
|
* pointed to by expectedCurrentValue is still updated with the current value.
|
ganlikun |
0:06036f8bee2d
|
174
|
* This property helps writing concise code for the following incr:
|
ganlikun |
0:06036f8bee2d
|
175
|
*
|
ganlikun |
0:06036f8bee2d
|
176
|
* function incr(p : pointer to int, a : int) returns int {
|
ganlikun |
0:06036f8bee2d
|
177
|
* done = false
|
ganlikun |
0:06036f8bee2d
|
178
|
* value = *p // This fetch operation need not be atomic.
|
ganlikun |
0:06036f8bee2d
|
179
|
* while not done {
|
ganlikun |
0:06036f8bee2d
|
180
|
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
ganlikun |
0:06036f8bee2d
|
181
|
* }
|
ganlikun |
0:06036f8bee2d
|
182
|
* return value + a
|
ganlikun |
0:06036f8bee2d
|
183
|
* }
|
ganlikun |
0:06036f8bee2d
|
184
|
*/
|
ganlikun |
0:06036f8bee2d
|
185
|
bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue);
|
ganlikun |
0:06036f8bee2d
|
186
|
|
ganlikun |
0:06036f8bee2d
|
187
|
/**
|
ganlikun |
0:06036f8bee2d
|
188
|
* Atomic compare and set. It compares the contents of a memory location to a
|
ganlikun |
0:06036f8bee2d
|
189
|
* given value and, only if they are the same, modifies the contents of that
|
ganlikun |
0:06036f8bee2d
|
190
|
* memory location to a given new value. This is done as a single atomic
|
ganlikun |
0:06036f8bee2d
|
191
|
* operation. The atomicity guarantees that the new value is calculated based on
|
ganlikun |
0:06036f8bee2d
|
192
|
* up-to-date information; if the value had been updated by another thread in
|
ganlikun |
0:06036f8bee2d
|
193
|
* the meantime, the write would fail due to a mismatched expectedCurrentValue.
|
ganlikun |
0:06036f8bee2d
|
194
|
*
|
ganlikun |
0:06036f8bee2d
|
195
|
* Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
|
ganlikun |
0:06036f8bee2d
|
196
|
* you to the article on compare-and swap].
|
ganlikun |
0:06036f8bee2d
|
197
|
*
|
ganlikun |
0:06036f8bee2d
|
198
|
* @param ptr The target memory location.
|
ganlikun |
0:06036f8bee2d
|
199
|
* @param[in,out] expectedCurrentValue A pointer to some location holding the
|
ganlikun |
0:06036f8bee2d
|
200
|
* expected current value of the data being set atomically.
|
ganlikun |
0:06036f8bee2d
|
201
|
* The computed 'desiredValue' should be a function of this current value.
|
ganlikun |
0:06036f8bee2d
|
202
|
* @note: This is an in-out parameter. In the
|
ganlikun |
0:06036f8bee2d
|
203
|
* failure case of atomic_cas (where the
|
ganlikun |
0:06036f8bee2d
|
204
|
* destination isn't set), the pointee of expectedCurrentValue is
|
ganlikun |
0:06036f8bee2d
|
205
|
* updated with the current value.
|
ganlikun |
0:06036f8bee2d
|
206
|
* @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
|
ganlikun |
0:06036f8bee2d
|
207
|
*
|
ganlikun |
0:06036f8bee2d
|
208
|
* @return true if the memory location was atomically
|
ganlikun |
0:06036f8bee2d
|
209
|
* updated with the desired value (after verifying
|
ganlikun |
0:06036f8bee2d
|
210
|
* that it contained the expectedCurrentValue),
|
ganlikun |
0:06036f8bee2d
|
211
|
* false otherwise. In the failure case,
|
ganlikun |
0:06036f8bee2d
|
212
|
* exepctedCurrentValue is updated with the new
|
ganlikun |
0:06036f8bee2d
|
213
|
* value of the target memory location.
|
ganlikun |
0:06036f8bee2d
|
214
|
*
|
ganlikun |
0:06036f8bee2d
|
215
|
* pseudocode:
|
ganlikun |
0:06036f8bee2d
|
216
|
* function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
|
ganlikun |
0:06036f8bee2d
|
217
|
* if *p != *old {
|
ganlikun |
0:06036f8bee2d
|
218
|
* *old = *p
|
ganlikun |
0:06036f8bee2d
|
219
|
* return false
|
ganlikun |
0:06036f8bee2d
|
220
|
* }
|
ganlikun |
0:06036f8bee2d
|
221
|
* *p = new
|
ganlikun |
0:06036f8bee2d
|
222
|
* return true
|
ganlikun |
0:06036f8bee2d
|
223
|
* }
|
ganlikun |
0:06036f8bee2d
|
224
|
*
|
ganlikun |
0:06036f8bee2d
|
225
|
* @note: In the failure case (where the destination isn't set), the value
|
ganlikun |
0:06036f8bee2d
|
226
|
* pointed to by expectedCurrentValue is still updated with the current value.
|
ganlikun |
0:06036f8bee2d
|
227
|
* This property helps writing concise code for the following incr:
|
ganlikun |
0:06036f8bee2d
|
228
|
*
|
ganlikun |
0:06036f8bee2d
|
229
|
* function incr(p : pointer to int, a : int) returns int {
|
ganlikun |
0:06036f8bee2d
|
230
|
* done = false
|
ganlikun |
0:06036f8bee2d
|
231
|
* value = *p // This fetch operation need not be atomic.
|
ganlikun |
0:06036f8bee2d
|
232
|
* while not done {
|
ganlikun |
0:06036f8bee2d
|
233
|
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
ganlikun |
0:06036f8bee2d
|
234
|
* }
|
ganlikun |
0:06036f8bee2d
|
235
|
* return value + a
|
ganlikun |
0:06036f8bee2d
|
236
|
* }
|
ganlikun |
0:06036f8bee2d
|
237
|
*/
|
ganlikun |
0:06036f8bee2d
|
238
|
bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue);
|
ganlikun |
0:06036f8bee2d
|
239
|
|
ganlikun |
0:06036f8bee2d
|
240
|
/**
|
ganlikun |
0:06036f8bee2d
|
241
|
* Atomic compare and set. It compares the contents of a memory location to a
|
ganlikun |
0:06036f8bee2d
|
242
|
* given value and, only if they are the same, modifies the contents of that
|
ganlikun |
0:06036f8bee2d
|
243
|
* memory location to a given new value. This is done as a single atomic
|
ganlikun |
0:06036f8bee2d
|
244
|
* operation. The atomicity guarantees that the new value is calculated based on
|
ganlikun |
0:06036f8bee2d
|
245
|
* up-to-date information; if the value had been updated by another thread in
|
ganlikun |
0:06036f8bee2d
|
246
|
* the meantime, the write would fail due to a mismatched expectedCurrentValue.
|
ganlikun |
0:06036f8bee2d
|
247
|
*
|
ganlikun |
0:06036f8bee2d
|
248
|
* Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
|
ganlikun |
0:06036f8bee2d
|
249
|
* you to the article on compare-and swap].
|
ganlikun |
0:06036f8bee2d
|
250
|
*
|
ganlikun |
0:06036f8bee2d
|
251
|
* @param ptr The target memory location.
|
ganlikun |
0:06036f8bee2d
|
252
|
* @param[in,out] expectedCurrentValue A pointer to some location holding the
|
ganlikun |
0:06036f8bee2d
|
253
|
* expected current value of the data being set atomically.
|
ganlikun |
0:06036f8bee2d
|
254
|
* The computed 'desiredValue' should be a function of this current value.
|
ganlikun |
0:06036f8bee2d
|
255
|
* @note: This is an in-out parameter. In the
|
ganlikun |
0:06036f8bee2d
|
256
|
* failure case of atomic_cas (where the
|
ganlikun |
0:06036f8bee2d
|
257
|
* destination isn't set), the pointee of expectedCurrentValue is
|
ganlikun |
0:06036f8bee2d
|
258
|
* updated with the current value.
|
ganlikun |
0:06036f8bee2d
|
259
|
* @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
|
ganlikun |
0:06036f8bee2d
|
260
|
*
|
ganlikun |
0:06036f8bee2d
|
261
|
* @return true if the memory location was atomically
|
ganlikun |
0:06036f8bee2d
|
262
|
* updated with the desired value (after verifying
|
ganlikun |
0:06036f8bee2d
|
263
|
* that it contained the expectedCurrentValue),
|
ganlikun |
0:06036f8bee2d
|
264
|
* false otherwise. In the failure case,
|
ganlikun |
0:06036f8bee2d
|
265
|
* exepctedCurrentValue is updated with the new
|
ganlikun |
0:06036f8bee2d
|
266
|
* value of the target memory location.
|
ganlikun |
0:06036f8bee2d
|
267
|
*
|
ganlikun |
0:06036f8bee2d
|
268
|
* pseudocode:
|
ganlikun |
0:06036f8bee2d
|
269
|
* function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
|
ganlikun |
0:06036f8bee2d
|
270
|
* if *p != *old {
|
ganlikun |
0:06036f8bee2d
|
271
|
* *old = *p
|
ganlikun |
0:06036f8bee2d
|
272
|
* return false
|
ganlikun |
0:06036f8bee2d
|
273
|
* }
|
ganlikun |
0:06036f8bee2d
|
274
|
* *p = new
|
ganlikun |
0:06036f8bee2d
|
275
|
* return true
|
ganlikun |
0:06036f8bee2d
|
276
|
* }
|
ganlikun |
0:06036f8bee2d
|
277
|
*
|
ganlikun |
0:06036f8bee2d
|
278
|
* @note: In the failure case (where the destination isn't set), the value
|
ganlikun |
0:06036f8bee2d
|
279
|
* pointed to by expectedCurrentValue is still updated with the current value.
|
ganlikun |
0:06036f8bee2d
|
280
|
* This property helps writing concise code for the following incr:
|
ganlikun |
0:06036f8bee2d
|
281
|
*
|
ganlikun |
0:06036f8bee2d
|
282
|
* function incr(p : pointer to int, a : int) returns int {
|
ganlikun |
0:06036f8bee2d
|
283
|
* done = false
|
ganlikun |
0:06036f8bee2d
|
284
|
* value = *p // This fetch operation need not be atomic.
|
ganlikun |
0:06036f8bee2d
|
285
|
* while not done {
|
ganlikun |
0:06036f8bee2d
|
286
|
* done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
|
ganlikun |
0:06036f8bee2d
|
287
|
* }
|
ganlikun |
0:06036f8bee2d
|
288
|
* return value + a
|
ganlikun |
0:06036f8bee2d
|
289
|
* }
|
ganlikun |
0:06036f8bee2d
|
290
|
*/
|
ganlikun |
0:06036f8bee2d
|
291
|
bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue);
|
ganlikun |
0:06036f8bee2d
|
292
|
|
ganlikun |
0:06036f8bee2d
|
293
|
/**
|
ganlikun |
0:06036f8bee2d
|
294
|
* Atomic increment.
|
ganlikun |
0:06036f8bee2d
|
295
|
* @param valuePtr Target memory location being incremented.
|
ganlikun |
0:06036f8bee2d
|
296
|
* @param delta The amount being incremented.
|
ganlikun |
0:06036f8bee2d
|
297
|
* @return The new incremented value.
|
ganlikun |
0:06036f8bee2d
|
298
|
*/
|
ganlikun |
0:06036f8bee2d
|
299
|
uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta);
|
ganlikun |
0:06036f8bee2d
|
300
|
|
ganlikun |
0:06036f8bee2d
|
301
|
/**
|
ganlikun |
0:06036f8bee2d
|
302
|
* Atomic increment.
|
ganlikun |
0:06036f8bee2d
|
303
|
* @param valuePtr Target memory location being incremented.
|
ganlikun |
0:06036f8bee2d
|
304
|
* @param delta The amount being incremented.
|
ganlikun |
0:06036f8bee2d
|
305
|
* @return The new incremented value.
|
ganlikun |
0:06036f8bee2d
|
306
|
*/
|
ganlikun |
0:06036f8bee2d
|
307
|
uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta);
|
ganlikun |
0:06036f8bee2d
|
308
|
|
ganlikun |
0:06036f8bee2d
|
309
|
/**
|
ganlikun |
0:06036f8bee2d
|
310
|
* Atomic increment.
|
ganlikun |
0:06036f8bee2d
|
311
|
* @param valuePtr Target memory location being incremented.
|
ganlikun |
0:06036f8bee2d
|
312
|
* @param delta The amount being incremented.
|
ganlikun |
0:06036f8bee2d
|
313
|
* @return The new incremented value.
|
ganlikun |
0:06036f8bee2d
|
314
|
*/
|
ganlikun |
0:06036f8bee2d
|
315
|
uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta);
|
ganlikun |
0:06036f8bee2d
|
316
|
|
ganlikun |
0:06036f8bee2d
|
317
|
/**
|
ganlikun |
0:06036f8bee2d
|
318
|
* Atomic increment.
|
ganlikun |
0:06036f8bee2d
|
319
|
* @param valuePtr Target memory location being incremented.
|
ganlikun |
0:06036f8bee2d
|
320
|
* @param delta The amount being incremented in bytes.
|
ganlikun |
0:06036f8bee2d
|
321
|
* @return The new incremented value.
|
ganlikun |
0:06036f8bee2d
|
322
|
*
|
ganlikun |
0:06036f8bee2d
|
323
|
* @note The type of the pointer argument is not taken into account
|
ganlikun |
0:06036f8bee2d
|
324
|
* and the pointer is incremented by bytes.
|
ganlikun |
0:06036f8bee2d
|
325
|
*/
|
ganlikun |
0:06036f8bee2d
|
326
|
void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta);
|
ganlikun |
0:06036f8bee2d
|
327
|
|
ganlikun |
0:06036f8bee2d
|
328
|
/**
|
ganlikun |
0:06036f8bee2d
|
329
|
* Atomic decrement.
|
ganlikun |
0:06036f8bee2d
|
330
|
* @param valuePtr Target memory location being decremented.
|
ganlikun |
0:06036f8bee2d
|
331
|
* @param delta The amount being decremented.
|
ganlikun |
0:06036f8bee2d
|
332
|
* @return The new decremented value.
|
ganlikun |
0:06036f8bee2d
|
333
|
*/
|
ganlikun |
0:06036f8bee2d
|
334
|
uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta);
|
ganlikun |
0:06036f8bee2d
|
335
|
|
ganlikun |
0:06036f8bee2d
|
336
|
/**
|
ganlikun |
0:06036f8bee2d
|
337
|
* Atomic decrement.
|
ganlikun |
0:06036f8bee2d
|
338
|
* @param valuePtr Target memory location being decremented.
|
ganlikun |
0:06036f8bee2d
|
339
|
* @param delta The amount being decremented.
|
ganlikun |
0:06036f8bee2d
|
340
|
* @return The new decremented value.
|
ganlikun |
0:06036f8bee2d
|
341
|
*/
|
ganlikun |
0:06036f8bee2d
|
342
|
uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta);
|
ganlikun |
0:06036f8bee2d
|
343
|
|
ganlikun |
0:06036f8bee2d
|
344
|
/**
|
ganlikun |
0:06036f8bee2d
|
345
|
* Atomic decrement.
|
ganlikun |
0:06036f8bee2d
|
346
|
* @param valuePtr Target memory location being decremented.
|
ganlikun |
0:06036f8bee2d
|
347
|
* @param delta The amount being decremented.
|
ganlikun |
0:06036f8bee2d
|
348
|
* @return The new decremented value.
|
ganlikun |
0:06036f8bee2d
|
349
|
*/
|
ganlikun |
0:06036f8bee2d
|
350
|
uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta);
|
ganlikun |
0:06036f8bee2d
|
351
|
|
ganlikun |
0:06036f8bee2d
|
352
|
/**
|
ganlikun |
0:06036f8bee2d
|
353
|
* Atomic decrement.
|
ganlikun |
0:06036f8bee2d
|
354
|
* @param valuePtr Target memory location being decremented.
|
ganlikun |
0:06036f8bee2d
|
355
|
* @param delta The amount being decremented in bytes.
|
ganlikun |
0:06036f8bee2d
|
356
|
* @return The new decremented value.
|
ganlikun |
0:06036f8bee2d
|
357
|
*
|
ganlikun |
0:06036f8bee2d
|
358
|
* @note The type of the pointer argument is not taken into account
|
ganlikun |
0:06036f8bee2d
|
359
|
* and the pointer is decremented by bytes
|
ganlikun |
0:06036f8bee2d
|
360
|
*/
|
ganlikun |
0:06036f8bee2d
|
361
|
void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta);
|
ganlikun |
0:06036f8bee2d
|
362
|
|
ganlikun |
0:06036f8bee2d
|
363
|
#ifdef __cplusplus
|
ganlikun |
0:06036f8bee2d
|
364
|
} // extern "C"
|
ganlikun |
0:06036f8bee2d
|
365
|
#endif
|
ganlikun |
0:06036f8bee2d
|
366
|
|
ganlikun |
0:06036f8bee2d
|
367
|
|
ganlikun |
0:06036f8bee2d
|
368
|
#endif // __MBED_UTIL_CRITICAL_H__
|
ganlikun |
0:06036f8bee2d
|
369
|
|
ganlikun |
0:06036f8bee2d
|
370
|
/** @}*/
|
ganlikun |
0:06036f8bee2d
|
371
|
|