Nebula
Loading...
Searching...
No Matches
interlocked.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
11#include "core/types.h"
12
13//------------------------------------------------------------------------------
14namespace Gcc
15{
17{
18public:
20 static int Increment(int volatile* var);
22 static int Decrement(int volatile* var);
24 static long Increment(long volatile* var);
26 static long Decrement(long volatile* var);
28 static int Add(int volatile* var, int add);
30 static int Or(int volatile* var, int value);
32 static int And(int volatile* var, int value);
34 static int Xor(int volatile* var, int value);
36 static int Exchange(int volatile* dest, int value);
38 static void* ExchangePointer(void* volatile* dest, void* value);
40 static int CompareExchange(int volatile* dest, int exchange, int comparand);
42 static void* CompareExchangePointer(void* volatile* dest, void* exchange, void* comparand);
43};
44
45//------------------------------------------------------------------------------
48inline int
49GccInterlocked::Increment(int volatile* var)
50{
51 return __sync_add_and_fetch(var, 1);
52}
53
54//------------------------------------------------------------------------------
57inline int
58GccInterlocked::Decrement(int volatile* var)
59{
60 return __sync_sub_and_fetch(var, 1);
61}
62
63//------------------------------------------------------------------------------
66inline long
67GccInterlocked::Increment(long volatile* var)
68{
69 return __sync_add_and_fetch(var, 1);
70}
71
72//------------------------------------------------------------------------------
75inline long
76GccInterlocked::Decrement(long volatile* var)
77{
78 return __sync_sub_and_fetch(var, 1);
79}
80
81//------------------------------------------------------------------------------
84inline int
85GccInterlocked::Add(int volatile* var, int add)
86{
87 return __sync_fetch_and_add(var, add);
88}
89
90//------------------------------------------------------------------------------
93inline int
94GccInterlocked::Or(int volatile* var, int add)
95{
96 return __sync_fetch_and_or(var, add);
97}
98
99//------------------------------------------------------------------------------
102inline int
103GccInterlocked::And(int volatile* var, int add)
104{
105 return __sync_fetch_and_and(var, add);
106}
107
108//------------------------------------------------------------------------------
111inline int
112GccInterlocked::Xor(int volatile* var, int add)
113{
114 return __sync_fetch_and_xor(var, add);
115}
116
117//------------------------------------------------------------------------------
120inline int
121GccInterlocked::Exchange(int volatile* dest, int value)
122{
123 return __sync_lock_test_and_set(dest, value);
124}
125
126//------------------------------------------------------------------------------
129inline void*
130GccInterlocked::ExchangePointer(void* volatile* dest, void* value)
131{
132 return __sync_lock_test_and_set(dest, value);
133}
134
135//------------------------------------------------------------------------------
138inline int
139GccInterlocked::CompareExchange(int volatile* dest, int exchange, int comparand)
140{
141 return __sync_val_compare_and_swap(dest, comparand, exchange);
142}
143
144//------------------------------------------------------------------------------
147inline void*
148GccInterlocked::CompareExchangePointer(void* volatile* dest, void* exchange, void* comparand)
149{
150 return __sync_val_compare_and_swap(dest, comparand, exchange);
151}
152
153} // namespace Gcc
154//------------------------------------------------------------------------------
Provides simple atomic operations on shared variables using gcc compiler builtins.
Definition interlocked.h:17
static int Xor(int volatile *var, int value)
interlocked xor
Definition interlocked.h:112
static void * ExchangePointer(void *volatile *dest, void *value)
interlocked exchange
Definition interlocked.h:130
static int Or(int volatile *var, int value)
interlocked or
Definition interlocked.h:94
static int Decrement(int volatile *var)
interlocked decrement
Definition interlocked.h:58
static int Add(int volatile *var, int add)
interlocked add
Definition interlocked.h:85
static int CompareExchange(int volatile *dest, int exchange, int comparand)
interlocked compare-exchange
Definition interlocked.h:139
static int And(int volatile *var, int value)
interlocked and
Definition interlocked.h:103
static void * CompareExchangePointer(void *volatile *dest, void *exchange, void *comparand)
interlocked exchange
Definition interlocked.h:148
static int Exchange(int volatile *dest, int value)
interlocked exchange
Definition interlocked.h:121
static int Increment(int volatile *var)
interlocked increment
Definition interlocked.h:49
Definition interlocked.h:15