File: request_with_waiters.cpp

package info (click to toggle)
libstxxl 1.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 45,101; ansic: 4,071; perl: 610; sh: 555; xml: 174; makefile: 18
file content (64 lines) | stat: -rw-r--r-- 1,834 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/***************************************************************************
 *  lib/io/request_with_waiters.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
 *  Copyright (C) 2008 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#include <stxxl/bits/common/mutex.h>
#include <stxxl/bits/common/onoff_switch.h>
#include <stxxl/bits/io/request_with_waiters.h>
#include <stxxl/bits/parallel.h>

#include <algorithm>
#include <functional>

STXXL_BEGIN_NAMESPACE

bool request_with_waiters::add_waiter(onoff_switch* sw)
{
    // this lock needs to be obtained before poll(), otherwise a race
    // condition might occur: the state might change and notify_waiters()
    // could be called between poll() and insert() resulting in waiter sw
    // never being notified
    scoped_mutex_lock lock(m_waiters_mutex);

    if (poll())                     // request already finished
    {
        return true;
    }

    m_waiters.insert(sw);

    return false;
}

void request_with_waiters::delete_waiter(onoff_switch* sw)
{
    scoped_mutex_lock lock(m_waiters_mutex);
    m_waiters.erase(sw);
}

void request_with_waiters::notify_waiters()
{
    scoped_mutex_lock lock(m_waiters_mutex);
    std::for_each(m_waiters.begin(),
                  m_waiters.end(),
                  std::mem_fun(&onoff_switch::on)
                  _STXXL_FORCE_SEQUENTIAL);
}

size_t request_with_waiters::num_waiters()
{
    scoped_mutex_lock lock(m_waiters_mutex);
    return m_waiters.size();
}

STXXL_END_NAMESPACE
// vim: et:ts=4:sw=4