Mstdlib-1.24.0
m_atomic.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_ATOMIC_H__
25#define __M_ATOMIC_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31
32/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
33
34__BEGIN_DECLS
35
36/*! \addtogroup m_atomic Atomics
37 * \ingroup m_thread
38 *
39 * Operations which are guaranteed to be atomic.
40 *
41 * @{
42 */
43
44/*! Compare and swap 32bit integer.
45 *
46 * \param[in,out] ptr Pointer to var to operate on
47 * \param[in] expected Expected value of var before completing operation
48 * \param[in] newval Value to set var to
49 * \return M_TRUE on success, M_FALSE on failure
50 */
51M_API M_bool M_atomic_cas32(volatile M_uint32 *ptr, M_uint32 expected, M_uint32 newval);
52
53
54/*! Compare and swap 64bit integer.
55 *
56 * \param[in,out] ptr Pointer to var to operate on
57 * \param[in] expected Expected value of var before completing operation
58 * \param[in] newval Value to set var to
59 * \return M_TRUE on success, M_FALSE on failure
60 */
61M_API M_bool M_atomic_cas64(volatile M_uint64 *ptr, M_uint64 expected, M_uint64 newval);
62
63
64/*! Increment u32 by 1.
65 *
66 * \param[in] ptr Pointer to var to operate on.
67 *
68 * \return The value of pointer before operation.
69 */
70M_API M_uint32 M_atomic_inc_u32(volatile M_uint32 *ptr);
71
72
73/*! Increment u64 by 1.
74 *
75 * \param[in] ptr Pointer to var to operate on.
76 *
77 * \return The value of pointer before operation.
78 */
79M_API M_uint64 M_atomic_inc_u64(volatile M_uint64 *ptr);
80
81
82/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
83
84/*! Decrement u32 by 1.
85 *
86 * \param[in] ptr Pointer to var to operate on.
87 *
88 * \return The value of pointer before operation.
89 */
90M_API M_uint32 M_atomic_dec_u32(volatile M_uint32 *ptr);
91
92/*! Decrement u64 by 1.
93 *
94 * \param[in] ptr Pointer to var to operate on.
95 *
96 * \return The value of pointer before operation.
97 */
98M_API M_uint64 M_atomic_dec_u64(volatile M_uint64 *ptr);
99
100
101/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
102
103/*! Add a given value with u32.
104 *
105 * \param[in] ptr Pointer to var to operate on.
106 * \param[in] val Value to modify ptr with.
107 *
108 * \return The value of pointer before operation.
109 */
110M_API M_uint32 M_atomic_add_u32(volatile M_uint32 *ptr, M_uint32 val);
111
112
113/*! Add a given value with u64.
114 *
115 * \param[in] ptr Pointer to var to operate on.
116 * \param[in] val Value to modify ptr with.
117 *
118 * \return The value of pointer before operation.
119 */
120M_API M_uint64 M_atomic_add_u64(volatile M_uint64 *ptr, M_uint64 val);
121
122
123/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
124
125/*! Subtract a given value with u32.
126 *
127 * \param[in] ptr Pointer to var to operate on.
128 * \param[in] val Value to modify ptr with.
129 *
130 * \return The value of pointer before operation.
131 */
132M_API M_uint32 M_atomic_sub_u32(volatile M_uint32 *ptr, M_uint32 val);
133
134
135/*! Subtract a given value with u64.
136 *
137 * \param[in] ptr Pointer to var to operate on.
138 * \param[in] val Value to modify ptr with.
139 *
140 * \return The value of pointer before operation.
141 */
142M_API M_uint64 M_atomic_sub_u64(volatile M_uint64 *ptr, M_uint64 val);
143
144/*! @} */
145
146__END_DECLS
147
148#endif /* __M_ATOMIC_H__ */
M_uint32 M_atomic_sub_u32(volatile M_uint32 *ptr, M_uint32 val)
M_bool M_atomic_cas32(volatile M_uint32 *ptr, M_uint32 expected, M_uint32 newval)
M_uint64 M_atomic_inc_u64(volatile M_uint64 *ptr)
M_bool M_atomic_cas64(volatile M_uint64 *ptr, M_uint64 expected, M_uint64 newval)
M_uint32 M_atomic_inc_u32(volatile M_uint32 *ptr)
M_uint32 M_atomic_dec_u32(volatile M_uint32 *ptr)
M_uint64 M_atomic_add_u64(volatile M_uint64 *ptr, M_uint64 val)
M_uint32 M_atomic_add_u32(volatile M_uint32 *ptr, M_uint32 val)
M_uint64 M_atomic_dec_u64(volatile M_uint64 *ptr)
M_uint64 M_atomic_sub_u64(volatile M_uint64 *ptr, M_uint64 val)