Welcome to mirror list, hosted at ThFree Co, Russian Federation.

async_file_handle.ipp « posix « impl « detail « v2.0 « afio « boost « include - github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 112524d7cc1c0c5cfeeca77e0531a28721ff9c11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* handle.hpp
A handle to something
(C) 2015 Niall Douglas http://www.nedprod.com/
File Created: Dec 2015


Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include "../../../handle.hpp"

#include <fcntl.h>
#include <unistd.h>
#if BOOST_AFIO_USE_POSIX_AIO
#include <aio.h>
#endif

BOOST_AFIO_V2_NAMESPACE_BEGIN

result<async_file_handle> async_file_handle::clone(io_service &service) const noexcept
{
  BOOST_OUTCOME_TRY(v, clone());
  async_file_handle ret(std::move(v));
  ret._service = &service;
  return std::move(ret);
}

template <class CompletionRoutine, class BuffersType, class IORoutine>
result<async_file_handle::io_state_ptr<CompletionRoutine, BuffersType>> async_file_handle::_begin_io(async_file_handle::operation_t operation, async_file_handle::io_request<BuffersType> reqs, CompletionRoutine &&completion, IORoutine &&ioroutine) noexcept
{
  // Need to keep a set of aiocbs matching the scatter-gather buffers
  struct state_type : public _io_state_type<CompletionRoutine, BuffersType>
  {
#if BOOST_AFIO_USE_POSIX_AIO
    struct aiocb aiocbs[1];
#else
#error todo
#endif
    state_type(handle *_parent, operation_t _operation, CompletionRoutine &&f, size_t _items)
        : _io_state_type<CompletionRoutine, BuffersType>(_parent, _operation, std::forward<CompletionRoutine>(f), _items)
    {
    }
    virtual void operator()(long errcode, long bytes_transferred, void *internal_state) noexcept override final
    {
#if BOOST_AFIO_USE_POSIX_AIO
      struct aiocb **_paiocb = (struct aiocb **) internal_state;
      struct aiocb *aiocb = *_paiocb;
      assert(aiocb >= aiocbs && aiocb < aiocbs + this->items);
      *_paiocb = nullptr;
#else
#error todo
#endif
      if(this->result)
      {
        if(errcode)
          this->result = make_errored_result<BuffersType>((int) errcode);
        else
        {
// Figure out which i/o I am and update the buffer in question
#if BOOST_AFIO_USE_POSIX_AIO
          size_t idx = aiocb - aiocbs;
#else
#error todo
#endif
          if(idx >= this->items)
          {
            BOOST_AFIO_LOG_FATAL(0, "file_handle::io_state::operator() called with invalid index");
            std::terminate();
          }
          this->result.value()[idx].second = bytes_transferred;
        }
      }
      this->parent->service()->_work_done();
      // Are we done?
      if(!--this->items_to_go)
        this->completion(this);
    }
    virtual ~state_type() override final
    {
      // Do we need to cancel pending i/o?
      if(this->items_to_go)
      {
        for(size_t n = 0; n < this->items; n++)
        {
#if BOOST_AFIO_USE_POSIX_AIO
          int ret = aio_cancel(this->parent->native_handle().fd, aiocbs + n);
#if 0
          if(ret<0 || ret==AIO_NOTCANCELED)
          {
            std::cout << "Failed to cancel " << (aiocbs+n) << std::endl;
          }
          else if(ret==AIO_CANCELED)
          {
            std::cout << "Cancelled " << (aiocbs+n) << std::endl;
          }
          else if(ret==AIO_ALLDONE)
          {
            std::cout << "Already done " << (aiocbs+n) << std::endl;
          }
#endif
#else
#error todo
#endif
        }
        // Pump the i/o service until all pending i/o is completed
        while(this->items_to_go)
        {
          auto res = this->parent->service()->run();
#ifndef NDEBUG
          if(res.has_error())
          {
            BOOST_AFIO_LOG_FATAL(0, "file_handle: io_service failed");
            std::terminate();
          }
          if(!res.get())
          {
            BOOST_AFIO_LOG_FATAL(0, "file_handle: io_service returns no work when i/o has not completed");
            std::terminate();
          }
#endif
        }
      }
    }
  } * state;
  extent_type offset = reqs.offset;
  size_t statelen = sizeof(state_type) + (reqs.buffers.size() - 1) * sizeof(struct aiocb), items(reqs.buffers.size());
  using return_type = io_state_ptr<CompletionRoutine, BuffersType>;
#if BOOST_AFIO_USE_POSIX_AIO && defined(AIO_LISTIO_MAX)
  if(items > AIO_LISTIO_MAX)
    return make_errored_result<return_type>(stl11::errc::invalid_argument);
#endif
  void *mem = ::calloc(1, statelen);
  if(!mem)
    return make_errored_result<return_type>(stl11::errc::not_enough_memory);
  return_type _state((_io_state_type<CompletionRoutine, BuffersType> *) mem);
  new((state = (state_type *) mem)) state_type(this, operation, std::forward<CompletionRoutine>(completion), items);
  // Noexcept move the buffers from req into result
  BuffersType &out = state->result.value();
  out = std::move(reqs.buffers);
  for(size_t n = 0; n < items; n++)
  {
#if BOOST_AFIO_USE_POSIX_AIO
    struct aiocb *aiocb = state->aiocbs + n;
    aiocb->aio_fildes = _v.fd;
    aiocb->aio_offset = offset;
    aiocb->aio_buf = (void *) out[n].first;
    aiocb->aio_nbytes = out[n].second;
    aiocb->aio_sigevent.sigev_notify = SIGEV_NONE;
    aiocb->aio_sigevent.sigev_value.sival_ptr = (void *) state;
    aiocb->aio_lio_opcode = (operation == operation_t::write) ? LIO_WRITE : LIO_READ;
#else
#error todo
#endif
    offset += out[n].second;
    ++state->items_to_go;
  }
  int ret = 0;
#if BOOST_AFIO_USE_POSIX_AIO
  if(service()->using_kqueues())
  {
#if BOOST_AFIO_COMPILE_KQUEUES
    // Only issue one kqueue event when entire scatter-gather has completed
    struct _sigev = {0};
#error todo
#endif
  }
  else
  {
    // Add these i/o's to the quick aio_suspend list
    service()->_aiocbsv.resize(service()->_aiocbsv.size() + items);
    struct aiocb **thislist = service()->_aiocbsv.data() + service()->_aiocbsv.size() - items;
    for(size_t n = 0; n < items; n++)
    {
      struct aiocb *aiocb = state->aiocbs + n;
      thislist[n] = aiocb;
    }
    ret = lio_listio(LIO_NOWAIT, thislist, items, nullptr);
  }
#else
#error todo
#endif
  if(ret < 0)
  {
    service()->_aiocbsv.resize(service()->_aiocbsv.size() - items);
    state->items_to_go = 0;
    state->result = make_errored_result<BuffersType>(errno);
    state->completion(state);
    return make_result<return_type>(std::move(_state));
  }
  service()->_work_enqueued(items);
  return make_result<return_type>(std::move(_state));
}

template <class CompletionRoutine> result<async_file_handle::io_state_ptr<CompletionRoutine, async_file_handle::buffers_type>> async_file_handle::async_read(async_file_handle::io_request<async_file_handle::buffers_type> reqs, CompletionRoutine &&completion) noexcept
{
  return _begin_io(operation_t::read, std::move(reqs), [completion = std::forward<CompletionRoutine>(completion)](auto *state) { completion(state->parent, state->result); }, nullptr);
}

template <class CompletionRoutine> result<async_file_handle::io_state_ptr<CompletionRoutine, async_file_handle::const_buffers_type>> async_file_handle::async_write(async_file_handle::io_request<async_file_handle::const_buffers_type> reqs, CompletionRoutine &&completion) noexcept
{
  return _begin_io(operation_t::write, std::move(reqs), [completion = std::forward<CompletionRoutine>(completion)](auto *state) { completion(state->parent, state->result); }, nullptr);
}

async_file_handle::io_result<async_file_handle::buffers_type> async_file_handle::read(async_file_handle::io_request<async_file_handle::buffers_type> reqs, deadline d) noexcept
{
  io_result<buffers_type> ret;
  auto _io_state(_begin_io(operation_t::read, std::move(reqs), [&ret](auto *state) { ret = std::move(state->result); }, nullptr));
  BOOST_OUTCOME_FILTER_ERROR(io_state, _io_state);

  // While i/o is not done pump i/o completion
  while(!ret.is_ready())
  {
    auto t(_service->run_until(d));
    // If i/o service pump failed or timed out, cancel outstanding i/o and return
    if(!t)
      return make_errored_result<buffers_type>(t.get_error());
#ifndef NDEBUG
    if(!ret.is_ready() && t && !t.get())
    {
      BOOST_AFIO_LOG_FATAL(_v.fd, "async_file_handle: io_service returns no work when i/o has not completed");
      std::terminate();
    }
#endif
  }
  return ret;
}

async_file_handle::io_result<async_file_handle::const_buffers_type> async_file_handle::write(async_file_handle::io_request<async_file_handle::const_buffers_type> reqs, deadline d) noexcept
{
  io_result<const_buffers_type> ret;
  auto _io_state(_begin_io(operation_t::write, std::move(reqs), [&ret](auto *state) { ret = std::move(state->result); }, nullptr));
  BOOST_OUTCOME_FILTER_ERROR(io_state, _io_state);

  // While i/o is not done pump i/o completion
  while(!ret.is_ready())
  {
    auto t(_service->run_until(d));
    // If i/o service pump failed or timed out, cancel outstanding i/o and return
    if(!t)
      return make_errored_result<const_buffers_type>(t.get_error());
#ifndef NDEBUG
    if(!ret.is_ready() && t && !t.get())
    {
      BOOST_AFIO_LOG_FATAL(_v.fd, "async_file_handle: io_service returns no work when i/o has not completed");
      std::terminate();
    }
#endif
  }
  return ret;
}

BOOST_AFIO_V2_NAMESPACE_END