File: Thread.cpp

package info (click to toggle)
imagemagick 6:6.0.6.2-2.9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 33,284 kB
  • ctags: 14,844
  • sloc: ansic: 190,790; cpp: 17,203; sh: 8,740; perl: 4,190; makefile: 1,740; tcl: 459
file content (106 lines) | stat: -rw-r--r-- 2,697 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
// This may look like C code, but it is really -*- C++ -*-
//
// Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002
//
// Implementation of thread support
//

#define MAGICK_IMPLEMENTATION

#include "Magick++/Thread.h"
#include "Magick++/Exception.h"

#include <string.h>

// Default constructor
Magick::MutexLock::MutexLock(void)
#if defined(HasPTHREADS)
  // POSIX threads
  : _mutex()
{
  ::pthread_mutexattr_t attr;
  int sysError;
  if ( (sysError = ::pthread_mutexattr_init( &attr )) == 0 )
    if ( (sysError = ::pthread_mutex_init( &_mutex, &attr )) == 0 )
      {
        ::pthread_mutexattr_destroy( &attr );
        return;
      }
  throwExceptionExplicit( OptionError, "mutex initialization failed",
                          strerror(sysError) );
}
#else
#if defined(_VISUALC_) && defined(_MT)
// Win32 threads
  : _mutex()
{
  SECURITY_ATTRIBUTES security;

  /* Allow the semaphore to be inherited */
  security.nLength = sizeof(security);
  security.lpSecurityDescriptor = NULL;
  security.bInheritHandle = TRUE;

  /* Create the semaphore, with initial value signaled */
  _mutex.id = ::CreateSemaphore(&security, 1, MAXSEMLEN, NULL);
  if ( _mutex.id != NULL )
    return;
  throwExceptionExplicit( OptionError, "mutex initialization failed" );
}
#else
// Threads not supported
{
}
#endif
#endif

// Destructor
Magick::MutexLock::~MutexLock(void)
{
#if defined(HasPTHREADS)
  int sysError;
  if ( (sysError = ::pthread_mutex_destroy( &_mutex )) == 0 )
    return;
  throwExceptionExplicit( OptionError, "mutex destruction failed",
                          strerror(sysError) );
#endif
#if defined(_MT) && defined(_VISUALC_)
  if ( ::CloseHandle(_mutex.id) != 0 )
    return;
  throwExceptionExplicit( OptionError, "mutex destruction failed" );
#endif
}

// Lock mutex
void Magick::MutexLock::lock(void)
{
#if defined(HasPTHREADS)
  int sysError;
  if ( (sysError = ::pthread_mutex_lock( &_mutex )) == 0)
    return;
  throwExceptionExplicit( OptionError, "mutex lock failed",
                          strerror(sysError));
#endif
#if defined(_MT) && defined(_VISUALC_)
  if (WaitForSingleObject(_mutex.id,INFINITE) != WAIT_FAILED)
    return;
  throwExceptionExplicit( OptionError, "mutex lock failed" );
#endif
}

// Unlock mutex
void Magick::MutexLock::unlock(void)
{
#if defined(HasPTHREADS)
  int sysError;
  if ( (sysError = ::pthread_mutex_unlock( &_mutex )) == 0)
    return;
  throwExceptionExplicit( OptionError, "mutex unlock failed",
                          strerror(sysError) );
#endif
#if defined(_MT) && defined(_VISUALC_)
  if ( ReleaseSemaphore(_mutex.id, 1, NULL) == TRUE )
    return;
  throwExceptionExplicit( OptionError, "mutex unlock failed" );
#endif
}