-
Notifications
You must be signed in to change notification settings - Fork 2
/
PKTaskQueue.h
226 lines (185 loc) · 6.11 KB
/
PKTaskQueue.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
215
216
217
218
219
220
221
222
223
224
225
226
/*
* PKTaskQueue.h
* PlayerKit
*
* Created by James MacWhinnie on 3/19/10.
* Copyright 2010 Roundabout Software. All rights reserved.
*
*/
#ifndef PKTaskQueue_h
#define PKTaskQueue_h
#include <queue>
#include <Block.h>
#include "RBObject.h"
#include "RBAtomic.h"
#include "RBException.h"
class CAPThread;
/*!
@class
@abstract The PKTaskQueue class is a simple FIFO task queue that executes tasks on a secondary worker thread.
@discussion Internally PKTaskQueue is simply an std::queue, an RBSemaphore, and a CAPThread. When a task
is added to a queue, it is injected into the std::queue of the receiver, and the RBSemaphore
is signalled to wake up the CAPThread.
*/
class PKTaskQueue : public RBLockableObject
{
public:
#pragma mark -Public
/*!
@typedef
@abstract The prototype functions must match to be used as tasks in PKTaskQueue.
@discussion Any C++ exceptions thrown by a TaskProc will be consumed and ignored.
*/
typedef void(*TaskProc)(void *userInfo);
/*!
@typedef
@abstract The prototype blocks must match to be used as tasks in PKTaskQueue.
@discussion Any C++ exceptions thrown by a TaskBlock will be consumed and ignored.
*/
typedef void(^TaskBlock)();
protected:
#pragma mark -Protected
/*!
@class
@abstract The QueuedTask functor class is used to represent tasks in PKTaskQueue's task-queue.
*/
class QueuedTask : public RBObject
{
protected:
TaskProc mImplementation;
void *mUserInfo;
RBSemaphore *mSignalSemaphore;
public:
#pragma mark Constructor/Destructor
/*!
@abstract The one and only constructor.
@param implementation The function that provides the implementation of this task
@param userInfo The value to pass to `implementation`
@param isSynchronous Whether or not this task is to be executed synchronously
@discussion Synchronous tasks have an internal semaphore that is signalled after their implementation
is applied. When a synchronous task is queued, `WaitForApplication` should be called on it.
*/
QueuedTask(TaskProc implementaton, void *userInfo, bool isSynchronous) :
RBObject("PKTaskQueue::QueuedTask"),
mImplementation(implementaton),
mUserInfo(userInfo),
mSignalSemaphore(NULL)
{
if(isSynchronous)
mSignalSemaphore = new RBSemaphore("PKTaskQueue::QueuedTask::mSignalSemaphore");
}
/*!
@abstract The one and only destructor.
@discussion If the task is synchronous this will signal its semaphore to prevent deadlock.
*/
~QueuedTask()
{
if(mSignalSemaphore)
{
mSignalSemaphore->SignalIf(^(int value) { return value > 1; });
delete mSignalSemaphore;
}
}
#pragma mark -
#pragma mark Overloads
/*!
@method
@abstract Apply the queued task's implementation.
@discussion If the receiver is synchronous, this will cause the receiver's semaphore to be signalled.
*/
void operator()()
{
mImplementation(mUserInfo);
if(mSignalSemaphore)
mSignalSemaphore->SignalIf(^(int value) { return value > 1; });
}
#pragma mark -
#pragma mark Waiting
/*!
@method
@abstract Block the calling thread until the receiver is applied.
@discussion This method does nothing for asynchronous tasks.
*/
void WaitForApplication() const throw(RBException)
{
if(mSignalSemaphore && (mSignalSemaphore->GetInstantaneousValue() == 1))
mSignalSemaphore->Wait();
}
};
/* owner */ const char *mName;
/* n/a */ std::queue</* strong */ QueuedTask *> mQueueTasks;
/* n/a */ RBAtomicBool mIsCancelled;
/* n/a */ RBSemaphore mSleepSemaphore;
/* weak */ CAPThread *mProcessingThread;
#pragma mark -
#pragma mark Processing Thread
/*!
@method
@abstract Start the receiver's processing thread if it isn't already running.
*/
void StartProcessingThread();
/*!
@method
@abstract Stop the receiver's processing thread if it's running.
*/
void StopProcessingThread();
#pragma mark -
/*!
@method
@abstract The implementation of the processing thread.
@discussion This method processes tasks for task queues.
*/
static void *ProcessingThreadCallback(PKTaskQueue *self);
public:
#pragma mark -Public
#pragma mark Constructor/Destructor
/*!
@abstract The one and only constructor.
*/
PKTaskQueue(const char *name = "");
/*!
@abstract The destructor.
*/
virtual ~PKTaskQueue();
#pragma mark -
#pragma mark Describing PKTaskQueue
virtual CFStringRef CopyDescription();
#pragma mark -
#pragma mark Queuing Tasks
/*!
@method
@abstract Queue up a task function for asynchronous execution
@param proc The function to execute asynchronously. May not be NULL.
@param userInfo The value to pass as the function's parameter.
@discussion If this method is called and no other tasks have been queued in the receiver, the receiver's
worker thread will be fired as a side effect of the method invocation.
*/
void Async(TaskProc proc, void *userInfo);
/*!
@method
@abstract Queue up a task block for asynchronous execution
@param block The block to execute asynchronously. May not be NULL.
@discussion If this method is called and no other tasks have been queued in the receiver, the receiver's
worker thread will be fired as a side effect of the method invocation.
*/
void Async(TaskBlock block);
#pragma mark -
/*!
@method
@abstract Queue up a task function for synchronous execution
@param proc The function to execute on the receiver's worker thread. May not be NULL.
@param userInfo The value to pass as the function's parameter.
@discussion If this method is called and no other tasks have been queued in the receiver, the receiver's
worker thread will be fired as a side effect of the method invocation.
*/
void Sync(TaskProc proc, void *userInfo);
/*!
@method
@abstract Queue up a task block for synchronous execution
@param proc The block to execute on the receiver's worker thread. May not be NULL.
@discussion If this method is called and no other tasks have been queued in the receiver, the receiver's
worker thread will be fired as a side effect of the method invocation.
*/
void Sync(TaskBlock block);
};
#endif /* PKTaskQueue_h */