Stxxl  1.4.0
io/request_with_waiters.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  io/request_with_waiters.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2008 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #include <stxxl/bits/io/request_with_waiters.h>
00015 #include <algorithm>
00016 #include <stxxl/bits/parallel.h>
00017 
00018 
00019 __STXXL_BEGIN_NAMESPACE
00020 
00021 bool request_with_waiters::add_waiter(onoff_switch * sw)
00022 {
00023     // this lock needs to be obtained before poll(), otherwise a race
00024     // condition might occur: the state might change and notify_waiters()
00025     // could be called between poll() and insert() resulting in waiter sw
00026     // never being notified
00027     scoped_mutex_lock lock(waiters_mutex);
00028 
00029     if (poll())                     // request already finished
00030     {
00031         return true;
00032     }
00033 
00034     waiters.insert(sw);
00035 
00036     return false;
00037 }
00038 
00039 void request_with_waiters::delete_waiter(onoff_switch * sw)
00040 {
00041     scoped_mutex_lock lock(waiters_mutex);
00042     waiters.erase(sw);
00043 }
00044 
00045 void request_with_waiters::notify_waiters()
00046 {
00047     scoped_mutex_lock lock(waiters_mutex);
00048     std::for_each(waiters.begin(),
00049                   waiters.end(),
00050                   std::mem_fun(&onoff_switch::on)
00051                   _STXXL_FORCE_SEQUENTIAL);
00052 }
00053 
00054 /*
00055 int request_with_waiters::nwaiters()
00056 {
00057     scoped_mutex_lock lock(waiters_mutex);
00058     return waiters.size();
00059 }
00060 */
00061 
00062 __STXXL_END_NAMESPACE
00063 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines