last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2005 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 /** 00042 * \file cvrSemaphore.h 00043 * Contains the cvr::semaphore wrapper class to encapulate the 00044 * differences between the POSIX and Win32 concepts. 00045 * \author Thomas Rusert 00046 * \date 04.11.1999 00047 * 00048 * $Id: cvrSemaphore.h,v 1.5 2006/01/03 14:49:25 alvarado Exp $ 00049 */ 00050 00051 00052 #ifndef _CVR_SEMAPHORE_H_ 00053 #define _CVR_SEMAPHORE_H_ 00054 00055 #include "cvrObject.h" 00056 #include "cvrMacroSymbols.h" 00057 00058 #ifndef _CVR_WIN32 00059 # include <semaphore.h> 00060 #else 00061 # include <windows.h> 00062 # include <process.h> 00063 # define SEM_VALUE_MAX 16777215 // (2^24-1) 00064 #endif 00065 00066 namespace cvr { 00067 /** 00068 * Class for inter-thread synchronisation concept of semaphores. 00069 * 00070 * The difference between semaphores and mutexes is that the later one only 00071 * tracks if the mutex has been locked or not, while the semaphore counts 00072 * the number of "locks" (called here "waits()") and requires the same 00073 * number of unlocks (here "posts()") to release the access. 00074 * 00075 * The semaphore is unlocked when the counter reaches the value zero (see 00076 * getValue()), which means that in construction time you decide how far 00077 * from unlocking the semaphore is (for values greater than zero) or if it 00078 * is from the beginning unlocked (for value zero). Note that the default 00079 * counter value is set to 1. 00080 * 00081 * @see cvr::mutex, cvr::thread 00082 */ 00083 class semaphore { 00084 public: 00085 /** 00086 * Default constructor. 00087 * 00088 * The semaphore is unlocked when the counter reaches the value 00089 * zero, which means that in construction time you decide how far 00090 * from unlocking the semaphore is (for values greater than 00091 * zero) or if it is from the beginning unlocked (for value zero). 00092 * 00093 * @param initialValue initial value for the semaphore counter. 00094 */ 00095 semaphore(const int initialValue = 1); 00096 00097 /** 00098 * Destructor 00099 */ 00100 virtual ~semaphore(); 00101 00102 /** 00103 * Wait on semaphore. 00104 * 00105 * Wait on semaphore, i.e. decrease the value or wait if counter <= 0 00106 * 00107 * @return true if successful, false otherwise 00108 */ 00109 bool wait(); 00110 00111 /** 00112 * Try to wait on semaphore, but do not block. 00113 * 00114 * If this function returns true, then you can get access to the resource 00115 * you are trying to protect, but if it returns false, then you may not 00116 * assume you have rights to do that. 00117 * 00118 * @return true if value was decreased. 00119 */ 00120 bool tryWait(); 00121 00122 /** 00123 * Post semaphore. 00124 * 00125 * Post semaphore, i.e. increase its value 00126 * 00127 * @return true if successful, false otherwise 00128 */ 00129 bool post(); 00130 00131 /** 00132 * Get current value. 00133 * 00134 * The returned value will be usually lower than zero or zero if 00135 * the semaphore has been unlocked. 00136 */ 00137 int getValue(); 00138 00139 /** 00140 * Reset value to initialValue 00141 */ 00142 void reset(); 00143 00144 protected: 00145 /** 00146 * Destroy the semaphore 00147 */ 00148 void destroy(); 00149 00150 /** 00151 * Initial semaphore value. 00152 * 00153 * This value is required to allow resetting the semaphore 00154 */ 00155 int initValue; 00156 00157 # ifndef _CVR_WIN32 00158 /** 00159 * The posix semaphore 00160 */ 00161 sem_t theSemaphore; 00162 # else 00163 /** 00164 * The WIN32 semaphore object 00165 */ 00166 HANDLE theSemaphore; 00167 00168 /** 00169 * Counter of the semaphore 00170 */ 00171 int counter; 00172 # endif 00173 }; 00174 } 00175 00176 #endif 00177