/[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 275 by schoenebeck, Sat Oct 9 15:40:35 2004 UTC revision 3054 by schoenebeck, Thu Dec 15 12:47:45 2016 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6     *   Copyright (C) 2005 - 2016 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 20  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
24  #include <iostream>  #ifdef HAVE_CONFIG_H
25    # include <config.h>
26    #endif
27    
28    #if AC_APPLE_UNIVERSAL_BUILD // maybe do this include for all Apple ones (defined(__APPLE__))?
29    # include <sys/cdefs.h> // defines the system macros checked below
30    #endif
31    
32    #ifndef _GNU_SOURCE
33    # define _GNU_SOURCE 1 /* so _XOPEN_SOURCE will be defined by features.h */
34    #endif
35    
36    #ifdef HAVE_FEATURES_H
37    # include <features.h>
38    #endif
39    
40    #if !defined(WIN32)
41    # if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 500
42    #  undef _XOPEN_SOURCE
43    #  define _XOPEN_SOURCE 500 /* to define PTHREAD_MUTEX_ERRORCHECK */
44    #  if (!defined(POSIX_C_SOURCE) || POSIX_C_SOURCE < 199801L) && !defined(__DARWIN_UNIX03)
45    #   warning "Seems you don't have a UNIX98 compatible system."
46    #   warning "Please run LinuxSampler's selftest to make sure this won't be a problem!"
47    #   warning "(compile tests with 'make tests', run them with 'src/testcases/linuxsamplertest')"
48    #  endif
49    # endif
50    #endif
51    
52    #include <iostream>
53  #include <errno.h>  #include <errno.h>
54    #include <stdlib.h> /* for exit(int) */
55    
56  #include "Mutex.h"  #include "Mutex.h"
57    
58    namespace LinuxSampler {
59    
60  Mutex::Mutex() {  Mutex::Mutex() {
61    #if defined(WIN32)
62        hMutex = CreateMutex( NULL, FALSE, NULL);
63        if (hMutex == NULL) {
64            std::cerr << "Mutex Constructor: Fatal error - CreateMutex error " << GetLastError() << "\n";
65            exit(1);
66        }
67    #else
68        pthread_mutexattr_init(&__posix_mutexattr);
69    
70      // the following function call only works on UNIX98 compatible systems      // the following function call only works on UNIX98 compatible systems
71        #if (_XOPEN_SOURCE > 500) || defined(__APPLE__)
72            // Mac OS X Note: 10.4 (and later) does support PTHREAD_MUTEX_ERRORCHECK, and
73            // actually LinuxSampler does not work if this call is omitted. However,
74            // defining _XOPEN_SOURCE macro seems to cause other problems. As a workaround,
75            // the symbol __APPLE__ is checked here. There should be a better solution
76            // to this problem. (Toshi Nagata, 27 Mar 2007)
77      if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) {      if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) {
78          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;
79          exit(-1);          exit(-1);
80      }      }
81        #endif
82      pthread_mutex_init(&__posix_mutex, &__posix_mutexattr);      pthread_mutex_init(&__posix_mutex, &__posix_mutexattr);
83    #endif
84  }  }
85    
86  Mutex::~Mutex() {  Mutex::~Mutex() {
87    #if defined(WIN32)
88        CloseHandle(hMutex);
89    #else
90      pthread_mutex_destroy(&__posix_mutex);      pthread_mutex_destroy(&__posix_mutex);
91        pthread_mutexattr_destroy(&__posix_mutexattr);
92    #endif
93  }  }
94    
95  void Mutex::Lock() {  void Mutex::Lock() {
96    #if defined(WIN32)
97        WaitForSingleObject(hMutex, INFINITE);
98    #else
99      pthread_mutex_lock(&__posix_mutex);      pthread_mutex_lock(&__posix_mutex);
100    #endif
101  }  }
102    
103  bool Mutex::Trylock() {  bool Mutex::Trylock() {
104    #if defined(WIN32)
105        if( WaitForSingleObject(hMutex, 0) == WAIT_TIMEOUT) return false;
106        return true;
107    #else
108      if (pthread_mutex_trylock(&__posix_mutex) == EBUSY)      if (pthread_mutex_trylock(&__posix_mutex) == EBUSY)
109              return false;          return false;
110      return true;      return true;
111    #endif
112  }  }
113    
114  void Mutex::Unlock() {  void Mutex::Unlock() {
115    #if defined(WIN32)
116        ReleaseMutex(hMutex);
117    #else
118      pthread_mutex_unlock(&__posix_mutex);      pthread_mutex_unlock(&__posix_mutex);
119    #endif
120  }  }
121    
122    } // namespace LinuxSampler

Legend:
Removed from v.275  
changed lines
  Added in v.3054

  ViewVC Help
Powered by ViewVC