STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
request_operations.h
Go to the documentation of this file.
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_HEADER
16 #define STXXL_IO_REQUEST_OPERATIONS_HEADER
17 
18 #include <stxxl/bits/namespace.h>
19 #include <stxxl/bits/io/request.h>
20 #include <stxxl/bits/io/iostats.h>
22 
23 
25 
26 //! \addtogroup iolayer
27 //! \{
28 
29 //! Collection of functions to track statuses of a number of requests.
30 
31 
32 //! Suspends calling thread until \b all given requests are completed.
33 //! \param reqs_begin begin of request sequence to wait for
34 //! \param reqs_end end of request sequence to wait for
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 
42 //! Suspends calling thread until \b all given requests are completed.
43 //! \param req_array array of request_ptr objects
44 //! \param count size of req_array
45 inline void wait_all(request_ptr req_array[], size_t count)
46 {
47  wait_all(req_array, req_array + count);
48 }
49 
50 //! Cancel requests.
51 //! The specified requests are canceled unless already being processed.
52 //! However, cancelation cannot be guaranteed.
53 //! Cancelled requests must still be waited for in order to ensure correct
54 //! operation.
55 //! \param reqs_begin begin of request sequence
56 //! \param reqs_end end of request sequence
57 //! \return number of request canceled
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 
71 //! Polls requests.
72 //! \param reqs_begin begin of request sequence to poll
73 //! \param reqs_end end of request sequence to poll
74 //! \return \c true if any of requests is completed, then index contains valid value, otherwise \c false
75 template <class request_iterator_>
76 request_iterator_ poll_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
77 {
78  while (reqs_begin != reqs_end)
79  {
80  if ((request_ptr(*reqs_begin))->poll())
81  return reqs_begin;
82 
83  ++reqs_begin;
84  }
85  return reqs_end;
86 }
87 
88 
89 //! Polls requests.
90 //! \param req_array array of request_ptr objects
91 //! \param count size of req_array
92 //! \param index contains index of the \b first completed request if any
93 //! \return \c true if any of requests is completed, then index contains valid value, otherwise \c false
94 inline bool poll_any(request_ptr req_array[], size_t count, size_t& index)
95 {
96  request_ptr* res = poll_any(req_array, req_array + count);
97  index = res - req_array;
98  return res != (req_array + count);
99 }
100 
101 
102 //! Suspends calling thread until \b any of requests is completed.
103 //! \param reqs_begin begin of request sequence to wait for
104 //! \param reqs_end end of request sequence to wait for
105 //! \return index in req_array pointing to the \b first completed request
106 template <class request_iterator_>
107 request_iterator_ wait_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
108 {
109  stats::scoped_wait_timer wait_timer(stats::WAIT_OP_ANY);
110 
111  onoff_switch sw;
112 
113  request_iterator_ cur = reqs_begin, result = reqs_end;
114 
115  for ( ; cur != reqs_end; cur++)
116  {
117  if ((request_ptr(*cur))->add_waiter(&sw))
118  {
119  // request is already done, no waiter was added to the request
120  result = cur;
121 
122  if (cur != reqs_begin)
123  {
124  while (--cur != reqs_begin)
125  (request_ptr(*cur))->delete_waiter(&sw);
126 
127  (request_ptr(*cur))->delete_waiter(&sw);
128  }
129 
130  (request_ptr(*result))->check_errors();
131 
132  return result;
133  }
134  }
135 
136  sw.wait_for_on();
137 
138  for (cur = reqs_begin; cur != reqs_end; cur++)
139  {
140  (request_ptr(*cur))->delete_waiter(&sw);
141  if (result == reqs_end && (request_ptr(*cur))->poll())
142  result = cur;
143  }
144 
145  return result;
146 }
147 
148 
149 //! Suspends calling thread until \b any of requests is completed.
150 //! \param req_array array of \c request_ptr objects
151 //! \param count size of req_array
152 //! \return index in req_array pointing to the \b first completed request
153 inline size_t wait_any(request_ptr req_array[], size_t count)
154 {
155  return wait_any(req_array, req_array + count) - req_array;
156 }
157 
158 //! \}
159 
161 
162 #endif // !STXXL_IO_REQUEST_OPERATIONS_HEADER
163 // vim: et:ts=4:sw=4
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.
counting_ptr< request > request_ptr
A reference counting pointer for request.
Definition: request.h:112
void wait_all(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Collection of functions to track statuses of a number of requests.
static void count(type_key *a, type_key *aEnd, int_type *bucket, int_type K, typename type_key::key_type offset, unsigned shift)
Definition: intksort.h:27
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
request_iterator_ wait_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Suspends calling thread until any of requests is completed.
request_iterator_ poll_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Polls requests.
void wait_for_on()
wait for switch to turn ON
Definition: onoff_switch.h:62
#define STXXL_END_NAMESPACE
Definition: namespace.h:17