00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 Beesnest is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcSemaphore.h: interface for the CdcSemaphore class. */ 00021 00022 /** 00023 * A Wrapper for an OS semaphore. 00024 * <p> 00025 * @author Erez Bibi 00026 * @version 1.0 00027 */ 00028 00029 #ifndef __CDC_SEMAPHORE 00030 #define __CDC_SEMAPHORE 00031 00032 #include "../DcGlobals.h" 00033 #include "../DcException.h" 00034 00035 00036 class CdcSemaphore 00037 { 00038 private: 00039 00040 /* Handle to the mutex. */ 00041 HANDLE hSemaphore; 00042 00043 public: 00044 00045 /** 00046 * Constructor - Creates the semaphore. 00047 * <p> 00048 * @param init_count How many are taken at creation time. 00049 * @param max_count Maximum 'places' in the semaphore. 00050 */ 00051 CdcSemaphore (int init_count = 0, int max_count = 100) 00052 { hSemaphore = CreateSemaphore (NULL, init_count, max_count, NULL); 00053 if (hSemaphore == NULL) 00054 throw CdcException ("Cannot create semaphore."); 00055 } 00056 00057 /** 00058 * Destructor - Releases the semaphore. 00059 */ 00060 virtual ~CdcSemaphore() 00061 { CloseHandle (hSemaphore); } 00062 00063 /** 00064 * Waits (takes) on a semaphore. 00065 * <p> 00066 * @param wait The max time to wait for the semaphore [mSec]. 00067 * @return (bool) False if timeout. 00068 */ 00069 bool Wait (int wait = INFINITE) 00070 { return WaitForSingleObject (hSemaphore, wait) == WAIT_OBJECT_0; } 00071 00072 /** 00073 * Release one or more 'waits' one the semaphore. 00074 * <p> 00075 * @param count How many 'waits' to release. 00076 * @return False when semaphore is empty. 00077 */ 00078 bool Call (int count = 1) 00079 { return ReleaseSemaphore (hSemaphore, count, NULL) != 0; } 00080 00081 }; 00082 00083 #endif /* __CDC_SEMAPHORE */ 00084