]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | /* |
2 | * This file is open source software, licensed to you under the terms | |
3 | * of the Apache License, Version 2.0 (the "License"). See the NOTICE file | |
4 | * distributed with this work for additional information regarding copyright | |
5 | * ownership. You may not use this file except in compliance with the License. | |
6 | * | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, | |
12 | * software distributed under the License is distributed on an | |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | * KIND, either express or implied. See the License for the | |
15 | * specific language governing permissions and limitations | |
16 | * under the License. | |
17 | */ | |
18 | /* | |
19 | * Copyright (C) 2019 ScyllaDB Ltd. | |
20 | */ | |
21 | ||
22 | #pragma once | |
23 | ||
24 | #include "syscall_work_queue.hh" | |
25 | ||
26 | namespace seastar { | |
27 | ||
28 | class reactor; | |
29 | ||
30 | class thread_pool { | |
31 | reactor* _reactor; | |
32 | uint64_t _aio_threaded_fallbacks = 0; | |
33 | #ifndef HAVE_OSV | |
34 | syscall_work_queue inter_thread_wq; | |
35 | posix_thread _worker_thread; | |
36 | std::atomic<bool> _stopped = { false }; | |
37 | std::atomic<bool> _main_thread_idle = { false }; | |
38 | public: | |
39 | explicit thread_pool(reactor* r, sstring thread_name); | |
40 | ~thread_pool(); | |
41 | template <typename T, typename Func> | |
f67539c2 | 42 | future<T> submit(Func func) noexcept { |
9f95a23c TL |
43 | ++_aio_threaded_fallbacks; |
44 | return inter_thread_wq.submit<T>(std::move(func)); | |
45 | } | |
46 | uint64_t operation_count() const { return _aio_threaded_fallbacks; } | |
47 | ||
48 | unsigned complete() { return inter_thread_wq.complete(); } | |
49 | // Before we enter interrupt mode, we must make sure that the syscall thread will properly | |
50 | // generate signals to wake us up. This means we need to make sure that all modifications to | |
51 | // the pending and completed fields in the inter_thread_wq are visible to all threads. | |
52 | // | |
53 | // Simple release-acquire won't do because we also need to serialize all writes that happens | |
54 | // before the syscall thread loads this value, so we'll need full seq_cst. | |
55 | void enter_interrupt_mode() { _main_thread_idle.store(true, std::memory_order_seq_cst); } | |
56 | // When we exit interrupt mode, however, we can safely used relaxed order. If any reordering | |
57 | // takes place, we'll get an extra signal and complete will be called one extra time, which is | |
58 | // harmless. | |
59 | void exit_interrupt_mode() { _main_thread_idle.store(false, std::memory_order_relaxed); } | |
60 | ||
61 | #else | |
62 | public: | |
63 | template <typename T, typename Func> | |
64 | future<T> submit(Func func) { std::cerr << "thread_pool not yet implemented on osv\n"; abort(); } | |
65 | #endif | |
66 | private: | |
67 | void work(sstring thread_name); | |
68 | }; | |
69 | ||
70 | ||
71 | } |