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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1481 - (hide annotations) (download)
Wed Nov 14 23:42:15 2007 UTC (16 years, 5 months ago) by senoner
File size: 4249 byte(s)
* win32 port work in progress:
* - implemented win32 support in the following classes:
* Thread, Condition, Mutex, Path, LscpServer
* - lscp.y use DONTCARE instead of VOID
*  (a win32 symbol defined)
* - completed win32 editor plugin loader

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 1149 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 schoenebeck 53 * *
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 persson 830 #ifdef HAVE_CONFIG_H
25     # include <config.h>
26     #endif
27    
28 schoenebeck 289 #ifndef _GNU_SOURCE
29     # define _GNU_SOURCE 1 /* so _XOPEN_SOURCE will be defined by features.h */
30     #endif
31    
32 persson 830 #ifdef HAVE_FEATURES_H
33 wylder 819 # include <features.h>
34     #endif
35 schoenebeck 289
36 senoner 1481 #if !defined(WIN32)
37 schoenebeck 289 #if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 500
38     # undef _XOPEN_SOURCE
39     # define _XOPEN_SOURCE 500 /* to define PTHREAD_MUTEX_ERRORCHECK */
40 senkov 290 # warning "Seems you don't have a UNIX98 compatible system."
41     # 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')"
43 schoenebeck 289 #endif
44 senoner 1481 #endif
45 schoenebeck 289
46 schoenebeck 53 #include <iostream>
47 capela 268 #include <errno.h>
48 schoenebeck 281 #include <stdlib.h> /* for exit(int) */
49 capela 268
50 schoenebeck 53 #include "Mutex.h"
51    
52 schoenebeck 880 namespace LinuxSampler {
53    
54 schoenebeck 53 Mutex::Mutex() {
55 senoner 1481 #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 persson 932 pthread_mutexattr_init(&__posix_mutexattr);
63    
64 schoenebeck 275 // the following function call only works on UNIX98 compatible systems
65 schoenebeck 1149 #if (_XOPEN_SOURCE > 500) || defined(__APPLE__)
66     // Mac OS X Note: 10.4 (and later) does support PTHREAD_MUTEX_ERRORCHECK, and
67     // actually LinuxSampler does not work if this call is omitted. However,
68     // defining _XOPEN_SOURCE macro seems to cause other problems. As a workaround,
69     // the symbol __APPLE__ is checked here. There should be a better solution
70     // to this problem. (Toshi Nagata, 27 Mar 2007)
71 schoenebeck 53 if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) {
72 senoner 1481 std::cerr << "Mutex Constructor: Fatal error - unable to pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK)\n" << std::flush;
73 schoenebeck 53 exit(-1);
74     }
75 schoenebeck 361 #endif
76 schoenebeck 53 pthread_mutex_init(&__posix_mutex, &__posix_mutexattr);
77 senoner 1481 #endif
78 schoenebeck 53 }
79    
80     Mutex::~Mutex() {
81 senoner 1481 #if defined(WIN32)
82     CloseHandle(hMutex);
83     #else
84 schoenebeck 53 pthread_mutex_destroy(&__posix_mutex);
85 persson 932 pthread_mutexattr_destroy(&__posix_mutexattr);
86 senoner 1481 #endif
87 schoenebeck 53 }
88    
89     void Mutex::Lock() {
90 senoner 1481 #if defined(WIN32)
91     WaitForSingleObject(hMutex, INFINITE);
92     #else
93 schoenebeck 53 pthread_mutex_lock(&__posix_mutex);
94 senoner 1481 #endif
95 schoenebeck 53 }
96    
97 senkov 165 bool Mutex::Trylock() {
98 senoner 1481 #if defined(WIN32)
99     if( WaitForSingleObject(hMutex, 0) == WAIT_TIMEOUT) return false;
100     return true;
101     #else
102 senkov 165 if (pthread_mutex_trylock(&__posix_mutex) == EBUSY)
103 senoner 1481 return false;
104 senkov 165 return true;
105 senoner 1481 #endif
106 senkov 165 }
107    
108 schoenebeck 53 void Mutex::Unlock() {
109 senoner 1481 #if defined(WIN32)
110     ReleaseMutex(hMutex);
111     #else
112 schoenebeck 53 pthread_mutex_unlock(&__posix_mutex);
113 senoner 1481 #endif
114 schoenebeck 53 }
115 schoenebeck 880
116     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC