| [906] | 1 | #include "TinyJS_Threading.h"
|
|---|
| 2 |
|
|---|
| 3 | #undef HAVE_THREADING
|
|---|
| 4 | #if !defined(NO_THREADING) && !defined(HAVE_CUSTOM_THREADING_IMPL)
|
|---|
| 5 | # define HAVE_THREADING
|
|---|
| 6 | # if defined(WIN32) && !defined(HAVE_PTHREAD)
|
|---|
| 7 | # include <windows.h>
|
|---|
| 8 | # else
|
|---|
| 9 | # include <pthread.h>
|
|---|
| 10 | # endif
|
|---|
| 11 | #endif
|
|---|
| 12 |
|
|---|
| 13 | #ifdef HAVE_THREADING
|
|---|
| 14 |
|
|---|
| 15 | #ifndef HAVE_PTHREAD
|
|---|
| 16 | // simple pthreads 4 windows
|
|---|
| 17 | # define pthread_t HANDLE
|
|---|
| 18 | # define pthread_create(t, stack, fnc, a) *(t) = CreateThread(NULL, stack, (LPTHREAD_START_ROUTINE)fnc, a, 0, NULL)
|
|---|
| 19 | # define pthread_join(t, v) WaitForSingleObject(t, INFINITE), GetExitCodeThread(t,(LPDWORD)v), CloseHandle(t)
|
|---|
| 20 |
|
|---|
| 21 | # define pthread_mutex_t HANDLE
|
|---|
| 22 | # define pthread_mutex_init(m, a) *(m) = CreateMutex(NULL, false, NULL)
|
|---|
| 23 | # define pthread_mutex_destroy(m) CloseHandle(*(m));
|
|---|
| 24 | # define pthread_mutex_lock(m) WaitForSingleObject(*(m), INFINITE);
|
|---|
| 25 | # define pthread_mutex_unlock(m) ReleaseMutex(*(m));
|
|---|
| 26 | #endif
|
|---|
| 27 |
|
|---|
| 28 | class CScriptMutex_impl : public CScriptMutex::CScriptMutex_t {
|
|---|
| 29 | public:
|
|---|
| 30 | CScriptMutex_impl() {
|
|---|
| 31 | pthread_mutex_init(&mutex, NULL);
|
|---|
| 32 | }
|
|---|
| 33 | ~CScriptMutex_impl() {
|
|---|
| 34 | pthread_mutex_destroy(&mutex);
|
|---|
| 35 | }
|
|---|
| 36 | void lock() {
|
|---|
| 37 | pthread_mutex_lock(&mutex);
|
|---|
| 38 | }
|
|---|
| 39 | void unlock() {
|
|---|
| 40 | pthread_mutex_unlock(&mutex);
|
|---|
| 41 | }
|
|---|
| 42 | pthread_mutex_t mutex;
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | CScriptMutex::CScriptMutex() {
|
|---|
| 46 | mutex = new CScriptMutex_impl;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | CScriptMutex::~CScriptMutex() {
|
|---|
| 50 | delete mutex;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | class CScriptThread_impl : public CScriptThread::CScriptThread_t {
|
|---|
| 54 | public:
|
|---|
| 55 | CScriptThread_impl(CScriptThread *_this) : activ(false), running(false), This(_this) {}
|
|---|
| 56 | ~CScriptThread_impl() {}
|
|---|
| 57 | void Run() {
|
|---|
| 58 | if(running) return;
|
|---|
| 59 | activ = true;
|
|---|
| 60 | pthread_create(&thread, NULL, (void*(*)(void*))ThreadFnc, this);
|
|---|
| 61 | while(!running);
|
|---|
| 62 | }
|
|---|
| 63 | int Stop() {
|
|---|
| 64 | if(!running) return -1;
|
|---|
| 65 | void *retvar;
|
|---|
| 66 | activ = false;
|
|---|
| 67 | pthread_join(thread, &retvar);
|
|---|
| 68 | running = false;
|
|---|
| 69 | return (int)retvar;
|
|---|
| 70 | }
|
|---|
| 71 | bool isActiv() { return activ; }
|
|---|
| 72 | static void *ThreadFnc(CScriptThread_impl *This) {
|
|---|
| 73 | This->running = true;
|
|---|
| 74 | return (void*) This->This->ThreadFnc();
|
|---|
| 75 | }
|
|---|
| 76 | bool activ;
|
|---|
| 77 | bool running;
|
|---|
| 78 | CScriptThread *This;
|
|---|
| 79 | pthread_t thread;
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | CScriptThread::CScriptThread() {
|
|---|
| 83 | thread = new CScriptThread_impl(this);
|
|---|
| 84 | }
|
|---|
| 85 | CScriptThread::~CScriptThread() {
|
|---|
| 86 | delete thread;
|
|---|
| 87 | }
|
|---|
| 88 | #endif // HAVE_THREADING
|
|---|