aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/threads/thread.cpp
blob: 1c35efbb7d14b0f9411cc58d6c0b7a8b2cae14ec (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
#include "../core/configuration.h"
#if defined(jin_thread)

#include "thread.h"

namespace JinEngine
{
    namespace Threads
    {

        class Mutex
        {
        public:
            Mutex();
            ~Mutex();

            void lock();
            void unlock();
        private:
        #if jin_thread == jin_thread_sdl
            SDL_mutex* mutex;
        #endif 
            friend class Conditional;
        };

        // ̼߳signal wait
        class Conditional
        {
        public:
            Conditional();
            ~Conditional();
            void signal();
            void broadcast();
            bool wait(Mutex* mutex, int timeout = -1);
        private:
        #if jin_thread == jin_thread_sdl
            SDL_cond* cond;
        #endif
        };

        class Lock
        {
        public:
            Lock(Mutex* m) : mutex(m) {
                mutex->lock();
            }

            Lock(Mutex& m) : mutex(&m) {
                mutex->lock();
            }

            ~Lock() {
                mutex->unlock();
            }
        private:
            Mutex* mutex;

            Lock(Lock&) {}

        };

        //////////////////////////////////////////////////////////////////////
    
        Mutex::Mutex()
        {
        #if jin_thread == jin_thread_sdl
            mutex = SDL_CreateMutex();
        #endif
        }

        Mutex::~Mutex()
        {
        #if jin_thread == jin_thread_sdl
            SDL_DestroyMutex(mutex);
        #endif
        }

        void Mutex::lock()
        {
        #if jin_thread == jin_thread_sdl
            SDL_LockMutex(mutex);
        #endif
        }

        void Mutex::unlock()
        {
        #if jin_thread == jin_thread_sdl
            SDL_UnlockMutex(mutex);
        #endif
        }

        //////////////////////////////////////////////////////////////////////

        Conditional::Conditional()
        {
        #if jin_thread == jin_thread_sdl
            cond = SDL_CreateCond();
        #endif
        }

        Conditional::~Conditional()
        {
        #if jin_thread == jin_thread_sdl
            SDL_DestroyCond(cond);
        #endif
        }

        void Conditional::signal()
        {
        #if jin_thread == jin_thread_sdl
            SDL_CondSignal(cond);
        #endif
        }

        void Conditional::broadcast()
        {
        #if jin_thread == jin_thread_sdl
            SDL_CondBroadcast(cond);
        #endif
        }

        bool Conditional::wait(Mutex* mutex, int timeout)
        {
        #if jin_thread == jin_thread_sdl
            if (timeout < 0)
                return !SDL_CondWait(cond, mutex->mutex);
            else
                return (SDL_CondWaitTimeout(cond, mutex->mutex, timeout) == 0);
        #endif
        }
    
        //////////////////////////////////////////////////////////////////////

        Thread::ThreadData::ThreadData(Mutex* m, Conditional* c)
            : mutex(m)
            , condition(c)
            , share()
        {
        }

        Thread::ThreadData::~ThreadData()
        {
        }

        void Thread::ThreadData::set(int slot, Variant value)
        {
            Lock l(mutex);
            share[slot] = value;
        }

        Thread::Variant Thread::ThreadData::get(int slot)
        {
            Lock l(mutex);
            return share[slot];
        }

        bool Thread::ThreadData::exist(int slot)
        {
            Lock l(mutex);
            return share.count(slot) == 1;
        }

        void Thread::ThreadData::remove(int slot)
        {
            Lock l(mutex);
            if (exist(slot))
            {
                share.erase(slot);
            }
        }

        //////////////////////////////////////////////////////////////////////

        Thread::Thread(const std::string tname, ThreadRunner runner)
            : name(tname)
            , running(false)
            , threadRunner(runner)
        {
            mutex = new Mutex();
            condition = new Conditional();
            common = new Thread::ThreadData(mutex, condition);
        }

        Thread::~Thread()
        {
        #if jin_thread == jin_thread_sdl
        #endif
        }

        const char* Thread::getName()
        {
            Lock l(mutex);
            return name.c_str();
        };
    
        bool Thread::isRunning()
        {
            Lock l(mutex);
            return running;
        };

        bool Thread::start(void* p)
        {
            Lock l(mutex);
            if (running) 
                return false;
            if (handle)
            {
            #if jin_thread == jin_thread_sdl
                SDL_WaitThread(handle, nullptr);
            #endif
            }
        #if jin_thread == jin_thread_sdl
            handle = SDL_CreateThread(threadRunner, name.c_str(), p);
        #elif jin_thread == jin_thread_cpp
            handle = new std::thread();
        #endif 
            return (running = (handle != nullptr));
        }

        void Thread::wait()
        {
            {
                Lock l(mutex);
                if (!handle)
                    return;
            }
        #if jin_thread == jin_thread_sdl
            SDL_WaitThread(handle, nullptr);
        #endif
            Lock l(mutex);
            running = false; 
            handle = nullptr;
        }

        void Thread::lock()
        {
            if (mutex != nullptr)
                mutex->lock();
        }

        void Thread::unlock()
        {
            if (mutex != nullptr)
                mutex->unlock();
        }

        void Thread::send(int slot, const Variant& value)
        {
            lock(); 
            common->set(slot, value);
            unlock();
            condition->broadcast();
        }

        bool Thread::receive(int slot)
        {
            return common->exist(slot);
        }

        Thread::Variant Thread::fetch(int slot)
        {
            Thread::Variant v = common->get(slot);
            return v;
        }

        Thread::Variant Thread::demand(int slot)
        {
            /**
             * pthread_mutex_lock(mtx);
             * while(pass == 0) 
             * {
             *     pthread_mutex_unlock(mtx);
             *     pthread_cond_just_wait(cv);
             *     pthread_mutex_lock(mtx);
             * }
             * pthread_mutex_unlock(mtx);
             */
            lock();
            while (!common->exist(slot))
            {
                if (common->exist(ThreadData::SLOT_ERROR))
                    return 0;
                condition->wait(mutex);
            }
            Thread::Variant v = common->get(slot);
            unlock();
            return v;
        }

        void Thread::remove(int slot)
        {
            lock(); 
            common->remove(slot);
            unlock();
        }

    } // namespace Threads
} // namespace JinEngine

#endif // defined(jin_thread)