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 - 2014 Christian Schoenebeck * |
* Copyright (C) 2005 - 2017 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 * |
46 |
*/ |
*/ |
47 |
class Mutex { |
class Mutex { |
48 |
public: |
public: |
49 |
/** |
enum type_t { |
50 |
* Constructor |
RECURSIVE, |
51 |
|
NON_RECURSIVE |
52 |
|
}; |
53 |
|
|
54 |
|
/** @brief Constructor |
55 |
|
* |
56 |
|
* Creates a new Mutex object. The optional @a type argument defines |
57 |
|
* the fundamental behavior of the Mutex object: |
58 |
|
* |
59 |
|
* - If @c RECURSIVE is passed (which is the default type) then the |
60 |
|
* mutex will manage an additional lock count such that it allows the |
61 |
|
* same thread to call Lock() multiple times; each time that thread |
62 |
|
* calls Lock() the lock count will be increased by one, each time it |
63 |
|
* calls Unlock() it will be decreased by one, and other threads will |
64 |
|
* only be unblocked once the lock count fell to zero again. |
65 |
|
* |
66 |
|
* - If @c NON_RECURSIVE is passed then it is considered to be an error |
67 |
|
* if the same thread calls Lock() while already owning the lock, and |
68 |
|
* likewise it is considered to be an error if Unlock() is called if |
69 |
|
* the calling thread hasn't locked the mutex. |
70 |
|
* |
71 |
|
* You should invest the required time to review your design in order to |
72 |
|
* decide which mutex behavior fits to your design. Even though it might |
73 |
|
* be tempting to stick with the lazy approach by using the @c RECURSIVE |
74 |
|
* type, using the @c NON_RECURSIVE type does make sense if your design |
75 |
|
* does not require a recursive mutex, because modern developer tools |
76 |
|
* assist you spotting potential threading bugs in your code while using |
77 |
|
* the @c NON_RECURSIVE type which can avoid developers' biggest fear of |
78 |
|
* undefined behavior, plus also keep in mind that certain OS APIs are |
79 |
|
* not compatible with recursive mutexes at all! |
80 |
|
* |
81 |
|
* @param type - optional: the fundamental behavior type for this mutex |
82 |
|
* (default: @c RECURSIVE) |
83 |
*/ |
*/ |
84 |
Mutex(); |
Mutex(type_t type = RECURSIVE); |
85 |
|
|
86 |
/** |
/** |
87 |
* Destructor |
* Destructor |
128 |
pthread_mutex_t __posix_mutex; |
pthread_mutex_t __posix_mutex; |
129 |
pthread_mutexattr_t __posix_mutexattr; |
pthread_mutexattr_t __posix_mutexattr; |
130 |
#endif |
#endif |
131 |
|
type_t type; |
132 |
}; |
}; |
133 |
|
|
134 |
// Lock guard for exception safe locking |
// Lock guard for exception safe locking |