STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
linuxaio_queue.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/io/linuxaio_queue.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2011 Johannes Singler <[email protected]>
7  * Copyright (C) 2014 Timo Bingmann <[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 #ifndef STXXL_IO_LINUXAIO_QUEUE_HEADER
15 #define STXXL_IO_LINUXAIO_QUEUE_HEADER
16 
18 
19 #if STXXL_HAVE_LINUXAIO_FILE
20 
21 #include <linux/aio_abi.h>
22 #include <list>
23 
26 
28 
29 //! \addtogroup reqlayer
30 //! \{
31 
32 //! Queue for linuxaio_file(s)
33 //!
34 //! Only one queue exists in a program, i.e. it is a singleton.
36 {
37  friend class linuxaio_request;
38 
40 
41 private:
42  //! OS context
43  aio_context_t context;
44 
45  //! storing linuxaio_request* would drop ownership
46  typedef std::list<request_ptr> queue_type;
47 
48  // "waiting" request have submitted to this queue, but not yet to the OS,
49  // those are "posted"
50  mutex waiting_mtx, posted_mtx;
51  queue_type waiting_requests, posted_requests;
52 
53  //! max number of OS requests
55  //! number of requests in waitings_requests
56  semaphore num_waiting_requests, num_free_events, num_posted_requests;
57 
58  // two threads, one for posting, one for waiting
59  thread_type post_thread, wait_thread;
61 
62  // Why do we need two threads, one for posting, and one for waiting? Is
63  // one not enough?
64  // 1. User call cannot io_submit directly, since this tends to take
65  // considerable time sometimes
66  // 2. A single thread cannot wait for the user program to post requests
67  // and the OS to produce I/O completion events at the same time
68  // (IOCB_CMD_NOOP does not seem to help here either)
69 
70  static const priority_op _priority_op = WRITE;
71 
72  static void * post_async(void* arg); // thread start callback
73  static void * wait_async(void* arg); // thread start callback
74  void post_requests();
75  void handle_events(io_event* events, long num_events, bool canceled);
76  void wait_requests();
77  void suspend();
78 
79  // needed by linuxaio_request
80  aio_context_t get_io_context() { return context; }
81 
82 public:
83  //! Construct queue. Requests max number of requests simultaneously
84  //! submitted to disk, 0 means as many as possible
85  linuxaio_queue(int desired_queue_length = 0);
86 
87  void add_request(request_ptr& req);
88  bool cancel_request(request_ptr& req);
89  void complete_request(request_ptr& req);
90  ~linuxaio_queue();
91 };
92 
93 //! \}
94 
96 
97 #endif // #if STXXL_HAVE_LINUXAIO_FILE
98 
99 #endif // !STXXL_IO_LINUXAIO_QUEUE_HEADER
100 // vim: et:ts=4:sw=4
std::list< request_ptr > queue_type
storing linuxaio_request* would drop ownership
High-performance smart pointer used as a wrapping reference counting pointer.
Definition: counting_ptr.h:50
Implementation of request queue worker threads. Worker threads can be started by start_thread and sto...
int max_events
max number of OS requests
aio_context_t context
OS context.
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
queue_type waiting_requests
state< thread_state > wait_thread_state
linuxaio_queue self_type
aio_context_t get_io_context()
semaphore num_waiting_requests
number of requests in waitings_requests
Request for an linuxaio_file.
Queue for linuxaio_file(s)
#define STXXL_END_NAMESPACE
Definition: namespace.h:17