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

coroutines.cpp « tests « test - github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e6a09b6ace1963cd9a1518b6398b782854f0a330 (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
/* Integration test kernel for map_handle create and close
(C) 2016-2017 Niall Douglas <http://www.nedproductions.biz/> (2 commits)
File Created: Aug 2016


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License in the accompanying file
Licence.txt or at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Distributed under the Boost Software License, Version 1.0.
    (See accompanying file Licence.txt or copy at
          http://www.boost.org/LICENSE_1_0.txt)
*/

#include "../test_kernel_decl.hpp"

#include <future>

static inline void TestAsyncFileHandleCoroutines()
{
#ifdef __cpp_coroutines
  //! [coroutines_example]
  namespace llfio = LLFIO_V2_NAMESPACE;

  // Create an i/o service for this thread
  llfio::io_service service;

  // Create an async file i/o handle attached to the i/o service for this thread
  llfio::async_file_handle h = llfio::async_file_handle::async_file(service, {}, "temp", llfio::file_handle::mode::write, llfio::file_handle::creation::if_needed, llfio::file_handle::caching::only_metadata, llfio::file_handle::flag::unlink_on_first_close).value();

  // Truncate to 1Mb
  h.truncate(1024 * 4096);

  // Launch 8 coroutines, each writing 4Kb of chars 0-8 to every 32Kb block
  auto coroutine = [&h](size_t no) -> std::future<void> {
    std::vector<llfio::byte, llfio::utils::page_allocator<llfio::byte>> buffer(4096);
    memset(buffer.data(), (int) ('0' + no), 4096);
    llfio::async_file_handle::const_buffer_type bt{buffer.data(), buffer.size()};
    for(size_t n = 0; n < 128; n++)
    {
      // This will initiate the i/o, and suspend the coroutine until completion.
      // The caller will thus resume execution with a valid unsignaled future.
      auto written = co_await h.co_write({bt, n * 32768 + no * 4096}).value();
      written.value();
    }
  };
  std::vector<std::future<void>> coroutines;
  for(size_t n = 0; n < 8; n++)
  {
    // Construct each coroutine, initiating the i/o, then suspending.
    coroutines.push_back(coroutine(n));
  }
  // Pump the i/o, multiplexing the coroutines, until no more work remains.
  while(service.run().value())
    ;
  // Make sure nothing went wrong by fetching the futures.
  for(auto &i : coroutines)
  {
    i.get();
  }
  //! [coroutines_example]

  // Check that the file has the right contents
  alignas(4096) llfio::byte buffer1[4096], buffer2[4096];
  llfio::async_file_handle::extent_type offset = 0;
  for(size_t n = 0; n < 128; n++)
  {
    for(size_t m = 0; m < 8; m++)
    {
      memset(buffer2, (int) ('0' + m), 4096);
      h.read(offset, {{buffer1, 4096}}).value();
      BOOST_CHECK(!memcmp(buffer1, buffer2, 4096));
      offset += 4096;
    }
  }
#endif
}

static inline void TestPostSelfToRunCoroutines()
{
#ifdef __cpp_coroutines
  namespace llfio = LLFIO_V2_NAMESPACE;
  llfio::io_service service;
  std::atomic<bool> ready(false);
  auto runthreadid = QUICKCPPLIB_NAMESPACE::utils::thread::this_thread_id();
  auto coroutinethread = [&]() -> void {
    auto coroutine = [&]() -> std::future<void> {
      auto thisthreadid = QUICKCPPLIB_NAMESPACE::utils::thread::this_thread_id();
      BOOST_CHECK(thisthreadid != runthreadid);
      ready = true;
      co_await llfio::io_service::awaitable_post_to_self(service);
      thisthreadid = QUICKCPPLIB_NAMESPACE::utils::thread::this_thread_id();
      BOOST_CHECK(thisthreadid == runthreadid);
      // std::cout << "Coroutine exiting" << std::endl;
      ready = false;
    };
    // std::cout << "Thread waiting on coroutine" << std::endl;
    coroutine().get();
    // std::cout << "Thread exiting" << std::endl;
  };
  auto asynch = std::async(std::launch::async, coroutinethread);
  while(!ready)
  {
    std::this_thread::yield();
  }
  while(ready)
  {
    service.run().value();
  }
  asynch.get();
#endif
}

KERNELTEST_TEST_KERNEL(integration, llfio, coroutines, async_file_handle, "Tests that llfio::async_file_handle works as expected with Coroutines", TestAsyncFileHandleCoroutines())
KERNELTEST_TEST_KERNEL(integration, llfio, coroutines, co_post_self_to_run, "Tests that llfio::io_service::co_post_self_to_run() works as expected with Coroutines", TestPostSelfToRunCoroutines())