sprint 1-alpha
|
00001 /****************************************************************************** 00002 * SPRINT::semaphore 00003 * 00004 * Copyright (C) 2005-2010 Paolo Medici <www.pmx.it> 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 * 00020 *******************************************************************************/ 00021 00022 #ifndef _SPRINT_SEMAPHORE_H 00023 #define _SPRINT_SEMAPHORE_H 00024 00025 #include <sysdef.h> 00026 00031 namespace sprint { 00032 00033 #ifdef WIN32 00034 00036 class semaphore { 00037 private: 00038 HANDLE hSemaphore; 00039 public: 00040 semaphore(unsigned int queuesize) 00041 { 00042 hSemaphore = ::CreateSemaphore(NULL, 0, queuesize, NULL); 00043 } 00044 ~semaphore() 00045 { 00046 ::CloseHandle(hSemaphore); 00047 } 00048 00050 inline long release() 00051 { 00052 long pc; 00053 ::ReleaseSemaphore(hSemaphore, 1, &pc); 00054 return pc; 00055 } 00056 00058 inline bool wait() 00059 { 00060 return ::WaitForSingleObject(hSemaphore, INFINITE)==WAIT_OBJECT_0; 00061 } 00062 00063 inline HANDLE gethandle() 00064 { 00065 return hSemaphore; 00066 } 00067 }; 00068 00069 00070 #elif defined(__unix__) 00071 00072 #include <semaphore.h> 00073 00075 class semaphore { 00076 sem_t sem; 00077 public: 00078 semaphore(unsigned int queuesize) 00079 { 00080 sem_init (&sem, FALSE, queuesize); 00081 } 00082 ~semaphore() 00083 { 00084 sem_destroy(&sem); 00085 } 00086 00087 inline int release() 00088 { 00089 int value; 00090 sem_post(&sem); 00091 sem_getvalue(&sem, &value); /* se serve! */ 00092 return value; 00093 } 00094 00095 inline bool wait() 00096 { 00097 sem_wait(&sem); 00098 return true; 00099 } 00100 00101 }; 00102 00103 00104 #endif // WIN32/UNIX 00105 00106 } 00107 00108 #endif // _SPRINT_SEMAPHORE_H