aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/basetypes/MCOperationQueue.cpp
blob: 0652eb75296336cd97de020c88d5ec61173e9a41 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "MCOperationQueue.h"

#include <libetpan/libetpan.h>

#include "MCOperation.h"
#include "MCOperationCallback.h"
#include "MCOperationQueueCallback.h"
#include "MCMainThread.h"
#include "MCUtils.h"
#include "MCArray.h"
#include "MCLog.h"
#include "MCAutoreleasePool.h"
#include "MCMainThreadAndroid.h"
#include "MCAssert.h"

using namespace mailcore;

OperationQueue::OperationQueue()
{
    mOperations = new Array();
    mStarted = false;
    pthread_mutex_init(&mLock, NULL);
    mWaiting = false;
    mOperationSem = mailsem_new();
    mStartSem = mailsem_new();
    mStopSem = mailsem_new();
    mWaitingFinishedSem = mailsem_new();
    mQuitting = false;
    mCallback = NULL;
#if __APPLE__
    mDispatchQueue = dispatch_get_main_queue();
#endif
    _pendingCheckRunning = false;
}

OperationQueue::~OperationQueue()
{
#if __APPLE__
    if (mDispatchQueue != NULL) {
        dispatch_release(mDispatchQueue);
    }
#endif
    MC_SAFE_RELEASE(mOperations);
    pthread_mutex_destroy(&mLock);
    mailsem_free(mOperationSem);
    mailsem_free(mStartSem);
    mailsem_free(mStopSem);
    mailsem_free(mWaitingFinishedSem);
}

void OperationQueue::addOperation(Operation * op)
{
    pthread_mutex_lock(&mLock);
    mOperations->addObject(op);
    pthread_mutex_unlock(&mLock);
    mailsem_up(mOperationSem);
    startThread();
}

void OperationQueue::cancelAllOperations()
{
    pthread_mutex_lock(&mLock);
    for (unsigned int i = 0 ; i < mOperations->count() ; i ++) {
        Operation * op = (Operation *) mOperations->objectAtIndex(i);
        op->cancel();
    }
    pthread_mutex_unlock(&mLock);
}

void OperationQueue::runOperationsOnThread(OperationQueue * queue)
{
    queue->runOperations();
}

void OperationQueue::runOperations()
{
#if defined(__ANDROID) || defined(ANDROID)
    androidSetupThread();
#endif

    MCLog("start thread");
    mailsem_up(mStartSem);
    
    while (true) {
        Operation * op = NULL;
        bool needsCheckRunning = false;
        bool quitting;
        
        AutoreleasePool * pool = new AutoreleasePool();
        
        mailsem_down(mOperationSem);
        
        pthread_mutex_lock(&mLock);
        if (mOperations->count() > 0) {
            op = (Operation *) mOperations->objectAtIndex(0);
        }
        quitting = mQuitting;
        pthread_mutex_unlock(&mLock);

        //MCLog("quitting %i %p", mQuitting, op);
        if ((op == NULL) && quitting) {
            MCLog("stopping %p", this);
            mailsem_up(mStopSem);
            
            retain(); // (2)
#if __APPLE__
            performMethodOnDispatchQueue((Object::Method) &OperationQueue::stoppedOnMainThread, NULL, mDispatchQueue, true);
#else
            performMethodOnMainThread((Object::Method) &OperationQueue::stoppedOnMainThread, NULL, true);
#endif
            
            pool->release();
            break;
        }

        MCAssert(op != NULL);
        performOnCallbackThread(op, (Object::Method) &OperationQueue::beforeMain, op, true);
        
        if (!op->isCancelled() || op->shouldRunWhenCancelled()) {
            op->main();
        }
        
        op->retain()->autorelease();
        
        pthread_mutex_lock(&mLock);
        mOperations->removeObjectAtIndex(0);
        if (mOperations->count() == 0) {
            if (mWaiting) {
                mailsem_up(mWaitingFinishedSem);
            }
            needsCheckRunning = true;
        }
        pthread_mutex_unlock(&mLock);
        
        if (!op->isCancelled()) {
            performOnCallbackThread(op, (Object::Method) &OperationQueue::callbackOnMainThread, op, true);
        }
        
        if (needsCheckRunning) {
            retain(); // (1)
            //MCLog("check running %p", this);
#if __APPLE__
            performMethodOnDispatchQueue((Object::Method) &OperationQueue::checkRunningOnMainThread, this, mDispatchQueue);
#else
            performMethodOnMainThread((Object::Method) &OperationQueue::checkRunningOnMainThread, this);
#endif
        }
        
        pool->release();
    }
    MCLog("cleanup thread %p", this);
#if defined(__ANDROID) || defined(ANDROID)
    androidUnsetupThread();
#endif
}

void OperationQueue::performOnCallbackThread(Operation * op, Method method, void * context, bool waitUntilDone)
{
#if __APPLE__
    dispatch_queue_t queue = op->callbackDispatchQueue();
    if (queue == NULL) {
        queue = dispatch_get_main_queue();
    }
    performMethodOnDispatchQueue(method, context, queue, waitUntilDone);
#else
    performMethodOnMainThread(method, context, waitUntilDone);
#endif
}

void OperationQueue::beforeMain(Operation * op)
{
    op->beforeMain();
}

void OperationQueue::callbackOnMainThread(Operation * op)
{
    op->afterMain();
    
    if (op->isCancelled())
        return;
    
    if (op->callback() != NULL) {
        op->callback()->operationFinished(op);
    }
}

void OperationQueue::checkRunningOnMainThread(void * context)
{
    retain(); // (4)
    if (_pendingCheckRunning) {
#if __APPLE__
        cancelDelayedPerformMethodOnDispatchQueue((Object::Method) &OperationQueue::checkRunningAfterDelay, NULL, mDispatchQueue);
#else
        cancelDelayedPerformMethod((Object::Method) &OperationQueue::checkRunningAfterDelay, NULL);
#endif
        release(); // (4)
    }
    _pendingCheckRunning = true;
    
#if __APPLE__
    performMethodOnDispatchQueueAfterDelay((Object::Method) &OperationQueue::checkRunningAfterDelay, NULL, mDispatchQueue, 1);
#else
    performMethodAfterDelay((Object::Method) &OperationQueue::checkRunningAfterDelay, NULL, 1);
#endif

    release(); // (1)
}

void OperationQueue::checkRunningAfterDelay(void * context)
{
    _pendingCheckRunning = false;
    pthread_mutex_lock(&mLock);
    if (!mQuitting) {
        if (mOperations->count() == 0) {
            MCLog("trying to quit %p", this);
            mailsem_up(mOperationSem);
            mQuitting = true;
        }
    }
    pthread_mutex_unlock(&mLock);
    
    // Number of operations can't be changed because it runs on main thread.
    // And addOperation() should also be called from main thread.
    
    release(); // (4)
}

void OperationQueue::stoppedOnMainThread(void * context)
{
    MCLog("thread stopped %p", this);
    mailsem_down(mStopSem);
    mStarted = false;
    
    if (mCallback) {
        mCallback->queueStoppedRunning();
    }
    
    if (mOperations->count() > 0) {
        //Operations have been added while thread was quitting, so restart automatically
        startThread();
    }

    release(); // (2)

    release(); // (3)
}

void OperationQueue::startThread()
{
    if (mStarted)
        return;
    
    if (mCallback) {
        mCallback->queueStartRunning();
    }
    
    retain(); // (3)
    mQuitting = false;
    mStarted = true;
    pthread_create(&mThreadID, NULL, (void * (*)(void *)) OperationQueue::runOperationsOnThread, this);
    pthread_detach(mThreadID);
    mailsem_down(mStartSem);
}

unsigned int OperationQueue::count()
{
    unsigned int count;
    
    pthread_mutex_lock(&mLock);
    count = mOperations->count();
    pthread_mutex_unlock(&mLock);
    
    return count;
}

void OperationQueue::setCallback(OperationQueueCallback * callback)
{
    mCallback = callback;
}

OperationQueueCallback * OperationQueue::callback()
{
    return mCallback;
}

#if 0
void OperationQueue::waitUntilAllOperationsAreFinished()
{
    bool waiting = false;
    
    pthread_mutex_lock(&mLock);
    if (mOperations->count() > 0) {
        mWaiting = true;
        waiting = true;
    }
    pthread_mutex_unlock(&mLock);
    
    if (waiting) {
        sem_wait(&mWaitingFinishedSem);
    }
    mWaiting = false;
}
#endif

#if __APPLE__
void OperationQueue::setDispatchQueue(dispatch_queue_t dispatchQueue)
{
    if (mDispatchQueue != NULL) {
        dispatch_release(mDispatchQueue);
    }
    mDispatchQueue = dispatchQueue;
    if (mDispatchQueue != NULL) {
        dispatch_retain(mDispatchQueue);
    }
}

dispatch_queue_t OperationQueue::dispatchQueue()
{
    return mDispatchQueue;
}
#endif