2 * Copyright 2017 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 bool Baton::timed_wait(TimeoutController::Duration timeout) {
45 return timed_wait(timeout, []() {});
48 void Baton::waitThread() {
49 if (spinWaitForEarlyPost()) {
50 assert(waitingFiber_.load(std::memory_order_acquire) == POSTED);
54 auto fiber = waitingFiber_.load();
58 waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) {
60 folly::detail::MemoryIdler::futexWait(
61 futex_.futex, uint32_t(THREAD_WAITING));
62 fiber = waitingFiber_.load(std::memory_order_acquire);
63 } while (fiber == THREAD_WAITING);
66 if (LIKELY(fiber == POSTED)) {
71 if (fiber == TIMEOUT) {
72 throw std::logic_error("Thread baton can't have timeout status");
74 if (fiber == THREAD_WAITING) {
75 throw std::logic_error("Other thread is already waiting on this baton");
77 throw std::logic_error("Other fiber is already waiting on this baton");
80 bool Baton::spinWaitForEarlyPost() {
83 "isn't this assert clearer than an uninitialized variable warning?");
84 for (int i = 0; i < PreBlockAttempts; ++i) {
89 // The pause instruction is the polite way to spin, but it doesn't
90 // actually affect correctness to omit it if we don't have it.
91 // Pausing donates the full capabilities of the current core to
92 // its other hyperthreads for a dozen cycles or so
99 bool Baton::timedWaitThread(TimeoutController::Duration timeout) {
100 if (spinWaitForEarlyPost()) {
101 assert(waitingFiber_.load(std::memory_order_acquire) == POSTED);
105 auto fiber = waitingFiber_.load();
108 fiber == NO_WAITER &&
109 waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) {
110 auto deadline = TimeoutController::Clock::now() + timeout;
113 futex_.futex.futexWaitUntil(uint32_t(THREAD_WAITING), deadline);
114 if (wait_rv == folly::detail::FutexResult::TIMEDOUT) {
117 fiber = waitingFiber_.load(std::memory_order_relaxed);
118 } while (fiber == THREAD_WAITING);
121 if (LIKELY(fiber == POSTED)) {
126 if (fiber == TIMEOUT) {
127 throw std::logic_error("Thread baton can't have timeout status");
129 if (fiber == THREAD_WAITING) {
130 throw std::logic_error("Other thread is already waiting on this baton");
132 throw std::logic_error("Other fiber is already waiting on this baton");
139 void Baton::postHelper(intptr_t new_value) {
140 auto fiber = waitingFiber_.load();
143 if (fiber == THREAD_WAITING) {
144 assert(new_value == POSTED);
149 if (fiber == POSTED || fiber == TIMEOUT) {
152 } while (!waitingFiber_.compare_exchange_weak(fiber, new_value));
154 if (fiber != NO_WAITER) {
155 reinterpret_cast<Fiber*>(fiber)->resume();
159 bool Baton::try_wait() {
160 auto state = waitingFiber_.load();
161 return state == POSTED;
164 void Baton::postThread() {
165 auto expected = THREAD_WAITING;
167 if (!waitingFiber_.compare_exchange_strong(expected, POSTED)) {
171 futex_.futex.futexWake(1);
174 void Baton::reset() {
175 waitingFiber_.store(NO_WAITER, std::memory_order_relaxed);
179 void Baton::TimeoutHandler::scheduleTimeout(
180 TimeoutController::Duration timeout) {
181 assert(fiberManager_ != nullptr);
182 assert(timeoutFunc_ != nullptr);
183 assert(timeoutPtr_ == 0);
185 if (timeout.count() > 0) {
187 fiberManager_->timeoutManager_->registerTimeout(timeoutFunc_, timeout);
191 void Baton::TimeoutHandler::cancelTimeout() {
193 fiberManager_->timeoutManager_->cancel(timeoutPtr_);