-
Notifications
You must be signed in to change notification settings - Fork 2
/
PKTaskQueue.cpp
215 lines (162 loc) · 4.82 KB
/
PKTaskQueue.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
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
215
/*
* PKTaskQueue.cpp
* PlayerKit
*
* Created by James MacWhinnie on 3/19/10.
* Copyright 2010 Roundabout Software. All rights reserved.
*
*/
#include "PKTaskQueue.h"
#include "CAPThread.h"
#include <dlfcn.h>
//We load the objc_[un]registerThreadWithCollector functions at runtime
//so we don't have to link the objc runtime in PlayerKit.
typedef void(*ObjCGarbageCollectorProc)();
static ObjCGarbageCollectorProc objc_registerThreadWithCollector = (ObjCGarbageCollectorProc)dlsym(RTLD_DEFAULT, "objc_registerThreadWithCollector");
static ObjCGarbageCollectorProc objc_unregisterThreadWithCollector = (ObjCGarbageCollectorProc)dlsym(RTLD_DEFAULT, "objc_unregisterThreadWithCollector");
#pragma mark PKTaskQueue
static void TaskBlockForwarder(void *userInfo)
{
PKTaskQueue::TaskBlock block = (PKTaskQueue::TaskBlock)userInfo;
block();
Block_release(block);
}
#pragma mark -
#pragma mark Constructor/Destructor
PKTaskQueue::PKTaskQueue(const char *name) :
RBLockableObject("PKTaskQueue"),
mName(strdup(name)),
mQueueTasks(),
mIsCancelled(false),
mSleepSemaphore("PKTaskQueue::mSleepSemaphore"),
mProcessingThread(NULL)
{
}
PKTaskQueue::~PKTaskQueue()
{
free((void *)(mName));
mName = NULL;
}
#pragma mark -
#pragma mark Describing PKTaskQueue
CFStringRef PKTaskQueue::CopyDescription()
{
return CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("<%s:%p %s>"), mClassName, this, mName);
}
#pragma mark -
#pragma mark Processing Thread
void PKTaskQueue::StartProcessingThread()
{
Acquisitor lock(this);
mIsCancelled = false;
if(!mProcessingThread)
{
mProcessingThread = new CAPThread(CAPThread::ThreadRoutine(&ProcessingThreadCallback), //in threadRoutine
this, //in threadRoutineUserInfo
CAPThread::kMaxThreadPriority, //in priority
true, //in isPriorityFixed?
true); //in shouldAutoDelete?
mProcessingThread->Start();
}
}
void PKTaskQueue::StopProcessingThread()
{
mIsCancelled = true;
mSleepSemaphore.SignalIf(^(int value) { return value > 1; });
}
#pragma mark -
void *PKTaskQueue::ProcessingThreadCallback(PKTaskQueue *self)
{
//Prevent «auto_zone_thread_registration_error» issues caused by `TaskBlockForwarder`.
if(objc_registerThreadWithCollector) objc_registerThreadWithCollector();
self->Retain();
pthread_setname_np(self->mName);
while (!self->mIsCancelled)
{
while (self->mQueueTasks.size() > 0)
{
QueuedTask *topTask = NULL;
{
Acquisitor lock(self);
topTask = self->mQueueTasks.front();
self->mQueueTasks.pop();
}
try
{
(*topTask)();
}
catch (...)
{
std::cerr << "An unexpected exception was raised in the PKTaskQueue named " << self->mName << std::endl;
}
topTask->Release();
}
try
{
self->mSleepSemaphore.Wait();
}
catch (RBException e)
{
std::cerr << "mSleepSemaphore.Wait failed with error code " << e.GetCode() << ". Aborting." << std::endl;
}
}
self->mProcessingThread = NULL;
self->Release();
if(objc_unregisterThreadWithCollector) objc_unregisterThreadWithCollector();
return NULL;
}
#pragma mark -
#pragma mark Queuing Tasks
void PKTaskQueue::Async(TaskProc proc, void *userInfo)
{
RBParameterAssert(proc);
Acquisitor lock(this);
//Start the processing thread if it hasn't been already
if(!mProcessingThread)
this->StartProcessingThread();
QueuedTask *task = new QueuedTask(proc, userInfo, true);
mQueueTasks.push(task);
//Prevent «auto_zone_thread_registration_error» issues caused by `RBSemaphore::SignalIf`.
if(objc_registerThreadWithCollector) objc_registerThreadWithCollector();
mSleepSemaphore.SignalIf(^(int value) { return value > 0; });
}
void PKTaskQueue::Async(TaskBlock block)
{
RBParameterAssert(block);
this->Async(&TaskBlockForwarder, Block_copy(block));
}
#pragma mark -
void PKTaskQueue::Sync(TaskProc proc, void *userInfo)
{
RBParameterAssert(proc);
Acquisitor lock(this);
//Start the processing thread if it hasn't been already
if(!mProcessingThread)
this->StartProcessingThread();
QueuedTask *task = new QueuedTask(proc, userInfo, true);
//We retain the task so it can't be destroyed out from underneath us if it's processed and released
task->Retain();
mQueueTasks.push(task);
//Prevent «auto_zone_thread_registration_error» issues caused by `RBSemaphore::SignalIf`.
if(objc_registerThreadWithCollector) objc_registerThreadWithCollector();
try
{
mSleepSemaphore.SignalIf(^(int value) { return value > 0; });
//We relinquish `lock` before waiting for a signal so we don't cause a deadlock
lock.Relinquish();
task->WaitForApplication();
}
catch (...)
{
//Balance the above retain
task->Release();
throw;
}
//Balance the above retain
task->Release();
}
void PKTaskQueue::Sync(TaskBlock block)
{
RBParameterAssert(block);
this->Sync(&TaskBlockForwarder, Block_copy(block));
}