thread_os2.cpp

Go to the documentation of this file.
00001 /* $Id: thread_os2.cpp 15764 2009-03-18 19:47:05Z rubidium $ */
00002 
00005 #include "stdafx.h"
00006 #include "thread.h"
00007 
00008 #define INCL_DOS
00009 #include <os2.h>
00010 #include <process.h>
00011 
00015 class ThreadObject_OS2 : public ThreadObject {
00016 private:
00017   TID thread;          
00018   OTTDThreadFunc proc; 
00019   void *param;         
00020   bool self_destruct;  
00021 
00022 public:
00026   ThreadObject_OS2(OTTDThreadFunc proc, void *param, bool self_destruct) :
00027     thread(0),
00028     proc(proc),
00029     param(param),
00030     self_destruct(self_destruct)
00031   {
00032     thread = _beginthread(stThreadProc, NULL, 32768, this);
00033   }
00034 
00035   /* virtual */ bool Exit()
00036   {
00037     _endthread();
00038     return true;
00039   }
00040 
00041   /* virtual */ void Join()
00042   {
00043     DosWaitThread(&this->thread, DCWW_WAIT);
00044     this->thread = 0;
00045   }
00046 private:
00051   static void stThreadProc(void *thr)
00052   {
00053     ((ThreadObject_OS2 *)thr)->ThreadProc();
00054   }
00055 
00060   void ThreadProc()
00061   {
00062     /* Call the proc of the creator to continue this thread */
00063     try {
00064       this->proc(this->param);
00065     } catch (OTTDThreadExitSignal e) {
00066     } catch (...) {
00067       NOT_REACHED();
00068     }
00069 
00070     if (self_destruct) {
00071       this->Exit();
00072       delete this;
00073     }
00074   }
00075 };
00076 
00077 /* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
00078 {
00079   ThreadObject *to = new ThreadObject_OS2(proc, param, thread == NULL);
00080   if (thread != NULL) *thread = to;
00081   return true;
00082 }
00083 
00087 class ThreadMutex_OS2 : public ThreadMutex {
00088 private:
00089   HMTX mutex;
00090 
00091 public:
00092   ThreadMutex_OS2()
00093   {
00094     DosCreateMutexSem(NULL, &mutex, 0, FALSE);
00095   }
00096 
00097   /* virtual */ ~ThreadMutex_OS2()
00098   {
00099     DosCloseMutexSem(mutex);
00100   }
00101 
00102   /* virtual */ void BeginCritical()
00103   {
00104     DosRequestMutexSem(mutex, (unsigned long) SEM_INDEFINITE_WAIT);
00105   }
00106 
00107   /* virtual */ void EndCritical()
00108   {
00109     DosReleaseMutexSem(mutex);
00110   }
00111 };
00112 
00113 /* static */ ThreadMutex *ThreadMutex::New()
00114 {
00115   return new ThreadMutex_OS2();
00116 }

Generated on Fri Jul 31 22:33:19 2009 for OpenTTD by  doxygen 1.5.6