00001 /* ES40 emulator. 00002 * Copyright (C) 2007-2008 by the ES40 Emulator Project 00003 * 00004 * WWW : http://sourceforge.net/projects/es40 00005 * E-mail : camiel@camicom.com 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 * 00021 * Although this is not required, the author would appreciate being notified of, 00022 * and receiving any modifications you may make to the source code that might serve 00023 * the general public. 00024 */ 00025 00037 // 00038 // Semaphore_POSIX.cpp 00039 // 00040 // $Id: Semaphore_POSIX.cpp,v 1.1 2008/03/31 19:13:33 iamcamiel Exp $ 00041 // 00042 // Library: Foundation 00043 // Package: Threading 00044 // Module: Semaphore 00045 // 00046 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 00047 // and Contributors. 00048 // 00049 // Permission is hereby granted, free of charge, to any person or organization 00050 // obtaining a copy of the software and accompanying documentation covered by 00051 // this license (the "Software") to use, reproduce, display, distribute, 00052 // execute, and transmit the Software, and to prepare derivative works of the 00053 // Software, and to permit third-parties to whom the Software is furnished to 00054 // do so, all subject to the following: 00055 // 00056 // The copyright notices in the Software and this entire statement, including 00057 // the above license grant, this restriction and the following disclaimer, 00058 // must be included in all copies of the Software, in whole or in part, and 00059 // all derivative works of the Software, unless such copies or derivative 00060 // works are solely in the form of machine-executable object code generated by 00061 // a source language processor. 00062 // 00063 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00064 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00065 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00066 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00067 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00068 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00069 // DEALINGS IN THE SOFTWARE. 00070 // 00071 00072 00073 #include "VMS/Semaphore_POSIX.h" 00074 #include <sys/time.h> 00075 00076 00077 namespace Poco { 00078 00079 00080 SemaphoreImpl::SemaphoreImpl(int n, int max): _n(n), _max(max) 00081 { 00082 poco_assert (n >= 0 && max > 0 && n <= max); 00083 00084 if (pthread_mutex_init(&_mutex, NULL)) 00085 throw SystemException("cannot create semaphore (mutex)"); 00086 if (pthread_cond_init(&_cond, NULL)) 00087 throw SystemException("cannot create semaphore (condition)"); 00088 } 00089 00090 00091 SemaphoreImpl::~SemaphoreImpl() 00092 { 00093 pthread_cond_destroy(&_cond); 00094 pthread_mutex_destroy(&_mutex); 00095 } 00096 00097 00098 void SemaphoreImpl::waitImpl() 00099 { 00100 if (pthread_mutex_lock(&_mutex)) 00101 throw SystemException("wait for semaphore failed (lock)"); 00102 while (_n < 1) 00103 { 00104 if (pthread_cond_wait(&_cond, &_mutex)) 00105 { 00106 pthread_mutex_unlock(&_mutex); 00107 throw SystemException("wait for semaphore failed"); 00108 } 00109 } 00110 --_n; 00111 pthread_mutex_unlock(&_mutex); 00112 } 00113 00114 00115 bool SemaphoreImpl::waitImpl(long milliseconds) 00116 { 00117 int rc = 0; 00118 struct timespec abstime; 00119 00120 #if defined(__VMS) 00121 struct timespec delta; 00122 delta.tv_sec = milliseconds / 1000; 00123 delta.tv_nsec = (milliseconds % 1000)*1000000; 00124 pthread_get_expiration_np(&delta, &abstime); 00125 #else 00126 struct timeval tv; 00127 gettimeofday(&tv, NULL); 00128 abstime.tv_sec = tv.tv_sec + milliseconds / 1000; 00129 abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000; 00130 if (abstime.tv_nsec >= 1000000000) 00131 { 00132 abstime.tv_nsec -= 1000000000; 00133 abstime.tv_sec++; 00134 } 00135 #endif 00136 00137 if (pthread_mutex_lock(&_mutex) != 0) 00138 throw SystemException("wait for semaphore failed (lock)"); 00139 while (_n < 1) 00140 { 00141 if ((rc = pthread_cond_timedwait(&_cond, &_mutex, &abstime))) 00142 { 00143 if (rc == ETIMEDOUT) break; 00144 pthread_mutex_unlock(&_mutex); 00145 throw SystemException("cannot wait for semaphore"); 00146 } 00147 } 00148 if (rc == 0) --_n; 00149 pthread_mutex_unlock(&_mutex); 00150 return rc == 0; 00151 } 00152 00153 00154 } // namespace Poco