STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
request_with_waiters.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/io/request_with_waiters.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
17 #include <stxxl/bits/parallel.h>
18 
19 #include <algorithm>
20 #include <functional>
21 
23 
24 bool request_with_waiters::add_waiter(onoff_switch* sw)
25 {
26  // this lock needs to be obtained before poll(), otherwise a race
27  // condition might occur: the state might change and notify_waiters()
28  // could be called between poll() and insert() resulting in waiter sw
29  // never being notified
30  scoped_mutex_lock lock(m_waiters_mutex);
31 
32  if (poll()) // request already finished
33  {
34  return true;
35  }
36 
37  m_waiters.insert(sw);
38 
39  return false;
40 }
41 
42 void request_with_waiters::delete_waiter(onoff_switch* sw)
43 {
44  scoped_mutex_lock lock(m_waiters_mutex);
45  m_waiters.erase(sw);
46 }
47 
48 void request_with_waiters::notify_waiters()
49 {
50  scoped_mutex_lock lock(m_waiters_mutex);
51  std::for_each(m_waiters.begin(),
52  m_waiters.end(),
53  std::mem_fun(&onoff_switch::on)
55 }
56 
57 size_t request_with_waiters::num_waiters()
58 {
59  scoped_mutex_lock lock(m_waiters_mutex);
60  return m_waiters.size();
61 }
62 
64 // vim: et:ts=4:sw=4
UnaryFunction for_each(ExtIterator begin, ExtIterator end, UnaryFunction functor, int_type nbuffers=0)
External equivalent of std::for_each, see stxxl::for_each.
Definition: scan.h:50
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:105
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define _STXXL_FORCE_SEQUENTIAL
Definition: parallel.h:50
#define STXXL_END_NAMESPACE
Definition: namespace.h:17