aboutsummaryrefslogtreecommitdiff
path: root/src/lua/thread/Thread.h
blob: 63f75249e8d8a9cc8a718368fe7dfa4f921526bb (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
#include "libjin/jin.h"
#include "../luaopen_types.h"

namespace jin
{
namespace lua
{
namespace thread
{

    class Thread : public Object
    {
    public:
        typedef jin::thread::Thread::Variant Variant;

        Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner)
            : name(_name)
            , code(_code)
        {
            thread = new jin::thread::Thread(_name, runner);
        }

        bool start()
        {
            return thread->start();
        }

        void wait()
        {
            thread->wait();
        }

        void send(int slot, const Variant& value)
        {
            thread->send(slot, value);
        }

        bool receive(int slot)
        {
            return thread->receive(slot);
        }

        Variant fetch(int slot)
        {
            return thread->fetch(slot);
        }

        Variant demand(int slot)
        {
            return thread->demand(slot);
        }

        void remove(int slot)
        {
            thread->remove(slot);
        }

        const char* getName()
        {
            return name.c_str();
        }

        bool isRunning()
        {
            return thread->isRunning();
        }

        void lock()
        {
            thread->lock();
        }

        void unlock()
        {
            thread->unlock();
        }

        static void threadRunner(jin::thread::Thread* t);

    private:
        ~Thread()
        {
            delete thread;
        }

        jin::thread::Thread* thread;

        const std::string name;
        const std::string code;
    };

} // thread
} // lua
} // jin