STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
disk_queues.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/io/disk_queues.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008-2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2009 Johannes Singler <[email protected]>
9  * Copyright (C) 2014 Timo Bingmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_IO_DISK_QUEUES_HEADER
17 #define STXXL_IO_DISK_QUEUES_HEADER
18 
19 #include <map>
20 
21 #include <stxxl/bits/namespace.h>
22 #include <stxxl/bits/singleton.h>
23 #include <stxxl/bits/io/iostats.h>
24 #include <stxxl/bits/io/request.h>
29 
31 
32 //! \addtogroup reqlayer
33 //! \{
34 
35 //! Encapsulates disk queues.
36 //! \remark is a singleton
37 class disk_queues : public singleton<disk_queues>
38 {
39  friend class singleton<disk_queues>;
40 
42  typedef std::map<DISKID, request_queue*> request_queue_map;
43 
44 protected:
47  {
48  stxxl::stats::get_instance(); // initialize stats before ourselves
49  }
50 
51 public:
52  void add_request(request_ptr& req, DISKID disk)
53  {
54 #ifdef STXXL_HACK_SINGLE_IO_THREAD
55  disk = 42;
56 #endif
57  request_queue_map::iterator qi = queues.find(disk);
58  request_queue* q;
59  if (qi == queues.end())
60  {
61  // create new request queue
62 #if STXXL_HAVE_LINUXAIO_FILE
63  if (dynamic_cast<linuxaio_request*>(req.get()))
64  q = queues[disk] = new linuxaio_queue(
65  dynamic_cast<linuxaio_file*>(req->get_file())->get_desired_queue_length()
66  );
67  else
68 #endif
69  q = queues[disk] = new request_queue_impl_qwqr();
70  }
71  else
72  q = qi->second;
73 
74  q->add_request(req);
75  }
76 
77  //! Cancel a request.
78  //! The specified request is canceled unless already being processed.
79  //! However, cancelation cannot be guaranteed.
80  //! Cancelled requests must still be waited for in order to ensure correct
81  //! operation.
82  //! \param req request to cancel
83  //! \param disk disk number for disk that \c req was scheduled on
84  //! \return \c true iff the request was canceled successfully
86  {
87 #ifdef STXXL_HACK_SINGLE_IO_THREAD
88  disk = 42;
89 #endif
90  if (queues.find(disk) != queues.end())
91  return queues[disk]->cancel_request(req);
92  else
93  return false;
94  }
95 
97  {
98  if (queues.find(disk) != queues.end())
99  return queues[disk];
100  else
101  return NULL;
102  }
103 
105  {
106  // deallocate all queues
107  for (request_queue_map::iterator i = queues.begin(); i != queues.end(); i++)
108  delete (*i).second;
109  }
110 
111  //! Changes requests priorities.
112  //! \param op one of:
113  //! - READ, read requests are served before write requests within a disk queue
114  //! - WRITE, write requests are served before read requests within a disk queue
115  //! - NONE, read and write requests are served by turns, alternately
117  {
118  for (request_queue_map::iterator i = queues.begin(); i != queues.end(); i++)
119  i->second->set_priority_op(op);
120  }
121 };
122 
123 //! \}
124 
126 
127 #endif // !STXXL_IO_DISK_QUEUES_HEADER
128 // vim: et:ts=4:sw=4
Interface of a request_queue to which requests can be added and canceled.
Definition: request_queue.h:26
Implementation of a local request queue having two queues, one for read and one for write requests...
void set_priority_op(request_queue::priority_op op)
Changes requests priorities.
Definition: disk_queues.h:116
long long int int64
Definition: types.h:38
Encapsulates disk queues.
Definition: disk_queues.h:37
stxxl::int64 DISKID
Definition: disk_queues.h:41
Type * get() const
return the enclosed pointer.
Definition: counting_ptr.h:122
void add_request(request_ptr &req, DISKID disk)
Definition: disk_queues.h:52
std::map< DISKID, request_queue * > request_queue_map
Definition: disk_queues.h:42
static instance_pointer get_instance()
Definition: singleton.h:38
request_queue_map queues
Definition: disk_queues.h:45
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
request_queue * get_queue(DISKID disk)
Definition: disk_queues.h:96
virtual void add_request(request_ptr &req)=0
Queue for linuxaio_file(s)
file * get_file() const
Definition: request.h:63
bool cancel_request(request_ptr &req, DISKID disk)
Cancel a request. The specified request is canceled unless already being processed. However, cancelation cannot be guaranteed. Cancelled requests must still be waited for in order to ensure correct operation.
Definition: disk_queues.h:85
#define STXXL_END_NAMESPACE
Definition: namespace.h:17