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/>.
10 /** @file thread_os2.cpp OS/2 implementation of Threads. */
12 #include "../stdafx.h"
19 #include "../safeguards.h"
22 * OS/2 version for ThreadObject.
24 class ThreadObject_OS2
: public ThreadObject
{
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?
33 * Create a thread and start it, calling proc(param).
35 ThreadObject_OS2(OTTDThreadFunc proc
, void *param
, bool self_destruct
) :
39 self_destruct(self_destruct
)
41 thread
= _beginthread(stThreadProc
, NULL
, 1048576, this);
44 /* virtual */ bool Exit()
50 /* virtual */ void Join()
52 DosWaitThread(&this->thread
, DCWW_WAIT
);
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();
66 * A new thread is created, and this function is called. Call the custom
67 * function of the creator of the thread.
71 /* Call the proc of the creator to continue this thread */
73 this->proc(this->param
);
74 } catch (OTTDThreadExitSignal e
) {
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
;
94 * OS/2 version of ThreadMutex.
96 class ThreadMutex_OS2
: public ThreadMutex
{
98 HMTX mutex
; ///< The mutex.
99 HEV event
; ///< Event for waiting.
100 uint recursive_count
; ///< Recursive lock count.
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?
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();