Stxxl  1.3.2
request_operations.h
1 /***************************************************************************
2  * include/stxxl/bits/io/request_operations.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, 2009 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2009 Johannes Singler <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_IO__REQUEST_OPERATIONS_H_
16 #define STXXL_IO__REQUEST_OPERATIONS_H_
17 
18 #include <stxxl/bits/namespace.h>
19 #include <stxxl/bits/io/request_ptr.h>
20 #include <stxxl/bits/io/iostats.h>
21 #include <stxxl/bits/common/switch.h>
22 
23 
24 __STXXL_BEGIN_NAMESPACE
25 
28 
30 
31 
35 template <class request_iterator_>
36 void wait_all(request_iterator_ reqs_begin, request_iterator_ reqs_end)
37 {
38  for ( ; reqs_begin != reqs_end; ++reqs_begin)
39  (request_ptr(*reqs_begin))->wait();
40 }
41 
45 inline void wait_all(request_ptr req_array[], int count)
46 {
47  wait_all(req_array, req_array + count);
48 }
49 
58 template <class request_iterator_>
59 typename std::iterator_traits<request_iterator_>::difference_type cancel_all(request_iterator_ reqs_begin, request_iterator_ reqs_end)
60 {
61  typename std::iterator_traits<request_iterator_>::difference_type num_canceled = 0;
62  while (reqs_begin != reqs_end)
63  {
64  if ((request_ptr(*reqs_begin))->cancel())
65  ++num_canceled;
66  ++reqs_begin;
67  }
68  return num_canceled;
69 }
70 
76 template <class request_iterator_>
77 request_iterator_ poll_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
78 {
79  while (reqs_begin != reqs_end)
80  {
81  if ((request_ptr(*reqs_begin))->poll())
82  return reqs_begin;
83 
84  ++reqs_begin;
85  }
86  return reqs_end;
87 }
88 
89 
95 inline bool poll_any(request_ptr req_array[], int count, int & index)
96 {
97  request_ptr * res = poll_any(req_array, req_array + count);
98  index = res - req_array;
99  return res != (req_array + count);
100 }
101 
102 
107 template <class request_iterator_>
108 request_iterator_ wait_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
109 {
110  stats::scoped_wait_timer wait_timer(stats::WAIT_OP_ANY);
111 
112  onoff_switch sw;
113 
114  request_iterator_ cur = reqs_begin, result = reqs_end;
115 
116  for ( ; cur != reqs_end; cur++)
117  {
118  if ((request_ptr(*cur))->add_waiter(&sw))
119  {
120  // request is already done, no waiter was added to the request
121  result = cur;
122 
123  if (cur != reqs_begin)
124  {
125  while (--cur != reqs_begin)
126  (request_ptr(*cur))->delete_waiter(&sw);
127 
128  (request_ptr(*cur))->delete_waiter(&sw);
129  }
130 
131  (request_ptr(*result))->check_errors();
132 
133  return result;
134  }
135  }
136 
137  sw.wait_for_on();
138 
139  for (cur = reqs_begin; cur != reqs_end; cur++)
140  {
141  (request_ptr(*cur))->delete_waiter(&sw);
142  if (result == reqs_end && (request_ptr(*cur))->poll())
143  result = cur;
144  }
145 
146  return result;
147 }
148 
149 
154 inline int wait_any(request_ptr req_array[], int count)
155 {
156  return wait_any(req_array, req_array + count) - req_array;
157 }
158 
160 
161 __STXXL_END_NAMESPACE
162 
163 #endif // !STXXL_IO__REQUEST_OPERATIONS_H_
164 // vim: et:ts=4:sw=4
request_iterator_ poll_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Polls requests.
Definition: request_operations.h:77
Implemented as reference counting smart pointer.
Definition: request_ptr.h:34
std::iterator_traits< request_iterator_ >::difference_type cancel_all(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Cancel requests The specified requests are canceled unless already being processed. However, cancelation cannot be guaranteed. Cancelled requests must still be waited for in order to ensure correct operation.
Definition: request_operations.h:59
void wait_all(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Collection of functions to track statuses of a number of requests.
Definition: request_operations.h:36
request_iterator_ wait_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Suspends calling thread until any of requests is completed.
Definition: request_operations.h:108