PBRT
|
00001 00002 // 00003 // Copyright (c) 2005, Industrial Light & Magic, a division of Lucas 00004 // Digital Ltd. LLC 00005 // 00006 // All rights reserved. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are 00010 // met: 00011 // * Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // * Redistributions in binary form must reproduce the above 00014 // copyright notice, this list of conditions and the following disclaimer 00015 // in the documentation and/or other materials provided with the 00016 // distribution. 00017 // * Neither the name of Industrial Light & Magic nor the names of 00018 // its contributors may be used to endorse or promote products derived 00019 // from this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00034 00035 #ifndef INCLUDED_ILM_THREAD_SEMAPHORE_H 00036 #define INCLUDED_ILM_THREAD_SEMAPHORE_H 00037 00038 //----------------------------------------------------------------------------- 00039 // 00040 // class Semaphore -- a wrapper class for 00041 // system-dependent counting semaphores 00042 // 00043 //----------------------------------------------------------------------------- 00044 00045 #include "IlmBaseConfig.h" 00046 00047 #if defined _WIN32 || defined _WIN64 00048 #ifdef NOMINMAX 00049 #undef NOMINMAX 00050 #endif 00051 #define NOMINMAX 00052 #include <windows.h> 00053 #elif HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES 00054 #include <pthread.h> 00055 #elif HAVE_PTHREAD && HAVE_POSIX_SEMAPHORES 00056 #include <semaphore.h> 00057 #endif 00058 00059 namespace IlmThread { 00060 00061 00062 class Semaphore 00063 { 00064 public: 00065 00066 Semaphore (unsigned int value = 0); 00067 virtual ~Semaphore(); 00068 00069 void wait(); 00070 bool tryWait(); 00071 void post(); 00072 int value() const; 00073 00074 private: 00075 00076 #if defined _WIN32 || defined _WIN64 00077 00078 mutable HANDLE _semaphore; 00079 00080 #elif HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES 00081 00082 // 00083 // If the platform has Posix threads but no semapohores, 00084 // then we implement them ourselves using condition variables 00085 // 00086 00087 struct sema_t 00088 { 00089 unsigned int count; 00090 unsigned long numWaiting; 00091 pthread_mutex_t mutex; 00092 pthread_cond_t nonZero; 00093 }; 00094 00095 mutable sema_t _semaphore; 00096 00097 #elif HAVE_PTHREAD && HAVE_POSIX_SEMAPHORES 00098 00099 mutable sem_t _semaphore; 00100 00101 #endif 00102 00103 void operator = (const Semaphore& s); // not implemented 00104 Semaphore (const Semaphore& s); // not implemented 00105 }; 00106 00107 00108 } // namespace IlmThread 00109 00110 #endif