ITK  4.2.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 
21 # include "itkObjectFactory.h"
22 #include <string>
23 
24 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
25 #include "itkMutexLock.h"
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/ipc.h>
29 #include <sys/sem.h>
30 #include <errno.h>
31 #endif
32 
33 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
34 #ifdef ITK_USE_PTHREADS
35 #ifdef sun
36 #include <synch.h>
37 #else
38 #include <semaphore.h>
39 #endif
40 #endif
41 #endif
42 
43 #ifdef ITK_USE_WIN32_THREADS
44 # include "itkWindows.h"
45 #endif
46 
47 namespace itk
48 {
49 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
50 typedef int SemaphoreType;
51 #endif
52 
53 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
54 #ifdef ITK_USE_PTHREADS
55 #ifdef sun
56 typedef sema_t SemaphoreType;
57 #else
58 #ifdef __APPLE__
59 typedef sem_t *SemaphoreType;
60 #else
61 typedef sem_t SemaphoreType;
62 #endif
63 #endif
64 #endif
65 #endif
66 
67 #ifdef ITK_USE_WIN32_THREADS
68 typedef HANDLE SemaphoreType;
69 #endif
70 
71 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
72 #ifndef ITK_USE_PTHREADS
73 #ifndef ITK_USE_WIN32_THREADS
74 typedef int SemaphoreType;
75 #endif
76 #endif
77 #endif
78 
102 class ITK_EXPORT Semaphore:public LightObject
103 {
104 public:
106  typedef Semaphore Self;
110 
112  itkNewMacro(Self);
113 
115  itkTypeMacro(Semaphore, LightObject);
116 
118  void Initialize(unsigned int value);
119 
122  void Up();
123 
129  void Down();
130 
132  void Remove();
133 
134 protected:
135  Semaphore ();
136  ~Semaphore();
137  // Newer compilers do agreessive dead code checking.
138  // function is only to avoid the detection that
139  // warning: private field 'm_Pad1' is not used
140  // warning: private field 'm_Pad2' is not used
141  std::string NeverUseThisCompilerWarningAvoidanceFunctionForPad1Pad2(void) const
142  {
143  return std::string(this->m_Pad1)+"-"+std::string(this->m_Pad2);
144  }
145 private:
146 
147 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
148 
150  static int m_IPCSemaphoreKey;
151 
153  static SimpleMutexLock m_Mutex;
154 
155  int UnixIpcSemaphoreCreate(int unix_semaphore_key);
156 
157  void UnixIpcSemaphoreRemove(int sid);
158 
159  void UnixIpcSemaphoreCall(int sid, int op);
160 
161  void UnixIpcSemaphoreDown(int sid);
162 
163  void UnixIpcSemaphoreUp(int sid);
164 
165 #endif
166 
167  char m_Pad1[128]; // to avoid false sharing in case of shared memory
168  // multiprocessor systems
169 
171 
172  char m_Pad2[128]; // to avoid false sharing in case of shared memory
173  // multiprocessor systems
174 
175 #ifdef __APPLE__
176  std::string GetUniqueName();
177 
178  static int m_SemaphoreCount;
179  std::string m_SemaphoreName;
180 #endif
181 
183 #ifdef ITK_USE_PTHREADS
184  bool m_PThreadsSemaphoreRemoved;
185 #endif
186 };
187 } //itk
188 
189 #endif
190