summaryrefslogtreecommitdiff
path: root/src/LuaTask.cc
blob: 5c035212f399fcb7700a76b0fb6c2d1f87580f40 (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
#include "LuaTask.h"
#include "Main.h"
#include "TaskMan.h"
#include "Printer.h"

namespace {

class LuaTaskStopper : public Balau::LuaExecCell {
    virtual void run(Balau::Lua &) { }
};

};

Balau::LuaExecCell::LuaExecCell() {
    Printer::elog(E_TASK, "LuaExecCell created at %p", this);
}

Balau::LuaMainTask::LuaMainTask() {
    Printer::elog(E_TASK, "LuaMainTask created at %p", this);
    L.open_base();
    L.open_table();
    L.open_string();
    L.open_math();
    L.open_debug();
    L.open_bit();
    L.open_jit();
    L.open_ffi();
}

Balau::LuaMainTask::~LuaMainTask() {
    L.close();
}

void Balau::LuaMainTask::exec(LuaExecCell * cell) {
    Printer::elog(E_TASK, "LuaMainTask at %p is asked to queue Cell %p", this, cell);
    m_queue.push(cell);
}

void Balau::LuaMainTask::stop() {
    Printer::elog(E_TASK, "LuaMainTask at %p asked to stop", this);
    exec(new LuaTaskStopper());
}

void Balau::LuaMainTask::Do() {
    LuaExecCell * cell = NULL;
    for (;;) {
        Printer::elog(E_TASK, "LuaMainTask at %p tries to pop an ExecCell", this);
        try {
            cell = m_queue.pop();
        }
        catch (Balau::EAgain & e) {
            taskSwitch();
        }
        Printer::elog(E_TASK, "LuaMainTask at %p popped %p", this, cell);
        if (dynamic_cast<LuaTaskStopper *>(cell)) {
            Printer::elog(E_TASK, "LuaMainTask at %p is stopping", this);
            delete cell;
            return;
        }
        TaskMan::registerTask(new LuaTask(L.thread(), cell), this);
    }
}

void Balau::LuaTask::Do() {
    while(true) {
        try {
            if (L.yielded())
                L.resume();
            else
                m_cell->run(L);
        }
        catch (GeneralException & e) {
            m_cell->m_exception = new GeneralException(e);
        }
        catch (...) {
            m_cell->setError();
        }
        if (L.yielded()) {
            yield();
            continue;
        }
        if (m_cell->m_detached)
            delete m_cell;
        else
            m_cell->m_event.trigger();
        break;
    }
}

void Balau::LuaExecCell::exec(LuaMainTask * mainTask) {
    if (!m_detached)
        Task::prepare(&m_event);
    mainTask->exec(this);
    if (!m_detached)
        Task::operationYield(&m_event);
}

void Balau::LuaExecCell::throwError() throw (GeneralException) {
    if (!gotError())
        return;

    if (m_exception) {
        GeneralException copy(*m_exception);
        delete m_exception;
        m_exception = NULL;
        throw copy;
    } else {
        throw GeneralException("Unknown error");
    }
}

void Balau::LuaExecString::run(Lua & L) {
    L.load(m_str);
}

void Balau::LuaExecFile::run(Lua & L) {
    L.load(m_file);
}