forked from decarbonization/PlayerKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBAtomic.cpp
74 lines (59 loc) · 1.58 KB
/
RBAtomic.cpp
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
/*
* RBAtomic.cpp
* PlayerKit
*
* Created by Peter MacWhinnie on 7/8/09.
* Copyright 2009 Roundabout Software. All rights reserved.
*
*/
#include "RBAtomic.h"
#include <mach/mach_init.h>
const RBAtomicBool RBAtomicBool::True(true);
const RBAtomicBool RBAtomicBool::False(false);
#pragma mark RBSemaphore
#pragma mark Constructor/Destructor
RBSemaphore::RBSemaphore(const char *name) :
RBObject("RBSemaphore"),
mMachSemaphore(NULL),
mValue(0)
{
int errorCode = semaphore_create(mach_task_self(), &mMachSemaphore, SYNC_POLICY_FIFO, 0);
RBAssertNoErr(errorCode, CFSTR("semaphore_create failed with error %d"), errorCode);
}
RBSemaphore::~RBSemaphore()
{
if(mMachSemaphore)
{
semaphore_destroy(mach_task_self(), mMachSemaphore);
mMachSemaphore = NULL;
}
}
#pragma mark -
#pragma mark Signalling
void RBSemaphore::Wait() throw(RBException)
{
mValue.Increment();
int errorCode = semaphore_wait(mMachSemaphore);
RBAssertNoErr(errorCode, CFSTR("semaphore_wait failed with error code %d"), errorCode);
}
void RBSemaphore::Signal() throw(RBException)
{
mValue.Decrement();
int errorCode = semaphore_signal(mMachSemaphore);
RBAssertNoErr(errorCode, CFSTR("semaphore_signal failed with error code %d"), errorCode);
}
void RBSemaphore::SignalIf(bool(^predicate)(int value)) throw(RBException)
{
if(predicate(this->GetInstantaneousValue()))
this->Signal();
}
#pragma mark -
#pragma mark Properties
int RBSemaphore::GetInstantaneousValue() const throw(RBException)
{
return mValue.GetValue();
}
semaphore_t RBSemaphore::GetMachSemaphore() const throw()
{
return mMachSemaphore;
}