summaryrefslogtreecommitdiff
path: root/milkway/mw-atomic.h
blob: dac3f7857407e767fe07ac2ea9fbbd3a6f7cf479 (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
/* flib - fundamental c library.
 *
 * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef F_ATOMIC_H
#define F_ATOMIC_H

#include <milkway/mw-types.h>

/**
 * @file mw-atomic.h
 * @brief Basic atomic integer and pointer operations
 * @defgroup atomic Basic atomic integer and pointer operations
 * @ingroup fcore
 * @{
 */

MW_BEGIN_DECLS

/**
 * @brief Reads the value of a integer by atomic.
 *
 * Reads the value of the integer pointed to by atomic. Issues
 * memory barrier as needed.
 *
 *  @param atomic a pointer to a integer.
 */
mw_public int
mw_atomic_get (const volatile int * atomic);

/**
 * @brief Sets the value of a integer by atomic.
 *
 * Sets the value of the integer pointed to by atomic. Issues
 * memory barrier as needed.
 *
 * @param atomic a pointer to a integer.
 * @param val the value to set.
 */
mw_public void
mw_atomic_set (volatile int * atomic, int val);

/**
 * @brief Adds a value to a integer by atomic.
 *
 * @param atomic a pointer to a integer.
 * @param count the value
 */
mw_public int
mw_atomic_add (volatile int * atomic, int count);

/**
 * @brief  Subs a value to a integer by atomic.
 *
 * @param atomic a pointer to a integer.
 * @param count the value
 */
mw_public int
mw_atomic_sub (volatile int * atomic, int count);

/**
 * @brief Subs a value to a integer by atomic and returns MW_TRUE if
 * result is zero.
 *
 * @param atomic a pointer to a integer.
 * @param count the value
 * @return MW_TRUE if result is zero
 */
mw_public mw_bool_t
mw_atomic_sub_and_test (volatile int * atomic, int count);

/**
 * @brief Compares oldval with the integer pointed to by atomic and if
 * they are equal, atomically exchanges atomic with newval. Issues
 * memory barrier as needed.
 *
 * @param atomic a pointer to a integer.
 * @param oldval the old value
 * @param newval the new value
 * @return MW_TRUE if oldval is equal to *atomic
 */
mw_public mw_bool_t
mw_atomic_cmpxchg (volatile int * atomic,
                  int oldval, int newval);

/**
 * @brief Sets the value of a pointer by atomic.
 *
 * @param atomic a pointer to a pointer
 * @param ptr the value to set
 */
mw_public void
mw_atomic_pointer_set (volatile mw_pointer_t * atomic,
		       mw_pointer_t            ptr);

/**
 * @brief Reads the value of a pointer by atomic.
 *
 * @param atomic a pointer to a pointer
 * @return returns the value of *atomic
 */
mw_public mw_pointer_t
mw_atomic_pointer_get (volatile mw_pointer_t * atomic);

/**
 * @brief Compares oldval with the pointer pointed to by atomic and if
 * they are equal, atomically exchanges atomic with newval. Issues
 * memory barrier as needed.
 *
 * @param atomic a pointer to a pointer.
 * @param oldval the old value
 * @param newval the new value
 * @return MW_TRUE if oldval is equal to *atomic
 */
mw_public mw_bool_t
mw_atomic_pointer_cmpxchg (volatile mw_pointer_t * atomic,
                          mw_pointer_t            oldval,
                          mw_pointer_t            newval);

MW_END_DECLS

/**
 * @}
 */

#endif