ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSemaphore.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkSemaphore_h
00019 #define __itkSemaphore_h
00020 
00021 # include "itkObjectFactory.h"
00022 #include <string>
00023 
00024 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
00025 #include "itkMutexLock.h"
00026 #include <stdio.h>
00027 #include <sys/types.h>
00028 #include <sys/ipc.h>
00029 #include <sys/sem.h>
00030 #include <errno.h>
00031 #endif
00032 
00033 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
00034 #ifdef ITK_USE_PTHREADS
00035 #ifdef sun
00036 #include <synch.h>
00037 #else
00038 #include <semaphore.h>
00039 #endif
00040 #endif
00041 #endif
00042 
00043 #ifdef ITK_USE_WIN32_THREADS
00044 # include "itkWindows.h"
00045 #endif
00046 
00047 namespace itk
00048 {
00049 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
00050 typedef int SemaphoreType;
00051 #endif
00052 
00053 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
00054 #ifdef ITK_USE_PTHREADS
00055 #ifdef sun
00056 typedef sema_t SemaphoreType;
00057 #else
00058 #ifdef __APPLE__
00059 typedef sem_t *SemaphoreType;
00060 #else
00061 typedef sem_t SemaphoreType;
00062 #endif
00063 #endif
00064 #endif
00065 #endif
00066 
00067 #ifdef ITK_USE_WIN32_THREADS
00068 typedef HANDLE SemaphoreType;
00069 #endif
00070 
00071 #ifndef ITK_USE_UNIX_IPC_SEMAPHORES
00072 #ifndef ITK_USE_PTHREADS
00073 #ifndef ITK_USE_WIN32_THREADS
00074 typedef int SemaphoreType;
00075 #endif
00076 #endif
00077 #endif
00078 
00102 class ITK_EXPORT Semaphore:public LightObject
00103 {
00104 public:
00106   typedef Semaphore                  Self;
00107   typedef LightObject                Superclass;
00108   typedef SmartPointer< Self >       Pointer;
00109   typedef SmartPointer< const Self > ConstPointer;
00110 
00112   itkNewMacro(Self);
00113 
00115   itkTypeMacro(Semaphore, LightObject);
00116 
00118   void Initialize(unsigned int value);
00119 
00122   void Up();
00123 
00129   void Down();
00130 
00132   void Remove();
00133 
00134 protected:
00135   Semaphore ();
00136   ~Semaphore();
00137 private:
00138 
00139 #ifdef ITK_USE_UNIX_IPC_SEMAPHORES
00140 
00142   static int m_IPCSemaphoreKey;
00143 
00145   static SimpleMutexLock m_Mutex;
00146 
00147   int  UnixIpcSemaphoreCreate(int unix_semaphore_key);
00148 
00149   void UnixIpcSemaphoreRemove(int sid);
00150 
00151   void UnixIpcSemaphoreCall(int sid, int op);
00152 
00153   void UnixIpcSemaphoreDown(int sid);
00154 
00155   void UnixIpcSemaphoreUp(int sid);
00156 
00157 #endif
00158 
00159   char Pad1[128]; // to avoid false sharing in case of shared memory
00160                   // multiprocessor systems
00161   SemaphoreType m_Sema;
00162   char          Pad2[128]; // to avoid false sharing in case of shared memory
00163                            // multiprocessor systems
00164 
00165 #ifdef __APPLE__
00166   std::string GetUniqueName();
00167 
00168   static int  m_SemaphoreCount;
00169   std::string m_SemaphoreName;
00170 #endif
00171 
00173 #ifdef ITK_USE_PTHREADS
00174   bool m_PThreadsSemaphoreRemoved;
00175 #endif
00176 };
00177 } //itk
00178 
00179 #endif
00180