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 // JThread.cpp: implementation of the JThread class. 00022 // 00023 ////////////////////////////////////////////////////////////////////// 00024 00025 #include "JSemaphore.h" 00026 00027 #ifdef WIN32 00028 #include "win32/JSemaphore_Win32.cpp" 00029 #else 00030 #include "linux/JSemaphore_Linux.cpp" 00031 #endif 00032 00033 00034 00035 namespace cmlabs { 00036 00037 // ***************************************************************** 00038 // Unit Testing 00039 // ***************************************************************** 00040 00041 00042 bool JSemaphore::unitTest() { 00043 JThread* thr = new JThread(); 00044 bool res = thr->unitTest(); 00045 delete(thr); 00046 return res; 00047 } 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 // ***************************************************************** 00058 // WriteAccessMutex 00059 // ***************************************************************** 00060 00061 WriteAccessMutex::WriteAccessMutex() { 00062 currentReadCount = 0; 00063 } 00064 00065 WriteAccessMutex::~WriteAccessMutex() { 00066 accessMutex.LeaveMutex(); 00067 } 00068 00069 bool WriteAccessMutex::gainReadAccess(long timeout) { 00070 if (!accessMutex.EnterMutex(timeout)) 00071 return false; 00072 currentReadCount++; 00073 accessMutex.LeaveMutex(); 00074 return true; 00075 } 00076 00077 bool WriteAccessMutex::releaseReadAccess() { 00078 // accessMutex.EnterMutex(100); 00079 currentReadCount--; 00080 if (currentReadCount < 0) 00081 currentReadCount = 0; 00082 // accessMutex.LeaveMutex(); 00083 return true; 00084 } 00085 00086 bool WriteAccessMutex::gainWriteAccess(long timeout) { 00087 00088 JTime start; 00089 // JTime now; 00090 // long ms; 00091 00092 if (!accessMutex.EnterMutex(timeout)) 00093 return false; 00094 00095 while (currentReadCount > 0) { 00096 // accessMutex.LeaveMutex(); 00097 wait(10); 00098 if (JTime() - start > timeout) { 00099 printf("### Timed out waiting for write access... ###\n"); 00100 accessMutex.LeaveMutex(); 00101 return false; 00102 } 00103 // now = JTime(); 00104 // ms = timeout - (now-start); 00105 // if (ms < 0) { 00106 // printf("### Timed out waiting for write access... ###\n"); 00107 // return false; 00108 // } 00109 // if (!accessMutex.EnterMutex(ms)) 00110 // return false; 00111 } 00112 00113 return true; 00114 } 00115 00116 bool WriteAccessMutex::releaseWriteAccess() { 00117 accessMutex.LeaveMutex(); 00118 return true; 00119 } 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 } // namespace cmlabs