Lo2kThread.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of Micole Architecture
00003  *
00004  * Copyright (C) 2007 Micole partners
00005  *
00006  * Micole Architecture is free software: you can redistribute it 
00007  * and/or modify it under the terms of the GNU Lesser General 
00008  * Public License as published by the Free Software Foundation, 
00009  * either version 3 of the License, or (at your option) any 
00010  * later version.
00011  *
00012  * Micole Architecture is distributed in the hope that it will be 
00013  * useful, * but WITHOUT ANY WARRANTY; without even the implied 
00014  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00015  * PURPOSE.  See the GNU Lesser General Public License for more 
00016  * details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Micole Architecture.  If not, see
00020  * <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 #ifndef LO2K_THREAD_H
00024 #define LO2K_THREAD_H
00025 
00026 #include <vtplayer.h>
00027 
00028 #ifdef _WIN32
00029 #ifndef _WINDOWS_
00030 /*#define _WINSOCKAPI_
00031 #define WIN32_LEAN_AND_MEAN
00032 #include <windows.h>*/
00033 #endif
00034 #else
00035 #error _WIN32 must be defined before you include thread.h
00036 #endif
00037 namespace lo2k
00038 {
00039 
00043 class Thread
00044 {
00045         #ifdef _WIN32
00046                 static DWORD WINAPI ThreadFunc(LPVOID pv)
00047                 {
00048                 try
00049                 {
00050                         (reinterpret_cast<Thread *>(pv))->run();
00051                 }
00052                 catch(...)
00053                 {
00054                 }
00055                 return 0;
00056                 }
00057         #elif defined(__sun)
00058         #else
00059         #endif
00060         public:
00061                 #ifdef _WIN32
00062                         typedef DWORD threadid;
00063                 #elif defined(__sun)
00064                 #else
00065                 #endif
00066                 
00070                 Thread()
00071                 {
00072                 }
00073                 
00077                 virtual ~Thread()
00078                 {
00079                 }
00080 
00085                 static threadid getthreadid()
00086                 {
00087                         #ifdef _WIN32
00088                         return ::GetCurrentThreadId();
00089                         #elif defined(__sun)
00090                         #else
00091                         #endif
00092                 }
00093 
00098                 static void sleep(long milliseconds=1)
00099                 {
00100                         #ifdef _WIN32
00101                         ::Sleep(milliseconds);
00102                         #elif defined(__sun)
00103                         #else
00104                         #endif
00105                 }
00106 
00110                 threadid start()
00111                 {
00112                         threadid id;
00113                         #ifdef _WIN32
00114                         ::CreateThread(NULL, 0, ThreadFunc, this, 0, &id);
00115                         #elif defined(__sun)
00116                         #else
00117                         #endif
00118                         return id;
00119                 }
00120                 
00126                 virtual void run()=0;
00127 };
00128 
00129 
00130 
00134 class VTPLAYER_API CriticalSection
00135 {
00136         #ifdef _WIN32
00137         typedef CRITICAL_SECTION cs;
00138         #elif defined(__sun)
00139         #else
00140         #endif
00141 private:
00142 
00143         cs m_cs;
00144         static CriticalSection *_instance;
00145 
00146 
00147         CriticalSection()
00148         {
00149                 #ifdef _WIN32
00150                 ::InitializeCriticalSection(&m_cs);
00151                 #elif defined(__sun)
00152                 #else
00153                 #endif
00154         }
00155 
00156 public:
00157 
00158         static CriticalSection * get()
00159         {
00160                 if (_instance == NULL)
00161                         _instance = new CriticalSection;
00162                 return _instance;
00163         }
00164 
00165 
00166         ~CriticalSection()
00167         {
00168                 #ifdef _WIN32
00169                 ::DeleteCriticalSection(&m_cs);
00170                 #elif defined(__sun)
00171                 #else
00172                 #endif
00173         }
00174         void enter()
00175         {
00176                 #ifdef _WIN32
00177                 ::EnterCriticalSection(&m_cs);
00178                 #elif defined(__sun)
00179                 #else
00180                 #endif
00181         }
00182         void leave()
00183         {
00184                 #ifdef _WIN32
00185                 ::LeaveCriticalSection(&m_cs);
00186                 #elif defined(__sun)
00187                 #else
00188                 #endif
00189         }
00190 
00191 };
00192 
00193 
00194 
00195 };
00196 
00197 #endif

Generated on Tue Oct 16 17:10:42 2007 for Micole by  doxygen 1.4.7