aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/basetypes/MCOperationQueue.cc
blob: 2e7eaf869db6240101da383f60724e732108a4ae (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
#include "MCOperationQueue.h"

#include "MCOperation.h"
#include "MCOperationCallback.h"
#include "MCMainThread.h"
#include "MCUtils.h"
#include "MCArray.h"
#include "MCLog.h"
#include "MCAutoreleasePool.h"
#include <libetpan/libetpan.h>
#include <thread>

using namespace mailcore;

OperationQueue::OperationQueue()
{
	mOperations = new Array();
	mStarted = false;
    mWaiting = false;
    mOperationSem = mailsem_new();
    mStartSem = mailsem_new();
    mStopSem = mailsem_new();
    mWaitingFinishedSem = mailsem_new();
}

OperationQueue::~OperationQueue()
{
    MC_SAFE_RELEASE(mOperations);
    mailsem_free(mOperationSem);
    mailsem_free(mStartSem);
    mailsem_free(mStopSem);
    mailsem_free(mWaitingFinishedSem);
}

void OperationQueue::addOperation(Operation * op)
{
	mLock.lock();
    mOperations->addObject(op);
	mLock.unlock();
    mailsem_up(mOperationSem);
    startThread();
}

void OperationQueue::runOperations()
{
    MCLog("start thread");
    mailsem_up(mStartSem);
    
    while (true) {
        Operation * op = NULL;
        bool needsCheckRunning = false;
        bool quitting;
        
        AutoreleasePool * pool = new AutoreleasePool();
        
        mailsem_down(mOperationSem);
        
		mLock.lock();
        if (mOperations->count() > 0) {
            op = (Operation *) mOperations->objectAtIndex(0);
        }
        quitting = mQuitting;
		mLock.unlock();

        MCLog("quitting %i %p", mQuitting, op);
        if ((op == NULL) && quitting) {
            MCLog("stopping %p", this);
            mailsem_up(mStopSem);
            pool->release();
            break;
        }

        op->main();
        
        op->retain()->autorelease();
        
		mLock.lock();
        mOperations->removeObjectAtIndex(0);
        if (mOperations->count() == 0) {
            if (mWaiting) {
                mailsem_up(mWaitingFinishedSem);
            }
            needsCheckRunning = true;
        }
		mLock.unlock();
        
        if (!op->isCancelled()) {
            if (op->callback() != NULL) {
                performMethodOnMainThread((Object::Method) &OperationQueue::callbackOnMainThread, op, true);
            }
        }
        
        if (needsCheckRunning) {
            retain(); // (1)
            MCLog("check running %p", this);
            performMethodOnMainThread((Object::Method) &OperationQueue::checkRunningOnMainThread, this);
        }
        
        pool->release();
    }
    MCLog("cleanup thread %p", this);
}

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

void OperationQueue::checkRunningOnMainThread(void * context)
{
    performMethodAfterDelay((Object::Method) &OperationQueue::checkRunningAfterDelay, NULL, 1);
}

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

void OperationQueue::startThread()
{
    if (mStarted)
        return;
    
    mQuitting = false;
    mStarted = true;
	std::thread t([=]()
	{
		runOperations();
	});
	t.detach();
    mailsem_down(mStartSem);
}

unsigned int OperationQueue::count()
{
    unsigned int count;
    
	mLock.lock();
    count = mOperations->count();
	mLock.unlock();
    
    return count;
}

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