ardour
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
libs
pbd
glib_semaphore.cc
Go to the documentation of this file.
1
/*
2
Copyright (C) 2010 Tim Mayberry
3
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18
*/
19
20
#include "
pbd/glib_semaphore.h
"
21
22
namespace
PBD
{
23
24
GlibSemaphore::GlibSemaphore
(gint initial_val)
25
:
26
m_counter(initial_val)
27
{ }
28
29
void
30
GlibSemaphore::wait
()
31
{
32
Glib::Threads::Mutex::Lock
guard (
m_mutex
);
33
34
while
(
m_counter
.
get
() < 1) {
35
m_cond
.wait(
m_mutex
);
36
}
37
38
// this shouldn't need to be inside the lock
39
--
m_counter
;
40
}
41
42
bool
43
GlibSemaphore::try_wait
()
44
{
45
if
(!
m_mutex
.trylock())
46
{
47
return
false
;
48
}
49
// lock successful
50
while
(
m_counter
.
get
() < 1) {
51
m_cond
.wait(
m_mutex
);
52
}
53
54
// the order of these should not matter
55
--
m_counter
;
56
m_mutex
.unlock();
57
return
true
;
58
}
59
60
void
61
GlibSemaphore::post
()
62
{
63
// atomic, no locking required
64
++
m_counter
;
65
m_cond
.signal();
66
}
67
68
}
// namespace PBD
PBD::GlibSemaphore::m_mutex
Glib::Threads::Mutex m_mutex
Definition:
glib_semaphore.h:51
PBD::GlibSemaphore::wait
void wait()
Definition:
glib_semaphore.cc:30
PBD::GlibSemaphore::GlibSemaphore
GlibSemaphore(const GlibSemaphore &sema)
PBD::GlibSemaphore::post
void post()
Definition:
glib_semaphore.cc:61
glib_semaphore.h
PBD::atomic_counter::get
gint get() const
Definition:
atomic_counter.h:42
PBD::GlibSemaphore::try_wait
bool try_wait()
Definition:
glib_semaphore.cc:43
PBD::GlibSemaphore::m_cond
Glib::Threads::Cond m_cond
Definition:
glib_semaphore.h:50
PBD::GlibSemaphore::m_counter
atomic_counter m_counter
Definition:
glib_semaphore.h:49
ARDOUR::Lock
Definition:
types.h:355
PBD
Definition:
debug.h:30
Generated on Sun May 24 2015 12:15:08 for ardour by
1.8.8