--- linuxsampler/trunk/src/common/Mutex.cpp 2004/04/26 17:15:51 53 +++ linuxsampler/trunk/src/common/Mutex.cpp 2005/02/09 01:22:18 361 @@ -2,7 +2,7 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -20,15 +20,34 @@ * MA 02111-1307 USA * ***************************************************************************/ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE 1 /* so _XOPEN_SOURCE will be defined by features.h */ +#endif + +#include + +#if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 500 +# undef _XOPEN_SOURCE +# define _XOPEN_SOURCE 500 /* to define PTHREAD_MUTEX_ERRORCHECK */ +# warning "Seems you don't have a UNIX98 compatible system." +# warning "Please run LinuxSampler's selftest to make sure this won't be a problem!" +# warning "(compile tests with 'make tests', run them with 'src/testcases/linuxsamplertest')" +#endif + #include +#include +#include /* for exit(int) */ #include "Mutex.h" Mutex::Mutex() { + // the following function call only works on UNIX98 compatible systems + #if (_XOPEN_SOURCE > 500) if (pthread_mutexattr_settype(&__posix_mutexattr, PTHREAD_MUTEX_ERRORCHECK)) { std::cout << "Mutex Constructor: Fatal error - unable to pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK)\n" << std::flush; exit(-1); } + #endif pthread_mutex_init(&__posix_mutex, &__posix_mutexattr); } @@ -40,6 +59,12 @@ pthread_mutex_lock(&__posix_mutex); } +bool Mutex::Trylock() { + if (pthread_mutex_trylock(&__posix_mutex) == EBUSY) + return false; + return true; +} + void Mutex::Unlock() { pthread_mutex_unlock(&__posix_mutex); }