STXXL  1.4.1
 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 
24 
25 //! \addtogroup reqlayer
26 //! \{
27 
28 //! Collection of functions to track statuses of a number of requests.
29 
30 //! Suspends calling thread until \b all given requests are completed.
31 //! \param reqs_begin begin of request sequence to wait for
32 //! \param reqs_end end of request sequence to wait for
33 template <class RequestIterator>
34 void wait_all(RequestIterator reqs_begin, RequestIterator reqs_end)
35 {
36  for ( ; reqs_begin != reqs_end; ++reqs_begin)
37  (request_ptr(*reqs_begin))->wait();
38 }
39 
40 //! Suspends calling thread until \b all given requests are completed.
41 //! \param req_array array of request_ptr objects
42 //! \param count size of req_array
43 inline void wait_all(request_ptr req_array[], size_t count)
44 {
45  wait_all(req_array, req_array + count);
46 }
47 
48 //! Cancel requests.
49 //! The specified requests are canceled unless already being processed.
50 //! However, cancelation cannot be guaranteed.
51 //! Cancelled requests must still be waited for in order to ensure correct
52 //! operation.
53 //! \param reqs_begin begin of request sequence
54 //! \param reqs_end end of request sequence
55 //! \return number of request canceled
56 template <class RequestIterator>
57 typename std::iterator_traits<RequestIterator>::difference_type
58 cancel_all(RequestIterator reqs_begin, RequestIterator reqs_end)
59 {
60  typename std::iterator_traits<RequestIterator>::difference_type num_canceled = 0;
61  while (reqs_begin != reqs_end)
62  {
63  if ((request_ptr(*reqs_begin))->cancel())
64  ++num_canceled;
65  ++reqs_begin;
66  }
67  return num_canceled;
68 }
69 
70 //! Polls requests.
71 //! \param reqs_begin begin of request sequence to poll
72 //! \param reqs_end end of request sequence to poll
73 //! \return \c true if any of requests is completed, then index contains valid value, otherwise \c false
74 template <class RequestIterator>
75 RequestIterator poll_any(RequestIterator reqs_begin, RequestIterator reqs_end)
76 {
77  while (reqs_begin != reqs_end)
78  {
79  if ((request_ptr(*reqs_begin))->poll())
80  return reqs_begin;
81 
82  ++reqs_begin;
83  }
84  return reqs_end;
85 }
86 
87 //! Polls requests.
88 //! \param req_array array of request_ptr objects
89 //! \param count size of req_array
90 //! \param index contains index of the \b first completed request if any
91 //! \return \c true if any of requests is completed, then index contains valid value, otherwise \c false
92 inline bool poll_any(request_ptr req_array[], size_t count, size_t& index)
93 {
94  request_ptr* res = poll_any(req_array, req_array + count);
95  index = res - req_array;
96  return res != (req_array + count);
97 }
98 
99 //! Suspends calling thread until \b any of requests is completed.
100 //! \param reqs_begin begin of request sequence to wait for
101 //! \param reqs_end end of request sequence to wait for
102 //! \return index in req_array pointing to the \b first completed request
103 template <class RequestIterator>
104 RequestIterator wait_any(RequestIterator reqs_begin, RequestIterator reqs_end)
105 {
106  stats::scoped_wait_timer wait_timer(stats::WAIT_OP_ANY);
107 
108  onoff_switch sw;
109 
110  RequestIterator cur = reqs_begin, result = reqs_end;
111 
112  for ( ; cur != reqs_end; cur++)
113  {
114  if ((request_ptr(*cur))->add_waiter(&sw))
115  {
116  // request is already done, no waiter was added to the request
117  result = cur;
118 
119  if (cur != reqs_begin)
120  {
121  while (--cur != reqs_begin)
122  (request_ptr(*cur))->delete_waiter(&sw);
123 
124  (request_ptr(*cur))->delete_waiter(&sw);
125  }
126 
127  (request_ptr(*result))->check_errors();
128 
129  return result;
130  }
131  }
132 
133  sw.wait_for_on();
134 
135  for (cur = reqs_begin; cur != reqs_end; cur++)
136  {
137  (request_ptr(*cur))->delete_waiter(&sw);
138  if (result == reqs_end && (request_ptr(*cur))->poll())
139  result = cur;
140  }
141 
142  return result;
143 }
144 
145 //! Suspends calling thread until \b any of requests is completed.
146 //! \param req_array array of \c request_ptr objects
147 //! \param count size of req_array
148 //! \return index in req_array pointing to the \b first completed request
149 inline size_t wait_any(request_ptr req_array[], size_t count)
150 {
151  return wait_any(req_array, req_array + count) - req_array;
152 }
153 
154 //! \}
155 
157 
158 #endif // !STXXL_IO_REQUEST_OPERATIONS_HEADER
159 // vim: et:ts=4:sw=4
High-performance smart pointer used as a wrapping reference counting pointer.
Definition: counting_ptr.h:50
counting_ptr< request > request_ptr
A reference counting pointer for request.
Definition: request.h:113
RequestIterator poll_any(RequestIterator reqs_begin, RequestIterator reqs_end)
Polls requests.
std::iterator_traits< RequestIterator >::difference_type cancel_all(RequestIterator reqs_begin, RequestIterator 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.
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
static void count(TypeKey *a, TypeKey *aEnd, int_type *bucket, int_type K, typename TypeKey::key_type offset, unsigned shift)
Definition: intksort.h:26
void wait_all(RequestIterator reqs_begin, RequestIterator reqs_end)
Collection of functions to track statuses of a number of requests.
RequestIterator wait_any(RequestIterator reqs_begin, RequestIterator reqs_end)
Suspends calling thread until any of requests is completed.
void wait_for_on()
wait for switch to turn ON
Definition: onoff_switch.h:61
#define STXXL_END_NAMESPACE
Definition: namespace.h:17