ardour
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
31 {
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
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
62 {
63  // atomic, no locking required
64  ++m_counter;
65  m_cond.signal();
66 }
67 
68 } // namespace PBD
Glib::Threads::Mutex m_mutex
GlibSemaphore(const GlibSemaphore &sema)
gint get() const
Glib::Threads::Cond m_cond
atomic_counter m_counter
Definition: debug.h:30