00001 /***************************** License ********************************** 00002 00003 Copyright (C) 2008 by Communicative Machines 00004 http://www.cmlabs.com All rights reserved. 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 00019 00020 ************************************************************************/ 00021 00022 00023 // JThread.h: interface for the JThread class. 00024 // 00025 ////////////////////////////////////////////////////////////////////// 00026 00027 #if !defined(AFX_JSEMAPHORE_H__7D7254A3_4145_4D83_94DE_1343EB810C61__INCLUDED_) 00028 #define AFX_JSEMAPHORE_H__7D7254A3_4145_4D83_94DE_1343EB810C61__INCLUDED_ 00029 00030 #if _MSC_VER > 1000 00031 #pragma once 00032 #endif // _MSC_VER > 1000 00033 00034 #ifndef WIN32 00035 #ifdef _WIN32 00036 #define WIN32 00037 #endif 00038 #endif 00039 00040 #ifdef WIN32 00041 #include <windows.h> 00042 #if !defined(_WIN32_WCE) 00043 #include <process.h> 00044 #endif 00045 #else 00046 #include <pthread.h> 00047 #include <signal.h> 00048 #include <sys/time.h> 00049 #include <unistd.h> 00050 #undef HANDLE 00051 #define HANDLE pthread_cond_t* 00052 #endif 00053 00054 #include "Object.h" 00055 #include "JString.h" 00056 // #include <thread.h> 00057 00058 namespace cmlabs { 00059 00060 class JThread; 00061 00062 class JSemaphore : public Object 00063 { 00064 public: 00065 JSemaphore(JString semName = ""); 00066 ~JSemaphore(); 00067 Object* clone() const { return NULL; } 00068 00069 int count; 00070 JString name; 00071 00072 bool wait(long ms = -1); 00073 bool post(); 00074 bool tryWait(); 00075 bool unitTest(); 00076 00077 private: 00078 00079 HANDLE hSemaphore; 00080 #ifndef WIN32 00081 pthread_mutex_t* mutex; 00082 #endif 00083 }; 00084 00085 class JSingleSemaphore : public Object 00086 { 00087 public: 00088 JSingleSemaphore(); 00089 ~JSingleSemaphore(); 00090 Object* clone() const { return NULL; } 00091 00092 bool wait(long ms = -1); 00093 bool postToAll(); 00094 bool tryWait(); 00095 00096 private: 00097 00098 HANDLE hSemaphore; 00099 #ifndef WIN32 00100 pthread_mutex_t* mutex; 00101 #endif 00102 }; 00103 00104 class JMutex : public Object 00105 { 00106 public: 00107 JMutex(); 00108 ~JMutex(); 00109 Object* clone() const { return NULL; } 00110 00111 // Try to lock the mutex 00112 bool enterMutex(long ms = -1) const; 00113 // Try to unlock the mutex 00114 bool leaveMutex() const; 00115 // For backward compatibility 00116 bool EnterMutex(long ms = -1) const; 00117 // For backward compatibility 00118 bool LeaveMutex() const; 00119 00120 private: 00121 00122 #ifdef WIN32 00123 HANDLE mutex; 00124 #else 00125 pthread_cond_t* semaphore; 00126 pthread_mutex_t* semaphoremutex; 00127 pthread_mutex_t* mutex; 00128 #endif 00129 }; 00130 00131 class WriteAccessMutex : public Object 00132 { 00133 public: 00134 WriteAccessMutex(); 00135 ~WriteAccessMutex(); 00136 Object* clone() const { return NULL; } 00137 00138 bool gainReadAccess(long ms = -1); 00139 bool releaseReadAccess(); 00140 00141 bool gainWriteAccess(long ms = -1); 00142 bool releaseWriteAccess(); 00143 00144 private: 00145 00146 int currentReadCount; 00147 JMutex accessMutex; 00148 }; 00149 00150 #ifndef WIN32 00151 bool calcTimeout(struct timespec &timeout, struct timeval &now, long ms); 00152 #endif 00153 00154 } // namespace cmlabs 00155 00156 #include "JThread.h" 00157 00158 00159 00160 00161 #endif // !defined(AFX_JSEMAPHORE_H__7D7254A3_4145_4D83_94DE_1343EB810C61__INCLUDED_)