Thrill  0.1
request_with_waiters.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/io/request_with_waiters.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
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 
14 #include <algorithm>
15 #include <functional>
16 #include <mutex>
17 
20 
21 namespace foxxll {
22 
24 {
25  // this lock needs to be obtained before poll(), otherwise a race
26  // condition might occur: the state might change and notify_waiters()
27  // could be called between poll() and insert() resulting in waiter sw
28  // never being notified
29  std::unique_lock<std::mutex> lock(waiters_mutex_);
30 
31  if (poll()) // request already finished
32  {
33  return true;
34  }
35 
36  waiters_.insert(sw);
37 
38  return false;
39 }
40 
42 {
43  std::unique_lock<std::mutex> lock(waiters_mutex_);
44  waiters_.erase(sw);
45 }
46 
48 {
49  std::unique_lock<std::mutex> lock(waiters_mutex_);
50  std::for_each(
51  waiters_.begin(), waiters_.end(),
52  std::mem_fun(&onoff_switch::on)
53  );
54 }
55 
57 {
58  std::unique_lock<std::mutex> lock(waiters_mutex_);
59  return waiters_.size();
60 }
61 
62 } // namespace foxxll
63 
64 /**************************************************************************/
size_t num_waiters()
returns number of waiters
void on()
turn switch ON and notify one waiter
virtual bool poll()=0
bool add_waiter(onoff_switch *sw) final
FOXXLL library namespace
void delete_waiter(onoff_switch *sw) final
std::set< onoff_switch * > waiters_