| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkSemaphore.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:47 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
DEF |
#ifndef __itkSemaphore_h_ |
| 18 |
DEF |
#define __itkSemaphore_h_ |
| 19 |
|
|
| 20 |
|
#include "itkObjectFactory.h" |
| 21 |
|
#include "itkLightObject.h" |
| 22 |
|
#include "itkConfigure.h" |
| 23 |
|
#include <string> |
| 24 |
|
|
| 25 |
|
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES |
| 26 |
|
#include "itkMutexLock.h" |
| 27 |
|
#include <stdio.h> |
| 28 |
|
#include <sys/types.h> |
| 29 |
|
#include <sys/ipc.h> |
| 30 |
|
#include <sys/sem.h> |
| 31 |
|
#include <errno.h> |
| 32 |
|
#endif |
| 33 |
|
|
| 34 |
|
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES |
| 35 |
|
#ifdef ITK_USE_SPROC |
| 36 |
|
#include "itkMultiThreader.h" |
| 37 |
|
#include <ulocks.h> |
| 38 |
|
#endif |
| 39 |
|
#ifdef ITK_USE_PTHREADS |
| 40 |
|
#ifdef sun |
| 41 |
|
#include <synch.h> |
| 42 |
|
#else |
| 43 |
|
#include <semaphore.h> |
| 44 |
|
#endif |
| 45 |
|
#endif |
| 46 |
|
#endif |
| 47 |
|
|
| 48 |
|
#ifdef ITK_USE_WIN32_THREADS |
| 49 |
|
#include "itkWindows.h" |
| 50 |
|
#endif |
| 51 |
|
|
| 52 |
|
namespace itk { |
| 53 |
|
|
| 54 |
|
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES |
| 55 |
IND |
**typedef int SemaphoreType; |
| 56 |
|
#endif |
| 57 |
|
|
| 58 |
|
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES |
| 59 |
|
#ifdef ITK_USE_SPROC |
| 60 |
|
typedef usema_t * SemaphoreType; |
| 61 |
|
#endif |
| 62 |
|
#ifdef ITK_USE_PTHREADS |
| 63 |
|
#ifdef sun |
| 64 |
|
typedef sema_t SemaphoreType; |
| 65 |
|
#else |
| 66 |
|
#ifdef __APPLE__ |
| 67 |
|
typedef sem_t *SemaphoreType; |
| 68 |
|
#else |
| 69 |
|
typedef sem_t SemaphoreType; |
| 70 |
|
#endif |
| 71 |
|
#endif |
| 72 |
|
#endif |
| 73 |
|
#endif |
| 74 |
|
|
| 75 |
|
#ifdef ITK_USE_WIN32_THREADS |
| 76 |
|
typedef HANDLE SemaphoreType; |
| 77 |
|
#endif |
| 78 |
|
|
| 79 |
|
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES |
| 80 |
|
#ifndef ITK_USE_SPROC |
| 81 |
|
#ifndef ITK_USE_PTHREADS |
| 82 |
|
#ifndef ITK_USE_WIN32_THREADS |
| 83 |
|
typedef int SemaphoreType; |
| 84 |
|
#endif |
| 85 |
|
#endif |
| 86 |
|
#endif |
| 87 |
|
#endif |
| 88 |
|
|
| 89 |
|
/** \class Semaphore |
| 90 |
|
* \brief The semaphore class is used to synchronize execution between threads. |
| 91 |
|
* |
| 92 |
|
* Semaphore objects maintain a counter value that can be incremented and |
| 93 |
|
* decremented by atomic calls to Up() and Down(). Semaphores are commonly used |
| 94 |
|
* to manage the use of a limited pool of system resources among several |
| 95 |
|
* threads. If Down() is called when the value of the semaphore is zero, the |
| 96 |
|
* calling thread will block until the semaphore value goes above zero again. |
| 97 |
|
* When a blocked thread is released, it decrements the Semaphore counter |
| 98 |
|
* before continuing to execute. |
| 99 |
|
* |
| 100 |
|
* The Up() and Down() operations are standard as defined by E.W. Dijkstra. |
| 101 |
|
* The Initialize( num ) operation creates the system semaphore object with an |
| 102 |
|
* initial counter value of num. Initialize must be called before the |
| 103 |
|
* Semaphore can be used. The Remove() method destroys the system semaphore |
| 104 |
|
* object. It is not necessary to call Remove() unless you want to |
| 105 |
|
* re-Initialize() the object. |
| 106 |
|
* |
| 107 |
|
* This class supports 3 types of semaphores on Unix systems, POSIX semaphores, |
| 108 |
|
* IPC semaphores, and IRIX semaphores from the SGI Sproc library. On Windows |
| 109 |
|
* systems, POSIX semaphores and WIN32 thread library semaphores are supported. |
| 110 |
|
* |
| 111 |
IND |
*/ |
| 112 |
|
class ITKCommon_EXPORT Semaphore : public LightObject |
| 113 |
|
{ |
| 114 |
|
public: |
| 115 |
|
/** Standard class typedefs. */ |
| 116 |
|
typedef Semaphore Self; |
| 117 |
TDA |
typedef LightObject Superclass; |
| 118 |
TDA |
typedef SmartPointer<Self> Pointer; |
| 119 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 120 |
|
|
| 121 |
|
/** Method for creation through the object factory. */ |
| 122 |
|
itkNewMacro(Self); |
| 123 |
|
|
| 124 |
|
/** Run-time type information (and related methods). */ |
| 125 |
|
itkTypeMacro(Semaphore, LightObject); |
| 126 |
|
|
| 127 |
|
/** Initialize the semaphore with a count of value. */ |
| 128 |
|
void Initialize(unsigned int value); |
| 129 |
|
|
| 130 |
|
/** Increment the semaphore count, unblocking up to one thread that may be |
| 131 |
IND |
******blocked in the down() method. */ |
| 132 |
|
void Up(); |
| 133 |
|
|
| 134 |
|
/** Decrement the semaphore count. If the count is zero, this thread will be |
| 135 |
IND |
******blocked until another thread calls the up() method. The order in which |
| 136 |
IND |
******threads will be unblocked is not defined, but implementors should give |
| 137 |
IND |
******preference to those threads that have waited the longest. |
| 138 |
IND |
***/ |
| 139 |
|
void Down(); |
| 140 |
|
|
| 141 |
|
/** Remove the semaphore from the system. */ |
| 142 |
|
void Remove (); |
| 143 |
|
|
| 144 |
|
protected: |
| 145 |
|
Semaphore (); |
| 146 |
|
~Semaphore(); |
| 147 |
|
|
| 148 |
|
private: |
| 149 |
|
|
| 150 |
|
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES |
| 151 |
|
/** Every IPC semaphore must be created with a unique key. This variable |
| 152 |
|
* increments with each new ITK Semaphore created to give a unique key. */ |
| 153 |
|
static int m_IPCSemaphoreKey; |
| 154 |
|
/** Mutex to lock increment operation on m_IPCSemaphoreKey */ |
| 155 |
|
static SimpleMutexLock m_Mutex; |
| 156 |
|
|
| 157 |
|
int UnixIpcSemaphoreCreate (int unix_semaphore_key); |
| 158 |
|
void UnixIpcSemaphoreRemove (int sid); |
| 159 |
|
void UnixIpcSemaphoreCall (int sid, int op); |
| 160 |
|
void UnixIpcSemaphoreDown (int sid); |
| 161 |
|
void UnixIpcSemaphoreUp (int sid); |
| 162 |
|
#endif |
| 163 |
|
|
| 164 |
LEN |
char Pad1[128]; // to avoid false sharing in case of shared memory multiprocessor systems |
| 165 |
|
SemaphoreType m_Sema; |
| 166 |
LEN |
char Pad2[128]; // to avoid false sharing in case of shared memory multiprocessor systems |
| 167 |
|
|
| 168 |
|
#ifdef __APPLE__ |
| 169 |
|
std::string GetUniqueName(); |
| 170 |
|
static int m_SemaphoreCount; |
| 171 |
|
std::string m_SemaphoreName; |
| 172 |
|
#endif |
| 173 |
|
|
| 174 |
IND |
/** When using pthread the semaphore cannot be removed twice so we use a flag */ |
| 175 |
|
#ifdef ITK_USE_PTHREADS |
| 176 |
|
bool m_PThreadsSemaphoreRemoved; |
| 177 |
|
#endif |
| 178 |
|
|
| 179 |
|
}; |
| 180 |
|
|
| 181 |
|
}//itk |
| 182 |
|
|
| 183 |
|
#endif |
| 184 |
|
|