ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkSemaphore.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkSemaphore_h
19 #define __itkSemaphore_h
20 #if !defined( ITK_LEGACY_REMOVE )
21 
22 # include "itkObjectFactory.h"
23 #include <string>
24 
25 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
26 #include "itkMutexLock.h"
27 #include <cstdio>
28 #include <sys/types.h>
29 #include <sys/ipc.h>
30 #include <sys/sem.h>
31 #include <cerrno>
32 #endif
33 
34 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
35 #ifdef ITK_USE_PTHREADS
36 #ifdef sun
37 #include <synch.h>
38 #else
39 #include <semaphore.h>
40 #endif
41 #endif
42 #endif
43 
44 #ifdef ITK_USE_WIN32_THREADS
45 # include "itkWindows.h"
46 #endif
47 
48 namespace itk
49 {
50 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
51 typedef int SemaphoreType;
52 #endif
53 
54 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
55 #ifdef ITK_USE_PTHREADS
56 #ifdef sun
57 typedef sema_t SemaphoreType;
58 #else
59 #ifdef __APPLE__
60 typedef sem_t *SemaphoreType;
61 #else
62 typedef sem_t SemaphoreType;
63 #endif
64 #endif
65 #endif
66 #endif
67 
68 #ifdef ITK_USE_WIN32_THREADS
69 typedef HANDLE SemaphoreType;
70 #endif
71 
72 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
73 #ifndef ITK_USE_PTHREADS
74 #ifndef ITK_USE_WIN32_THREADS
75 typedef int SemaphoreType;
76 #endif
77 #endif
78 #endif
79 
104 class ITK_EXPORT Semaphore:public LightObject
105 {
106 public:
108  typedef Semaphore Self;
109  typedef LightObject Superclass;
110  typedef SmartPointer< Self > Pointer;
111  typedef SmartPointer< const Self > ConstPointer;
112 
114  itkNewMacro(Self);
115 
117  itkTypeMacro(Semaphore, LightObject);
118 
120  void Initialize(unsigned int value);
121 
124  void Up();
125 
131  void Down();
132 
134  void Remove();
135 
136 protected:
137  Semaphore ();
138  ~Semaphore();
139  // Newer compilers do agreessive dead code checking.
140  // function is only to avoid the detection that
141  // warning: private field 'm_Pad1' is not used
142  // warning: private field 'm_Pad2' is not used
143  std::string NeverUseThisCompilerWarningAvoidanceFunctionForPad1Pad2(void) const
144  {
145  return std::string(this->m_Pad1)+"-"+std::string(this->m_Pad2);
146  }
147 
148 private:
149 
150 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
151 
153  static int m_IPCSemaphoreKey;
154 
156  static SimpleMutexLock m_Mutex;
157 
158  int UnixIpcSemaphoreCreate(int unix_semaphore_key);
159 
160  void UnixIpcSemaphoreRemove(int sid);
161 
162  void UnixIpcSemaphoreCall(int sid, int op);
163 
164  void UnixIpcSemaphoreDown(int sid);
165 
166  void UnixIpcSemaphoreUp(int sid);
167 
168 #endif
169 
170  char m_Pad1[128]; // to avoid false sharing in case of shared memory
171  // multiprocessor systems
172 
173  SemaphoreType m_Sema;
174 
175  char m_Pad2[128]; // to avoid false sharing in case of shared memory
176  // multiprocessor systems
177 
178 #ifdef __APPLE__
179  std::string GetUniqueName();
180 
181  static int m_SemaphoreCount;
182  std::string m_SemaphoreName;
183 #endif
184 
186 #ifdef ITK_USE_PTHREADS
187  bool m_PThreadsSemaphoreRemoved;
188 #endif
189 };
190 } //itk
191 
192 #endif //#if !defined( ITK_LEGACY_REMOVE )
193 #endif
194