/[svn]/linuxsampler/trunk/src/common/Mutex.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/common/Mutex.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1149 by schoenebeck, Sat Apr 7 22:32:47 2007 UTC revision 1481 by senoner, Wed Nov 14 23:42:15 2007 UTC
# Line 33  Line 33 
33  # include <features.h>  # include <features.h>
34  #endif  #endif
35    
36    #if !defined(WIN32)
37  #if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 500  #if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 500
38  # undef _XOPEN_SOURCE  # undef _XOPEN_SOURCE
39  # define _XOPEN_SOURCE 500 /* to define PTHREAD_MUTEX_ERRORCHECK */  # define _XOPEN_SOURCE 500 /* to define PTHREAD_MUTEX_ERRORCHECK */
# Line 40  Line 41 
41  # warning "Please run LinuxSampler's selftest to make sure this won't be a problem!"  # warning "Please run LinuxSampler's selftest to make sure this won't be a problem!"
42  # warning "(compile tests with 'make tests', run them with 'src/testcases/linuxsamplertest')"  # warning "(compile tests with 'make tests', run them with 'src/testcases/linuxsamplertest')"
43  #endif  #endif
44    #endif
45    
46  #include <iostream>  #include <iostream>
47  #include <errno.h>  #include <errno.h>
# Line 50  Line 52 
52  namespace LinuxSampler {  namespace LinuxSampler {
53    
54  Mutex::Mutex() {  Mutex::Mutex() {
55    #if defined(WIN32)
56        hMutex = CreateMutex( NULL, FALSE, NULL);
57        if (hMutex == NULL) {
58            std::cerr << "Mutex Constructor: Fatal error - CreateMutex error " << GetLastError() << "\n";
59            exit(1);
60        }
61    #else
62      pthread_mutexattr_init(&__posix_mutexattr);      pthread_mutexattr_init(&__posix_mutexattr);
63    
64      // the following function call only works on UNIX98 compatible systems      // the following function call only works on UNIX98 compatible systems
# Line 60  Mutex::Mutex() { Line 69  Mutex::Mutex() {
69          // the symbol __APPLE__ is checked here. There should be a better solution          // the symbol __APPLE__ is checked here. There should be a better solution
70          // to this problem. (Toshi Nagata, 27 Mar 2007)          // to this problem. (Toshi Nagata, 27 Mar 2007)
71      if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) {      if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) {
72          std::cout << "Mutex Constructor: Fatal error - unable to pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK)\n" << std::flush;          std::cerr << "Mutex Constructor: Fatal error - unable to pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK)\n" << std::flush;
73          exit(-1);          exit(-1);
74      }      }
75      #endif      #endif
76      pthread_mutex_init(&__posix_mutex, &__posix_mutexattr);      pthread_mutex_init(&__posix_mutex, &__posix_mutexattr);
77    #endif
78  }  }
79    
80  Mutex::~Mutex() {  Mutex::~Mutex() {
81    #if defined(WIN32)
82        CloseHandle(hMutex);
83    #else
84      pthread_mutex_destroy(&__posix_mutex);      pthread_mutex_destroy(&__posix_mutex);
85      pthread_mutexattr_destroy(&__posix_mutexattr);      pthread_mutexattr_destroy(&__posix_mutexattr);
86    #endif
87  }  }
88    
89  void Mutex::Lock() {  void Mutex::Lock() {
90    #if defined(WIN32)
91        WaitForSingleObject(hMutex, INFINITE);
92    #else
93      pthread_mutex_lock(&__posix_mutex);      pthread_mutex_lock(&__posix_mutex);
94    #endif
95  }  }
96    
97  bool Mutex::Trylock() {  bool Mutex::Trylock() {
98    #if defined(WIN32)
99        if( WaitForSingleObject(hMutex, 0) == WAIT_TIMEOUT) return false;
100        return true;
101    #else
102      if (pthread_mutex_trylock(&__posix_mutex) == EBUSY)      if (pthread_mutex_trylock(&__posix_mutex) == EBUSY)
103              return false;          return false;
104      return true;      return true;
105    #endif
106  }  }
107    
108  void Mutex::Unlock() {  void Mutex::Unlock() {
109    #if defined(WIN32)
110        ReleaseMutex(hMutex);
111    #else
112      pthread_mutex_unlock(&__posix_mutex);      pthread_mutex_unlock(&__posix_mutex);
113    #endif
114  }  }
115    
116  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.1149  
changed lines
  Added in v.1481

  ViewVC Help
Powered by ViewVC