forked from decarbonization/PlayerKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBAtomic.h
214 lines (176 loc) · 4 KB
/
RBAtomic.h
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* RBAtomic.h
* PlayerKit
*
* Created by Peter MacWhinnie on 7/8/09.
* Copyright 2009 Roundabout Software. All rights reserved.
*
*/
#ifndef RBAtomic_h
#define RBAtomic_h 1
#include <CoreFoundation/CoreFoundation.h>
#include <libKern/OSAtomic.h>
#include <mach/semaphore.h>
#include <mach/task.h>
#include "RBObject.h"
#include "RBLockableObject.h"
#include "RBException.h"
/*!
@class
@abstract This class represents a simple atomic counter.
@discussion This class is used to keep the number of active samples
synchronized between scheduled audio slices in PKAudioPlayerEngine.
*/
class RBAtomicCounter : public RBLockableObject
{
int32_t mValue;
public:
/*!
@ctor
@abstract Create an atomic counter with a default value of 0.
*/
RBAtomicCounter(int value = 0) :
RBLockableObject("RBAtomicCounter"),
mValue(value)
{
}
/*!
@method
@abstract Increment the receiver.
*/
void Increment()
{
OSAtomicIncrement32Barrier(&mValue);
}
/*!
@method
@abstract Decrement the receiver.
*/
void Decrement()
{
OSAtomicDecrement32Barrier(&mValue);
}
/*!
@method
@abstract Add n to the receiver.
*/
void Add(int32_t value)
{
OSAtomicAdd32Barrier(value, &mValue);
}
/*!
@method
@abstract Set the value of the receiver.
*/
void SetValue(int32_t value)
{
OSAtomicCompareAndSwap32Barrier(mValue, value, &mValue);
}
/*!
@method
@abstract Get the value of the receiver.
*/
int32_t GetValue() const
{
OSMemoryBarrier();
return mValue;
}
virtual bool IsEqualTo(RBAtomicCounter *other)
{
return this->GetValue() == other->GetValue();
}
};
#pragma mark -
/*!
@class
@abstract RBAtomicBool is a drop in replacement for the built-in C++ bool type
that uses atomic reads and writes.
*/
class RBAtomicBool : public RBLockableObject
{
protected:
int32_t mValue;
public:
//! @abstract The default constructor.
RBAtomicBool(bool value = false) :
RBLockableObject("RBAtomicBool"),
mValue(value? 1 : 0)
{
}
//! @abstract The copy constructor.
RBAtomicBool(RBAtomicBool &other) :
RBLockableObject("RBAtomicBool"),
mValue(other.GetValue()? 1 : 0)
{
}
//! @abstract Set the value of an atomic bool.
void SetValue(bool value)
{
int32_t newValue = value? 1 : 0;
OSAtomicCompareAndSwap32Barrier(mValue, newValue, &mValue);
}
//! @abstract Get the value of an atomic bool.
bool GetValue() const
{
OSMemoryBarrier();
return (mValue == 1)? true : false;
}
virtual bool IsEqualTo(RBAtomicBool *other)
{
return this->GetValue() == other->GetValue();
}
#pragma mark -
//! @abstract Convert an atomic bool into a plain bool.
operator bool() const
{
return GetValue();
}
//! @abstract Assign a new value to an atomic bool.
RBAtomicBool &operator=(bool value)
{
this->SetValue(value);
return *this;
}
//! @abstract Assign a new value to an atomic bool.
RBAtomicBool &operator=(RBAtomicBool &value)
{
this->SetValue(value.GetValue());
return *this;
}
//! @abstract Whether or not the receiver is equal to another bool.
bool operator==(RBAtomicBool &other) const
{
return GetValue() == other.GetValue();
}
//! @abstract Whether or not the receiver is not equal to another bool.
bool operator!=(RBAtomicBool &other) const
{
return GetValue() == other.GetValue();
}
#pragma mark -
//! @abstract The True atomic bool constant.
static const RBAtomicBool True;
//! @abstract The False atomic bool constant.
static const RBAtomicBool False;
};
#pragma mark -
class RBSemaphore : public RBObject
{
protected:
semaphore_t mMachSemaphore;
RBAtomicCounter mValue;
public:
#pragma mark Constructor/Destructor
RBSemaphore(const char *name = "");
virtual ~RBSemaphore();
#pragma mark -
#pragma mark Signalling
void Wait() throw(RBException);
void Signal() throw(RBException);
void SignalIf(bool(^predicate)(int value)) throw(RBException);
#pragma mark -
#pragma mark Properties
int GetInstantaneousValue() const throw(RBException);
semaphore_t GetMachSemaphore() const throw();
};
#endif /* RBAtomic_h */