(svn r28004) -Update from Eints:
[openttd.git] / src / thread / thread_os2.cpp
blobc66e2ad6430abf45b2284fa7bb66f871cbc62de5
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file thread_os2.cpp OS/2 implementation of Threads. */
12 #include "../stdafx.h"
13 #include "thread.h"
15 #define INCL_DOS
16 #include <os2.h>
17 #include <process.h>
19 #include "../safeguards.h"
21 /**
22 * OS/2 version for ThreadObject.
24 class ThreadObject_OS2 : public ThreadObject {
25 private:
26 TID thread; ///< System thread identifier.
27 OTTDThreadFunc proc; ///< External thread procedure.
28 void *param; ///< Parameter for the external thread procedure.
29 bool self_destruct; ///< Free ourselves when done?
31 public:
32 /**
33 * Create a thread and start it, calling proc(param).
35 ThreadObject_OS2(OTTDThreadFunc proc, void *param, bool self_destruct) :
36 thread(0),
37 proc(proc),
38 param(param),
39 self_destruct(self_destruct)
41 thread = _beginthread(stThreadProc, NULL, 1048576, this);
44 /* virtual */ bool Exit()
46 _endthread();
47 return true;
50 /* virtual */ void Join()
52 DosWaitThread(&this->thread, DCWW_WAIT);
53 this->thread = 0;
55 private:
56 /**
57 * On thread creation, this function is called, which calls the real startup
58 * function. This to get back into the correct instance again.
60 static void stThreadProc(void *thr)
62 ((ThreadObject_OS2 *)thr)->ThreadProc();
65 /**
66 * A new thread is created, and this function is called. Call the custom
67 * function of the creator of the thread.
69 void ThreadProc()
71 /* Call the proc of the creator to continue this thread */
72 try {
73 this->proc(this->param);
74 } catch (OTTDThreadExitSignal e) {
75 } catch (...) {
76 NOT_REACHED();
79 if (self_destruct) {
80 this->Exit();
81 delete this;
86 /* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
88 ThreadObject *to = new ThreadObject_OS2(proc, param, thread == NULL);
89 if (thread != NULL) *thread = to;
90 return true;
93 /**
94 * OS/2 version of ThreadMutex.
96 class ThreadMutex_OS2 : public ThreadMutex {
97 private:
98 HMTX mutex; ///< The mutex.
99 HEV event; ///< Event for waiting.
100 uint recursive_count; ///< Recursive lock count.
102 public:
103 ThreadMutex_OS2() : recursive_count(0)
105 DosCreateMutexSem(NULL, &mutex, 0, FALSE);
106 DosCreateEventSem(NULL, &event, 0, FALSE);
109 /* virtual */ ~ThreadMutex_OS2()
111 DosCloseMutexSem(mutex);
112 DosCloseEventSem(event);
115 /* virtual */ void BeginCritical(bool allow_recursive = false)
117 /* os2 mutex is recursive by itself */
118 DosRequestMutexSem(mutex, (unsigned long) SEM_INDEFINITE_WAIT);
119 this->recursive_count++;
120 if (!allow_recursive && this->recursive_count != 1) NOT_REACHED();
123 /* virtual */ void EndCritical(bool allow_recursive = false)
125 if (!allow_recursive && this->recursive_count != 1) NOT_REACHED();
126 this->recursive_count--;
127 DosReleaseMutexSem(mutex);
130 /* virtual */ void WaitForSignal()
132 assert(this->recursive_count == 1); // Do we need to call Begin/EndCritical multiple times otherwise?
133 this->EndCritical();
134 DosWaitEventSem(event, SEM_INDEFINITE_WAIT);
135 this->BeginCritical();
138 /* virtual */ void SendSignal()
140 DosPostEventSem(event);
144 /* static */ ThreadMutex *ThreadMutex::New()
146 return new ThreadMutex_OS2();