PF_API 0.52

Android/jni/Libs/Utils_LIB/UThread.h

Go to the documentation of this file.
00001 
00002 //    Copyright (C) 2006-2009 Dylan Blair
00003 //
00004 //    email: dblair@alumni.cs.utexas.edu
00005 //
00006 //    This library is free software; you can redistribute it and/or
00007 //    modify it under the terms of the GNU Lesser General Public
00008 //    License as published by the Free Software Foundation; either
00009 //    version 2.1 of the License, or (at your option) any later version.
00010 //
00011 //    This library is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 //    Lesser General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU Lesser General Public
00017 //    License along with this library; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 #ifndef UCRITICAL_SECTION_H
00022 #define UCRITICAL_SECTION_H
00023 
00024 #ifdef _WIN32
00025 #include <windows.h>
00026 #else
00027 #include <pthread.h>
00028 #include <semaphore.h>
00029 #endif
00030 
00031 #include <assert.h>
00032 
00033 namespace OpenSkyNet {
00034     namespace Utils {
00035         class LockableCS {
00036         public:
00037 #ifdef _WIN32
00038 
00039             inline LockableCS() throw() { InitializeCriticalSection(&_cS); }
00040             inline ~LockableCS() throw() { DeleteCriticalSection(&_cS); }
00041 
00042             inline void lock() throw() { EnterCriticalSection(&_cS); }
00043             inline void unlock() throw() { LeaveCriticalSection(&_cS); }
00044 #else
00045 
00046             inline LockableCS() throw() {
00047                 pthread_mutexattr_t mutexAttr;
00048                 pthread_mutexattr_settype(&mutexAttr,PTHREAD_MUTEX_RECURSIVE_NP);
00049                 pthread_mutex_init(&_cS,&mutexAttr);
00050                 pthread_mutexattr_destroy(&mutexAttr);
00051             }
00052             inline ~LockableCS() throw() { pthread_mutex_destroy(&_cS); }
00053 
00054             inline void lock() throw() { pthread_mutex_lock(&_cS); }
00055             inline void unlock() throw() { pthread_mutex_unlock(&_cS); }
00056 #endif
00057         private:
00058 #ifdef _WIN32
00059             CRITICAL_SECTION _cS;
00060 #else
00061             pthread_mutex_t _cS;
00062 #endif
00063         };
00064 
00065         class SyncEvent {
00066         public:
00067 #ifdef _WIN32
00068             inline SyncEvent() throw() { _event = CreateEvent(NULL,FALSE,FALSE,NULL); }
00069             inline ~SyncEvent() throw() { CloseHandle(_event); }
00070 
00071             inline void set() throw() { SetEvent(_event); }
00072             inline void wait() throw() { WaitForSingleObject(_event,INFINITE); }
00073 #else
00074 
00075             inline SyncEvent() throw() {
00076                 int error = sem_init(&_event,0,0);
00077                 assert(!error);
00078             }
00079             inline ~SyncEvent() throw() {
00080                 int error = sem_destroy(&_event);
00081                 assert(!error);
00082             }
00083 
00084             inline void set() throw() {
00085                 int val = 0;
00086                 int error = sem_getvalue(&_event,&val);
00087                 assert(!error);
00088                 assert(!val);
00089                 error = sem_post(&_event);
00090                 assert(!error);
00091             }
00092             inline void wait() throw() {
00093                 int val = 0;
00094                 int error = sem_getvalue(&_event,&val);
00095                 assert(!error);
00096                 assert(!val);
00097                 error = sem_wait(&_event);
00098                 assert(!error);
00099             }
00100 #endif
00101         private:
00102 #ifdef _WIN32
00103             HANDLE _event;
00104 #else
00105             sem_t _event;
00106 #endif
00107         };
00108 
00109         class Thread {
00110         public:
00111 #ifdef _WIN32
00112             void create(LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter) {
00113                 DWORD dwThreadId;
00114                 _hThread = CreateThread(
00115                     NULL,              // default security attributes
00116                     0,                 // use default stack size
00117                     lpStartAddress,    // thread function
00118                     lpParameter,       // argument to thread function
00119                     0,                 // use default creation flags
00120                     &dwThreadId);      // returns the thread identifier
00121             }
00122 
00123             void wait() {
00124                 WaitForSingleObject(_hThread, INFINITE);
00125             }
00126 
00127             void close() {
00128                 CloseHandle(_hThread);
00129             }
00130 #else
00131             void create(void* (*start_address)(void*), void* arg) {
00132                 // initialize the thread attribute
00133                 pthread_attr_init(&_threadAttr);
00134                 // set the stack size of the thread
00135                 pthread_attr_setstacksize(&_threadAttr, 1048576);
00136                 // create the thread
00137                 pthread_create(&_thread, &_threadAttr, start_address, arg);
00138             }
00139 
00140             void wait() {
00141                 pthread_join(_thread, NULL);
00142             }
00143 
00144             void close() {
00145                 pthread_attr_destroy(&_threadAttr);
00146             }
00147 #endif
00148         private:
00149 #ifdef _WIN32
00150             HANDLE _hThread;
00151 #else
00152             pthread_t _thread;
00153             pthread_attr_t _threadAttr;
00154 #endif
00155         };
00156     }
00157 }
00158 
00159 #endif //UCRITICAL_SECTION_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines