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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3455 - (show annotations) (download)
Sun Jan 27 10:07:54 2019 UTC (5 years, 2 months ago) by persson
File size: 4554 byte(s)
* Fixed some compiler warnings
* Fixed compilation error when cross-compiling to Mac

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2019 Christian Schoenebeck *
7 * *
8 * 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 *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #if 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_RECURSIVE */
44 # if (!defined(POSIX_C_SOURCE) || POSIX_C_SOURCE < 199801L) && !__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>
54 #include <stdlib.h> /* for exit(int) */
55
56 #include "Mutex.h"
57
58 namespace LinuxSampler {
59
60 Mutex::Mutex(type_t type) {
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 // pthread_mutexattr_settype() only works on UNIX98 compatible systems
70 switch (type) {
71 case RECURSIVE:
72 if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_RECURSIVE)) {
73 std::cerr << "Mutex Constructor: Fatal error - unable to pthread_mutexattr_settype(PTHREAD_MUTEX_RECURSIVE)\n" << std::flush;
74 exit(-1);
75 }
76 break;
77 case NON_RECURSIVE:
78 if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) {
79 std::cerr << "Mutex Constructor: Fatal error - unable to pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK)\n" << std::flush;
80 exit(-1);
81 }
82 break;
83 default:
84 std::cerr << "Mutex Constructor: Fatal error - Unknown mutex type requested\n" << std::flush;
85 exit(-1);
86 break;
87 }
88 pthread_mutex_init(&__posix_mutex, &__posix_mutexattr);
89 #endif
90 }
91
92 Mutex::~Mutex() {
93 #if defined(WIN32)
94 CloseHandle(hMutex);
95 #else
96 pthread_mutex_destroy(&__posix_mutex);
97 pthread_mutexattr_destroy(&__posix_mutexattr);
98 #endif
99 }
100
101 void Mutex::Lock() {
102 #if defined(WIN32)
103 WaitForSingleObject(hMutex, INFINITE);
104 #else
105 pthread_mutex_lock(&__posix_mutex);
106 #endif
107 }
108
109 bool Mutex::Trylock() {
110 #if defined(WIN32)
111 if( WaitForSingleObject(hMutex, 0) == WAIT_TIMEOUT) return false;
112 return true;
113 #else
114 if (pthread_mutex_trylock(&__posix_mutex) == EBUSY)
115 return false;
116 return true;
117 #endif
118 }
119
120 void Mutex::Unlock() {
121 #if defined(WIN32)
122 ReleaseMutex(hMutex);
123 #else
124 pthread_mutex_unlock(&__posix_mutex);
125 #endif
126 }
127
128 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC