Nebula
Loading...
Searching...
No Matches
interlocked.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12//------------------------------------------------------------------------------
13#include "core/types.h"
14#include "core/config.h"
15namespace Threading
16{
17
18using int64 = int64_t;
19typedef volatile int AtomicCounter;
20typedef volatile int64 AtomicCounter64;
21
22namespace Interlocked
23{
24using int64 = int64_t;
25
27int Add(int volatile* var, int add);
29int64 Add(int64 volatile* var, int64 add);
31int Or(int volatile* var, int value);
33int64 Or(int64 volatile* var, int64 value);
35int And(int volatile* var, int value);
37int64 And(int64 volatile* var, int64 value);
39int Xor(int volatile* var, int value);
41int64 Xor(int64 volatile* var, int64 value);
43int Exchange(int volatile* dest, int value);
45int64 Exchange(int64 volatile* dest, int64 value);
47int CompareExchange(int volatile* dest, int exchange, int comparand);
49int64 CompareExchange(int64 volatile* dest, int64 exchange, int64 comparand);
51void* ExchangePointer(void* volatile* dest, void* value);
53void* CompareExchangePointer(void* volatile* dest, void* exchange, void* comparand);
55int Increment(int volatile* var);
57int64 Increment(int64 volatile* var);
59int Decrement(int volatile* var);
61int64 Decrement(int64 volatile* var);
62
64{
66 int Add(int add)
67 {
68 Threading::Interlocked::Add((volatile int*)&this->value, add);
69 }
71 int Sub(int sub)
72 {
73 Threading::Interlocked::Add((volatile int*)&this->value, -sub);
74 }
76 int Or(int mask)
77 {
78 Threading::Interlocked::Or((volatile int*)&this->value, mask);
79 }
81 int And(int mask)
82 {
83 Threading::Interlocked::And((volatile int*)&this->value, mask);
84 }
86 int Exchange(int value)
87 {
88 Threading::Interlocked::Exchange((volatile int*)&this->value, value);
89 }
91 int CompareExchange(int exchange, int comparand)
92 {
93 Threading::Interlocked::CompareExchange((volatile int*)&this->value, exchange, comparand);
94 }
96 int Increment(int incr)
97 {
98 Threading::Interlocked::Increment((volatile int*)&this->value);
99 }
101 int Decrement(int decr)
102 {
103 Threading::Interlocked::Decrement((volatile int*)&this->value);
104 }
105
106private:
107 volatile int value;
108};
109
111{
114 {
115 Threading::Interlocked::Add((volatile int64*)&this->value, add);
116 }
119 {
120 Threading::Interlocked::Add((volatile int64*)&this->value, -sub);
121 }
124 {
125 Threading::Interlocked::Or((volatile int64*)&this->value, mask);
126 }
129 {
130 Threading::Interlocked::And((volatile int64*)&this->value, mask);
131 }
134 {
135 Threading::Interlocked::Exchange((volatile int64*)&this->value, value);
136 }
138 int64 CompareExchange(int64 exchange, int64 comparand)
139 {
140 Threading::Interlocked::CompareExchange((volatile int64*)&this->value, exchange, comparand);
141 }
144 {
145 Threading::Interlocked::Increment((volatile int64*)&this->value);
146 }
149 {
150 Threading::Interlocked::Decrement((volatile int64*)&this->value);
151 }
152private:
153 volatile int64 value;
154};
155
157{
159 void* Exchange(void* value)
160 {
161 return Threading::Interlocked::ExchangePointer((void* volatile*)&this->ptr, value);
162 }
164 void* CompareExchange(void* exchange, void* comparand)
165 {
166 return Threading::Interlocked::CompareExchangePointer((void* volatile*)&this->ptr, exchange, comparand);
167 }
168private:
169 volatile void* ptr;
170};
171
172
173} // namespace Interlocked
174
175} // namespace Threading
Provide simple atomic operations on memory variables.
Nebula compiler specific defines and configuration.
int Decrement(int volatile *var)
interlocked decrement, return result
Definition gccinterlocked.cc:157
int And(int volatile *var, int add)
interlocked and
Definition gccinterlocked.cc:58
int Or(int volatile *var, int add)
interlocked or
Definition gccinterlocked.cc:40
void * ExchangePointer(void *volatile *dest, void *value)
interlocked exchange
Definition gccinterlocked.cc:130
int Add(int volatile *var, int add)
interlocked add
Definition gccinterlocked.cc:22
int Exchange(int volatile *dest, int value)
interlocked exchange
Definition gccinterlocked.cc:94
int Xor(int volatile *var, int add)
interlocked xor
Definition gccinterlocked.cc:76
int CompareExchange(int volatile *dest, int exchange, int comparand)
interlocked compare-exchange
Definition gccinterlocked.cc:112
int Increment(int volatile *var)
interlocked increment, return result
Definition gccinterlocked.cc:148
void * CompareExchangePointer(void *volatile *dest, void *exchange, void *comparand)
interlocked compare-exchange pointer
Definition gccinterlocked.cc:139
The Jobs2 system provides a set of threads and a pool of jobs from which threads can pickup work.
Definition jobs2.h:16
volatile int64 AtomicCounter64
Definition interlocked.h:20
volatile int AtomicCounter
Definition interlocked.h:19
Definition interlocked.h:111
int64 And(int64 mask)
And.
Definition interlocked.h:128
int64 Increment(int64 incr)
Increment and return new value.
Definition interlocked.h:143
int64 Add(int64 add)
Add.
Definition interlocked.h:113
int64 CompareExchange(int64 exchange, int64 comparand)
Compare and exchange.
Definition interlocked.h:138
int64 Sub(int64 sub)
Subtract.
Definition interlocked.h:118
volatile int64 value
Definition interlocked.h:153
int64 Decrement(int64 decr)
Decrement and return new value.
Definition interlocked.h:148
int64 Or(int64 mask)
Or.
Definition interlocked.h:123
int64 Exchange(int64 value)
Exchange.
Definition interlocked.h:133
Definition interlocked.h:64
volatile int value
Definition interlocked.h:107
int Increment(int incr)
Increment and return new value.
Definition interlocked.h:96
int Exchange(int value)
Exchange.
Definition interlocked.h:86
int Or(int mask)
Or.
Definition interlocked.h:76
int Decrement(int decr)
Decrement and return new value.
Definition interlocked.h:101
int CompareExchange(int exchange, int comparand)
Compare and exchange.
Definition interlocked.h:91
int Sub(int sub)
Subtract.
Definition interlocked.h:71
int And(int mask)
And.
Definition interlocked.h:81
int Add(int add)
Add.
Definition interlocked.h:66
Definition interlocked.h:157
volatile void * ptr
Definition interlocked.h:169
void * CompareExchange(void *exchange, void *comparand)
Compare and exchange.
Definition interlocked.h:164
void * Exchange(void *value)
Exchange.
Definition interlocked.h:159
int64_t int64
Definition types.h:37