summaryrefslogtreecommitdiff
path: root/src/Task.cc
blob: 85cdafef62656596c877ac4dff1a109dd0d210e0 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#ifdef _MSC_VER
#include <windows.h> // for CALLBACK
#endif
#ifndef _WIN32
#define CALLBACK
#endif

#include "Task.h"
#include "TaskMan.h"
#include "Exceptions.h"
#include "Printer.h"
#include "Local.h"
#include "Main.h"

typedef void (*trampoline_t)(void *);

static trampoline_t s_trampoline = NULL;

namespace {

class RegisterTrampoline : public Balau::AtStart {
  public:
      RegisterTrampoline() : AtStart(0) { }
    void doStart() {
        Balau::Task::registerTrampoline();
    }
};

static RegisterTrampoline registerTrampoline;

}

static void CALLBACK trampoline(void * arg) {
    s_trampoline(arg);
}

void Balau::Task::registerTrampoline() {
    AAssert(s_trampoline == NULL, "Don't call registerTrampoline directly");
    s_trampoline = coroutineTrampoline;
}

static Balau::LocalTmpl<Balau::Task> localTask;

Balau::Task::Task() : m_status(STARTING), m_okayToEAgain(false), m_stackless(false) {
    Printer::elog(E_TASK, "Created a Task at %p", this);
    m_tls = Local::createTLS(g_tlsManager->getTLS());
}

bool Balau::Task::needsStacks() {
#ifndef _WIN32
    return true;
#else
    return false;
#endif
}

void Balau::Task::setup(TaskMan * taskMan, void * stack) {
    if (m_stackless) {
        IAssert(!stack, "Since we're stackless, no stack should've been allocated.");
        m_stack = NULL;
    } else {
        size_t size = stackSize();
#ifndef _WIN32
        IAssert(stack, "Can't setup a coroutine without a stack");
        m_stack = stack;
        coro_create(&m_ctx, trampoline, this, m_stack, size);
#else
        IAssert(!stack, "We shouldn't allocate stacks with Fibers");
        m_stack = NULL;
        m_fiber = CreateFiber(size, trampoline, this);
#endif
    }

    m_taskMan = taskMan;

    void * oldTLS = g_tlsManager->getTLS();
    g_tlsManager->setTLS(m_tls);
    localTask.set(this);
    g_tlsManager->setTLS(oldTLS);
}

Balau::Task::~Task() {
    free(m_tls);
}

void Balau::Task::coroutineTrampoline(void * arg) {
    Task * task = reinterpret_cast<Task *>(arg);
    IAssert(task, "We didn't get a task to trampoline from... ?");
    task->coroutine();
}

void Balau::Task::coroutine() {
    try {
        IAssert((m_status == STARTING) || (m_stackless && (m_status == RUNNING)), "The Task at %p has a bad status ? m_status = %s, stackless = %s", this, StatusToString(m_status), m_stackless ? "true" : "false");
        m_status = RUNNING;
        Do();
        m_status = STOPPED;
    }
    catch (Exit & e) {
        m_status = STOPPED;
        TaskMan::stop(e.getCode());
    }
    catch (TestException & e) {
        m_status = STOPPED;
        Printer::log(M_ERROR, "Unit test failed: %s", e.getMsg());
        const char * details = e.getDetails();
        if (details)
            Printer::log(M_ERROR, "  %s", details);
        auto trace = e.getTrace();
        for (String & str : trace)
            Printer::log(M_ERROR, "%s", str.to_charp());
        TaskMan::stop(-1);
    }
    catch (RessourceException & e) {
        m_status = STOPPED;
        Printer::log(M_ERROR, "Got a ressource exhaustion problem: %s", e.getMsg());
        const char * details = e.getDetails();
        if (details)
            Printer::log(M_ERROR, "  %s", details);
        auto trace = e.getTrace();
        for (String & str : trace)
            Printer::log(M_DEBUG, "%s", str.to_charp());
        TaskMan::stop(-1);
    }
    catch (TaskSwitch & e) {
        if (!m_stackless) {
            Printer::log(M_ERROR, "Task %s at %p isn't stackless, but still caused a task switch.", getName(), this);
            const char * details = e.getDetails();
            if (details)
                Printer::log(M_ERROR, "  %s", details);
            auto trace = e.getTrace();
            for (String & str : trace)
                Printer::log(M_DEBUG, "%s", str.to_charp());
            m_status = FAULTED;
        } else {
            Printer::elog(E_TASK, "Stackless task %s at %p is task-switching.", getName(), this);
        }
    }
    catch (EAgain & e) {
        Printer::log(M_ERROR, "Task %s at %p threw an EAgain - you should catch it and yield; the app will crash now", getName(), this);
        m_status = FAULTED;
    }
    catch (GeneralException & e) {
        Printer::log(M_WARNING, "Task %s at %p caused an exception: `%s' - stopping.", getName(), this, e.getMsg());
        const char * details = e.getDetails();
        if (details)
            Printer::log(M_WARNING, "  %s", details);
        auto trace = e.getTrace();
        for (String & str : trace)
            Printer::log(M_DEBUG, "%s", str.to_charp());
        m_status = FAULTED;
    }
    catch (...) {
        Printer::log(M_WARNING, "Task %s at %p caused an unknown exception - stopping.", getName(), this);
        m_status = FAULTED;
    }
    if (!m_stackless) {
#ifndef _WIN32
        coro_transfer(&m_ctx, &m_taskMan->m_returnContext);
#else
        SwitchToFiber(m_taskMan->m_fiber);
#endif
    }
}

void Balau::Task::switchTo() {
    Printer::elog(E_TASK, "Switching to task %p - %s", this, getName());
    IAssert(m_status == YIELDED || m_status == SLEEPING || m_status == STARTING, "The task at %p isn't either yielded, sleeping or starting... ? m_status = %s", this, StatusToString(m_status));
    void * oldTLS = g_tlsManager->getTLS();
    g_tlsManager->setTLS(m_tls);
    if (m_status == YIELDED || m_status == SLEEPING)
        m_status = RUNNING;
    if (m_stackless) {
        coroutine();
    } else {
#ifndef _WIN32
        coro_transfer(&m_taskMan->m_returnContext, &m_ctx);
#else
        SwitchToFiber(m_fiber);
#endif
    }
    g_tlsManager->setTLS(oldTLS);
    IAssert(m_status != RUNNING, "Task %s at %p is still running... ?", getName(), this);
}

bool Balau::Task::yield(bool stillRunning) {
    Printer::elog(E_TASK, "Task %p - %s yielding", this, getName());
    if (stillRunning)
        m_status = YIELDED;
    else
        m_status = SLEEPING;
    if (m_stackless) {
        return true;
    } else {
#ifndef _WIN32
        coro_transfer(&m_ctx, &m_taskMan->m_returnContext);
#else
        SwitchToFiber(m_taskMan->m_fiber);
#endif
    }
    return false;
}

Balau::Task * Balau::Task::getCurrentTask() {
    return localTask.get();
}

void Balau::Task::waitFor(Balau::Events::BaseEvent * e) {
    Printer::elog(E_TASK, "Task %p - %s waits for event %p (%s)", this, getName(), e, ClassName(e).c_str());
    e->registerOwner(this);
}

struct ev_loop * Balau::Task::getLoop() {
    return getTaskMan()->getLoop();
}

void Balau::Task::sleep(double timeout) {
    AAssert(!m_stackless && !m_okayToEAgain, "You can only call sleep on simple tasks.");
    Events::Timeout evt(timeout);
    waitFor(&evt);
    yield();
}

void Balau::Events::BaseEvent::doSignal() {
    Printer::elog(E_EVENT, "Event at %p (%s) is signaled with cb %p and task %p", this, ClassName(this).c_str(), m_cb, m_task);
    m_signal = true;
    if (m_cb)
        m_cb->gotEvent(this);
    if (m_task) {
        Printer::elog(E_EVENT, "Signaling task %p (%s - %s)", m_task, m_task->getName(), ClassName(m_task).c_str());
        m_task->getTaskMan()->signalTask(m_task);
    }
}

Balau::Events::TaskEvent::TaskEvent(Task * taskWaited) : m_taskWaited(NULL), m_ack(false), m_distant(false) {
    if (taskWaited)
        attachToTask(taskWaited);
}

void Balau::Events::TaskEvent::attachToTask(Task * taskWaited) {
    AAssert(!m_taskWaited, "You can't attach a TaskEvent twice.");
    m_ack = false;
    m_taskWaited = taskWaited;
    ScopeLock lock(m_taskWaited->m_eventLock);
    m_taskWaited->m_waitedBy.push_back(this);
}

void Balau::Events::TaskEvent::signal() {
    if (m_distant)
        m_evt.send();
    else
        doSignal();
}

void Balau::Events::TaskEvent::gotOwner(Task * task) {
    TaskMan * tm = task->getTaskMan();

    m_evt.stop();
    if (tm != m_taskWaited->getTaskMan()) {
        m_evt.set(tm->getLoop());
        m_evt.set<TaskEvent, &TaskEvent::evt_cb>(this);
        m_evt.start();
        m_distant = true;
    } else {
        m_distant = false;
    }
}

Balau::Events::TaskEvent::~TaskEvent() {
    if (!m_ack)
        ack();
    if (m_distant)
        m_evt.stop();
}

void Balau::Events::TaskEvent::ack() {
    AAssert(!m_ack, "You can't ack() a task event twice.");
    bool deleted = false;
    Task * t = m_taskWaited;
    t->m_eventLock.enter();
    for (auto i = t->m_waitedBy.begin(); i != t->m_waitedBy.end(); i++) {
        if (*i == this) {
            t->m_waitedBy.erase(i);
            deleted = true;
            break;
        }
    }
    Printer::elog(E_EVENT, "TaskEvent at %p being ack; removing from the 'waited by' list of %p (%s - %s); deleted = %s", this, t, t->getName(), ClassName(t).c_str(), deleted ? "true" : "false");
    t->m_eventLock.leave();
    t = NULL;
    IAssert(deleted, "We didn't find task %p in the waitedBy lists... ?", this);
    m_ack = true;
    reset();
    m_taskWaited = NULL;
}

void Balau::Events::Timeout::gotOwner(Task * task) {
    m_evt.set(task->getLoop());
    m_evt.start();
}

void Balau::Events::Async::gotOwner(Task * task) {
    m_evt.set(task->getLoop());
    m_evt.start();
}

void Balau::Events::Custom::gotOwner(Task * task) {
    m_loop = task->getLoop();
}

void Balau::Task::operationYield(Events::BaseEvent * evt, enum OperationYieldType yieldType) throw (GeneralException) {
    Task * t = getCurrentTask();
    if (evt)
        t->waitFor(evt);

    if (t->m_stackless) {
        AAssert(yieldType != SIMPLE, "You can't run simple operations from a stackless task.");
    }

    if (yieldType == STACKLESS) {
        AAssert(t->m_okayToEAgain, "You can't run a stackless operation from a non-okay-to-eagain task.");
    }

    bool gotSignal, doEAgain = false;

    do {
        doEAgain = t->yield(evt == NULL);
        static const char * YieldTypeToString[] = {
            "SIMPLE",
            "INTERRUPTIBLE",
            "STACKLESS",
        };
        if (doEAgain)
            Printer::elog(E_TASK, "operation couldn't yield; yieldType = %s; okayToEAgain = %s", YieldTypeToString[yieldType], t->m_okayToEAgain ? "true" : "false");
        else
            Printer::elog(E_TASK, "operation back from yielding; yieldType = %s; okayToEAgain = %s", YieldTypeToString[yieldType], t->m_okayToEAgain ? "true" : "false");
        gotSignal = evt ? evt->gotSignal() : true;
    } while (((yieldType == SIMPLE) || !t->m_okayToEAgain) && !gotSignal);

    if (!evt)
        return;

    if ((yieldType != SIMPLE) && t->m_okayToEAgain && !gotSignal && doEAgain) {
        AAssert(!t->m_cannotEAgain, "task at %p in simple context mode can't EAgain", t);
        Printer::elog(E_TASK, "operation is throwing an EAgain exception with event %p", evt);
        throw EAgain(evt);
    }
}

void Balau::QueueBase::iPush(void * t, Events::Async * event) {
    ScopeLock sl(m_lock);
    Cell * c = new Cell(t);
    c->m_prev = m_back;
    if (m_back)
        m_back->m_next = c;
    else
        m_front = c;
    m_back = c;
    if (event)
        event->trigger();
    else
        pthread_cond_signal(&m_cond);
}

void * Balau::QueueBase::iPop(Events::Async * event, bool wait) {
    ScopeLock sl(m_lock);
    while (!m_front && wait) {
        if (event) {
            Task::prepare(event);
            m_lock.leave();
            Task::operationYield(event, Task::INTERRUPTIBLE);
            m_lock.enter();
            event->resetMaybe();
        } else {
            pthread_cond_wait(&m_cond, &m_lock.m_lock);
        }
    }
    Cell * c = m_front;
    if (!c)
        return NULL;
    m_front = c->m_next;
    if (m_front)
        m_front->m_prev = NULL;
    else
        m_back = NULL;
    void * t = c->m_elem;
    delete c;
    return t;
}