2 * Copyright 2017-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <folly/detail/MemoryIdler.h>
21 #include <folly/fibers/FiberManagerInternal.h>
22 #include <folly/portability/Asm.h>
31 void Baton::wait(TimeoutHandler& timeoutHandler) {
32 auto timeoutFunc = [this, &timeoutHandler] {
36 timeoutHandler.timeoutPtr_ = 0;
38 timeoutHandler.timeoutFunc_ = std::ref(timeoutFunc);
39 timeoutHandler.fiberManager_ = FiberManager::getFiberManagerUnsafe();
41 timeoutHandler.cancelTimeout();
44 void Baton::waitThread() {
45 if (spinWaitForEarlyPost()) {
46 assert(waitingFiber_.load(std::memory_order_acquire) == POSTED);
50 auto fiber = waitingFiber_.load();
54 waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) {
56 folly::detail::MemoryIdler::futexWait(
57 futex_.futex, uint32_t(THREAD_WAITING));
58 fiber = waitingFiber_.load(std::memory_order_acquire);
59 } while (fiber == THREAD_WAITING);
62 if (LIKELY(fiber == POSTED)) {
67 if (fiber == TIMEOUT) {
68 throw std::logic_error("Thread baton can't have timeout status");
70 if (fiber == THREAD_WAITING) {
71 throw std::logic_error("Other thread is already waiting on this baton");
73 throw std::logic_error("Other fiber is already waiting on this baton");
76 bool Baton::spinWaitForEarlyPost() {
79 "isn't this assert clearer than an uninitialized variable warning?");
80 for (int i = 0; i < PreBlockAttempts; ++i) {
85 // The pause instruction is the polite way to spin, but it doesn't
86 // actually affect correctness to omit it if we don't have it.
87 // Pausing donates the full capabilities of the current core to
88 // its other hyperthreads for a dozen cycles or so
95 bool Baton::timedWaitThread(TimeoutController::Duration timeout) {
96 if (spinWaitForEarlyPost()) {
97 assert(waitingFiber_.load(std::memory_order_acquire) == POSTED);
101 auto fiber = waitingFiber_.load();
104 fiber == NO_WAITER &&
105 waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) {
106 auto deadline = TimeoutController::Clock::now() + timeout;
109 futex_.futex.futexWaitUntil(uint32_t(THREAD_WAITING), deadline);
110 if (wait_rv == folly::detail::FutexResult::TIMEDOUT) {
113 fiber = waitingFiber_.load(std::memory_order_relaxed);
114 } while (fiber == THREAD_WAITING);
117 if (LIKELY(fiber == POSTED)) {
122 if (fiber == TIMEOUT) {
123 throw std::logic_error("Thread baton can't have timeout status");
125 if (fiber == THREAD_WAITING) {
126 throw std::logic_error("Other thread is already waiting on this baton");
128 throw std::logic_error("Other fiber is already waiting on this baton");
135 void Baton::postHelper(intptr_t new_value) {
136 auto fiber = waitingFiber_.load();
139 if (fiber == THREAD_WAITING) {
140 assert(new_value == POSTED);
145 if (fiber == POSTED || fiber == TIMEOUT) {
148 } while (!waitingFiber_.compare_exchange_weak(fiber, new_value));
150 if (fiber != NO_WAITER) {
151 reinterpret_cast<Fiber*>(fiber)->resume();
155 bool Baton::try_wait() {
156 auto state = waitingFiber_.load();
157 return state == POSTED;
160 void Baton::postThread() {
161 auto expected = THREAD_WAITING;
163 if (!waitingFiber_.compare_exchange_strong(expected, POSTED)) {
167 futex_.futex.futexWake(1);
170 void Baton::reset() {
171 waitingFiber_.store(NO_WAITER, std::memory_order_relaxed);
175 void Baton::TimeoutHandler::scheduleTimeout(
176 TimeoutController::Duration timeout) {
177 assert(fiberManager_ != nullptr);
178 assert(timeoutFunc_ != nullptr);
179 assert(timeoutPtr_ == 0);
181 if (timeout.count() > 0) {
183 fiberManager_->timeoutManager_->registerTimeout(timeoutFunc_, timeout);
187 void Baton::TimeoutHandler::cancelTimeout() {
189 fiberManager_->timeoutManager_->cancel(timeoutPtr_);
192 } // namespace fibers