File: itkSemaphore.cxx

package info (click to toggle)
insighttoolkit 3.6.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 94,956 kB
  • ctags: 74,981
  • sloc: cpp: 355,621; ansic: 195,070; fortran: 28,713; python: 3,802; tcl: 1,996; sh: 1,175; java: 583; makefile: 415; csh: 184; perl: 175
file content (461 lines) | stat: -rw-r--r-- 10,226 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkSemaphore.cxx,v $
  Language:  C++
  Date:      $Date: 2006-02-06 22:08:11 $
  Version:   $Revision: 1.14 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "itkConfigure.h"
#include "itkSemaphore.h"

#ifdef __APPLE__
extern "C" {
#include <stdio.h>
#include <time.h>
}
#endif

/// DEBUG
extern "C" {
#include <stdio.h>
#include <errno.h>
}
///

namespace itk {

#ifdef __APPLE__
int Semaphore::m_SemaphoreCount = 0;
#endif

#ifdef ITK_USE_UNIX_IPC_SEMAPHORES
int Semaphore::m_IPCSemaphoreKey = 12345;
SimpleMutexLock Semaphore::m_Mutex;
#endif

#ifdef __APPLE__
std::string Semaphore::GetUniqueName()
{
  char s[255];  
  time_t t = time(0);
  snprintf(s,254,"MACSEM%d%d",static_cast<int>(t),m_SemaphoreCount); 
  return std::string(s); 
}
#endif

Semaphore::Semaphore ()
{
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES
  m_Sema= -1;
#endif


#ifdef __APPLE__
  m_Sema = 0;
  m_SemaphoreCount++;
  m_SemaphoreName = Semaphore::GetUniqueName();
#endif
  
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES  

#ifdef ITK_USE_SPROC
  m_Sema = 0;
#endif

#endif
  
#ifdef ITK_USE_WIN32_THREADS
  m_Sema = 0;
#endif

#ifdef ITK_USE_PTHREADS
  m_PThreadsSemaphoreRemoved = false;
#endif
}
  
void Semaphore::Initialize(unsigned int value)
{
  
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES
  // Obtain a lock over the m_IPCSemaphoreKey so that the new semaphore is
  // created with a unique unix_semaphore_key 
  Semaphore::m_Mutex.Lock();
  m_Sema = Semaphore::UnixIpcSemaphoreCreate(Semaphore::m_IPCSemaphoreKey);
  
  // by default the semaphore created has value 0
  Semaphore::m_IPCSemaphoreKey++;
  Semaphore::m_Mutex.Unlock();
  
  for (unsigned int i = 0; i < value; i++)
    {
    this->Up();
    }
#endif
  
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES  
#ifdef ITK_USE_SPROC
  if (MultiThreader::GetInitialized() == false)
    {
      MultiThreader::Initialize();
    }

  m_Sema = usnewsema(MultiThreader::GetThreadArena(), static_cast<int>(value));
  if ( ! m_Sema )
    {
      itkExceptionMacro( << " sem_init call failed with code " << m_Sema );
    }
  
#endif
#ifdef ITK_USE_PTHREADS

m_PThreadsSemaphoreRemoved = false;

#if defined sun
  if ( sema_init(&m_Sema, 0, value, NULL ) != 0 )
    {
    itkExceptionMacro( << "sema_init call failed" );
    }
#elif defined  __APPLE__
  m_Sema  = sem_open( m_SemaphoreName.c_str(), O_CREAT, 0x0644, value );
  if ( m_Sema == (sem_t *)SEM_FAILED )
    {
    //  perror("FAILED WITH ERROR:" );
    itkExceptionMacro( << "sem_open call failed on " << m_SemaphoreName.c_str() );
    }
#else
  if ( sem_init(&m_Sema, 0, value) != 0 )
    {
      itkExceptionMacro( << "sem_init call failed" );
    }
#endif // if defined sun

#endif // ifdef ITK_USE_PTHREADS
#endif // ifndef ITK_USE_UNIX_IPC_SEMAPHORES
  
#ifdef ITK_USE_WIN32_THREADS
  m_Sema = CreateSemaphore( 0, value, 0x7FFFFFFF, 0);
  if (m_Sema == 0)
    {
      itkExceptionMacro( << "CreateSemaphore call failed" );
    }
#endif
}
  
void Semaphore::Up()
{
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES
  Semaphore::UnixIpcSemaphoreUp(m_Sema);
#endif
  
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES

#ifdef ITK_USE_SPROC
  if (usvsema(m_Sema) == -1)
    {
      itkExceptionMacro( << "usvsema call failed." );
    }
#endif
#ifdef ITK_USE_PTHREADS
#ifdef sun
  if ( sema_post(&m_Sema) != 0 )
    {
      itkExceptionMacro( << "sema_post call failed." );
    }
#else
#ifdef __APPLE__
  if ( sem_post(m_Sema) != 0 )
    {
    itkExceptionMacro( << "sem_post call failed." );
    }
#else
  if ( sem_post(&m_Sema) != 0 )
    {
      itkExceptionMacro( << "sem_post call failed." );
    }
#endif
#endif // ifdef sun
#endif

#endif  // ITK_USE_UNIX_SEMAPHORES
  
#ifdef ITK_USE_WIN32_THREADS
  if ( ! ReleaseSemaphore ((HANDLE) m_Sema, 1 , 0) )
    {
      itkExceptionMacro( << "Semaphore post call failed." );
    }
#endif
}

void Semaphore::Down()
{
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES
  Semaphore::UnixIpcSemaphoreDown (m_Sema);
#endif
  
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES
#ifdef ITK_USE_SPROC
  if (uspsema(m_Sema) == -1)
    {
      itkExceptionMacro( << "uspsema call failed." );
    }
#endif
#ifdef ITK_USE_PTHREADS
#ifdef sun
  if (sema_wait(&m_Sema) != 0)
    {
    itkExceptionMacro( << "sema_wait call failed." );
    }
#else
#ifdef __APPLE__
  if (sem_wait(m_Sema) != 0)
    {
      itkExceptionMacro( << "sem_wait call failed." );
    }
#else
  
  if (sem_wait(&m_Sema) != 0)
    {
      itkExceptionMacro( << "sem_wait call failed." );
    }
#endif
#endif
#endif
#endif

#ifdef ITK_USE_WIN32_THREADS
  if ( WaitForSingleObject(m_Sema, INFINITE) == WAIT_FAILED )
    {
      itkExceptionMacro( << "WaitForSingleObject call failed. ");
    }
#endif
}

Semaphore::~Semaphore()
{
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES
  if (m_Sema != -1)
    {
      this->Remove();
    }
#endif
  
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES  
#ifdef ITK_USE_SPROC
  if (m_Sema != 0)
    {
      this->Remove();
    }
#endif
#ifdef ITK_USE_PTHREADS  
  if(!m_PThreadsSemaphoreRemoved)
    {
    this->Remove();
    }
#endif
#endif
  
#ifdef ITK_USE_WIN32_THREADS
  if (m_Sema != 0)
    {
      this->Remove();
    }
#endif
}

void Semaphore::Remove()
{
#ifdef ITK_USE_UNIX_IPC_SEMAPHORES
  if (m_Sema != -1)
    {
      Semaphore::UnixIpcSemaphoreRemove(m_Sema);
      m_Sema= -1;
    }
#endif
  
#ifndef ITK_USE_UNIX_IPC_SEMAPHORES

#ifdef ITK_USE_SPROC
  if (MultiThreader::GetThreadArena() != 0)
    {
    if (m_Sema != 0)
      {
      usfreesema(m_Sema, MultiThreader::GetThreadArena());
      }
    m_Sema = 0;
    }
#endif

#ifdef ITK_USE_PTHREADS
m_PThreadsSemaphoreRemoved = true;
#ifdef sun
  if ( sema_destroy(&m_Sema) != 0 )
    {
    itkExceptionMacro( << "sema_destroy call failed. " );
    }
#else

#ifndef __sgi
#ifndef __APPLE__
  // IRIX pthreads implementation of sem_destroy is buggy
  if ( sem_destroy(&m_Sema) != 0 )
    {
    itkExceptionMacro( << "sem_destroy call failed. " );
    }
#else //Still need to close semaphore and delete the file descriptor on MacOSX, otherwise the shared memory space is eventually exhosted.
  //Eventually (i.e. after several days of ITK regresssion testing) the semaphore creation process was failing with errno=ENOSPC
  //This implementation detail was taken from http://developer.apple.com/macosx/multithreadedprogramming.html
  if ( sem_close(this->m_Sema) !=0 )
      {
       itkExceptionMacro( << "sem_close call failed. " );
      }
  if ( sem_unlink(this->m_SemaphoreName.c_str()) !=0 )
      {
       itkExceptionMacro( << "sem_unlink call failed. " );
      }
#endif
#endif

#endif  // sun
#endif // pthreads
  
#endif // semaphores
  //#endif
  
#ifdef ITK_USE_WIN32_THREADS
  if (m_Sema != 0)
    {
    CloseHandle(m_Sema);
    }
  m_Sema= 0;
#endif
}

#ifdef ITK_USE_UNIX_IPC_SEMAPHORES

// UnixIpcSemaphoreCreate: will return the semaphore id (system wide)
// of the semaphore number (key) you give.
// If no semaphore has been established for this number, one is created.
int Semaphore::UnixIpcSemaphoreCreate(int unix_semaphore_key)
{
  int sid = -1;
  std::string s; 
 
  if( (sid = semget( (key_t)unix_semaphore_key, 1, 0666 | IPC_CREAT )) == -1 )
    {
    s =  "Error# %i in function UnixIpcSemaphoreCreate. - ";
    switch (errno)
      {
      case EEXIST:
        s += "Semaphore already exists. - ";
        break;
      case ENOSPC:
        s+= "System imposed limit on the number of semaphores is exceeded - ";
        break;
      case EACCES:
        s+= "Permission is denied - ";
        break;
      }
    itkExceptionMacro( << s.c_str() );
    }
  
  return( sid );
}

// UnixIpcSemaphoreDown: the semaphore signal operation.
// sid must be the system wide semaphore number
// returned by UnixIpcSemaphoreCreate above
void Semaphore::UnixIpcSemaphoreDown(int sid)
{
  Semaphore::UnixIpcSemaphoreCall(sid, -1);
}

// UnixIpcSemaphoreUp: the semaphore release operation.
// sid must be the system wide semaphore number
// returned by UnixIpcSemaphoreCreate above
void Semaphore::UnixIpcSemaphoreUp(int sid)
{
  Semaphore::UnixIpcSemaphoreCall(sid, 1);
}

// UixIpcSemaphoreRemove: remove a semaphore from the system.
// sid must be the system wide semaphore number
// returned from UnixIpcSemaphoreCreate
void Semaphore::UnixIpcSemaphoreRemove(int sid)
{
  std::string s;
  
  if( semctl( sid, 0, IPC_RMID, 0 ) == -1 )
    {
    s =  "Error removing semaphore %i - ";
    switch (errno)
      {
      case EINVAL:
        s +=  "Semaphore id# is not valid. - ";
        break;
      case EACCES:
        s+=  "Permission is denied - ";
        break;
      case EPERM:
        s+= "Permission is denied - ";
        break;
      }
    itkExceptionMacro ( << s.c_str() );
    }
}

// UnixIpcSemaphoreCall: makes the system call semop for your given
// semaphore and operation.
void Semaphore::UnixIpcSemaphoreCall (int sid, int op)
{
  struct sembuf sb;
  std::string s;
  
  sb.sem_num = 0;
  sb.sem_op = op;
  sb.sem_flg = 0;
  if( semop( sid, &sb, 1 ) == -1 )
    {
    
    if( op == -1 )
      {
      s =  "Error %i in UnixIpcSemaphoreDown call for semaphore %i - ";
      }
    else
      {
      s =  "Error %i in UnixIpcSemaphoreUp call for semaphore %i - ";
      }
    switch (errno)
      {
      case EINVAL:
        s +=  "Semaphore id# is not valid. -";
        break;
      case EFBIG:
        s +=  "Invalid sem_num for semaphore - ";
        break;
      case EACCES:
        s += "Permission denied for semaphore - ";
        break;
      case EAGAIN:
        s += "The operation would result in suspension of the calling process but NOWAIT is true.";
        break;
      case EIDRM:
        s += "The semid is removed from the system.";
        break;
      }
    itkExceptionMacro( << s.c_str() );
    }
}

#endif

}//end if namespace itk