Stxxl  1.4.0
include/stxxl/bits/io/disk_queues.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/io/disk_queues.h
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-2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *  Copyright (C) 2009 Johannes Singler <singler@ira.uka.de>
00009  *
00010  *  Distributed under the Boost Software License, Version 1.0.
00011  *  (See accompanying file LICENSE_1_0.txt or copy at
00012  *  http://www.boost.org/LICENSE_1_0.txt)
00013  **************************************************************************/
00014 
00015 #ifndef STXXL_IO_DISK_QUEUES_HEADER
00016 #define STXXL_IO_DISK_QUEUES_HEADER
00017 
00018 #include <map>
00019 
00020 #include <stxxl/bits/namespace.h>
00021 #include <stxxl/bits/singleton.h>
00022 #include <stxxl/bits/io/iostats.h>
00023 #include <stxxl/bits/io/request_queue_impl_qwqr.h>
00024 
00025 
00026 __STXXL_BEGIN_NAMESPACE
00027 
00028 //! \addtogroup iolayer
00029 //! \{
00030 
00031 class request_ptr;
00032 
00033 //! \brief Encapsulates disk queues
00034 //! \remark is a singleton
00035 class disk_queues : public singleton<disk_queues>
00036 {
00037     friend class singleton<disk_queues>;
00038 
00039     // 2 queues: write queue and read queue
00040     typedef request_queue_impl_qwqr request_queue_type;
00041 
00042     typedef stxxl::int64 DISKID;
00043     typedef std::map<DISKID, request_queue_type *> request_queue_map;
00044 
00045 protected:
00046     request_queue_map queues;
00047     disk_queues()
00048     {
00049         stxxl::stats::get_instance(); // initialize stats before ourselves
00050     }
00051 
00052 public:
00053     void add_request(request_ptr & req, DISKID disk)
00054     {
00055 #ifdef STXXL_HACK_SINGLE_IO_THREAD
00056         disk = 42;
00057 #endif
00058         if (queues.find(disk) == queues.end())
00059         {
00060             // create new request queue
00061             queues[disk] = new request_queue_type();
00062         }
00063         queues[disk]->add_request(req);
00064     }
00065 
00066     //! \brief Cancel a request
00067     //! The specified request is canceled unless already being processed.
00068     //! However, cancelation cannot be guaranteed.
00069     //! Cancelled requests must still be waited for in order to ensure correct
00070     //! operation.
00071     //! \param req request to cancel
00072     //! \param disk disk number for disk that \c req was scheduled on
00073     //! \return \c true iff the request was canceled successfully
00074     bool cancel_request(request_ptr & req, DISKID disk)
00075     {
00076 #ifdef STXXL_HACK_SINGLE_IO_THREAD
00077         disk = 42;
00078 #endif
00079         if (queues.find(disk) != queues.end())
00080             return queues[disk]->cancel_request(req);
00081         else
00082             return false;
00083     }
00084 
00085     ~disk_queues()
00086     {
00087         // deallocate all queues
00088         for (request_queue_map::iterator i = queues.begin(); i != queues.end(); i++)
00089             delete (*i).second;
00090     }
00091 
00092     //! \brief Changes requests priorities
00093     //! \param op one of:
00094     //!                 - READ, read requests are served before write requests within a disk queue
00095     //!                 - WRITE, write requests are served before read requests within a disk queue
00096     //!                 - NONE, read and write requests are served by turns, alternately
00097     void set_priority_op(request_queue::priority_op op)
00098     {
00099         for (request_queue_map::iterator i = queues.begin(); i != queues.end(); i++)
00100             i->second->set_priority_op(op);
00101     }
00102 };
00103 
00104 //! \}
00105 
00106 __STXXL_END_NAMESPACE
00107 
00108 #endif // !STXXL_IO_DISK_QUEUES_HEADER
00109 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines