2 * Copyright 2016 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/FiberManager.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(futex_.futex, THREAD_WAITING);
61 fiber = waitingFiber_.load(std::memory_order_acquire);
62 } while (fiber == THREAD_WAITING);
65 if (LIKELY(fiber == POSTED)) {
70 if (fiber == TIMEOUT) {
71 throw std::logic_error("Thread baton can't have timeout status");
73 if (fiber == THREAD_WAITING) {
74 throw std::logic_error("Other thread is already waiting on this baton");
76 throw std::logic_error("Other fiber is already waiting on this baton");
79 bool Baton::spinWaitForEarlyPost() {
82 "isn't this assert clearer than an uninitialized variable warning?");
83 for (int i = 0; i < PreBlockAttempts; ++i) {
88 // The pause instruction is the polite way to spin, but it doesn't
89 // actually affect correctness to omit it if we don't have it.
90 // Pausing donates the full capabilities of the current core to
91 // its other hyperthreads for a dozen cycles or so
98 bool Baton::timedWaitThread(TimeoutController::Duration timeout) {
99 if (spinWaitForEarlyPost()) {
100 assert(waitingFiber_.load(std::memory_order_acquire) == POSTED);
104 auto fiber = waitingFiber_.load();
107 fiber == NO_WAITER &&
108 waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) {
109 auto deadline = TimeoutController::Clock::now() + timeout;
112 futex_.futex.futexWaitUntil(THREAD_WAITING, deadline);
113 if (wait_rv == folly::detail::FutexResult::TIMEDOUT) {
116 fiber = waitingFiber_.load(std::memory_order_relaxed);
117 } while (fiber == THREAD_WAITING);
120 if (LIKELY(fiber == POSTED)) {
125 if (fiber == TIMEOUT) {
126 throw std::logic_error("Thread baton can't have timeout status");
128 if (fiber == THREAD_WAITING) {
129 throw std::logic_error("Other thread is already waiting on this baton");
131 throw std::logic_error("Other fiber is already waiting on this baton");
138 void Baton::postHelper(intptr_t new_value) {
139 auto fiber = waitingFiber_.load();
142 if (fiber == THREAD_WAITING) {
143 assert(new_value == POSTED);
148 if (fiber == POSTED || fiber == TIMEOUT) {
151 } while (!waitingFiber_.compare_exchange_weak(fiber, new_value));
153 if (fiber != NO_WAITER) {
154 reinterpret_cast<Fiber*>(fiber)->setData(0);
158 bool Baton::try_wait() {
159 auto state = waitingFiber_.load();
160 return state == POSTED;
163 void Baton::postThread() {
164 auto expected = THREAD_WAITING;
166 if (!waitingFiber_.compare_exchange_strong(expected, POSTED)) {
170 futex_.futex.futexWake(1);
173 void Baton::reset() {
174 waitingFiber_.store(NO_WAITER, std::memory_order_relaxed);
178 void Baton::TimeoutHandler::scheduleTimeout(
179 TimeoutController::Duration timeout) {
180 assert(fiberManager_ != nullptr);
181 assert(timeoutFunc_ != nullptr);
182 assert(timeoutPtr_ == 0);
184 if (timeout.count() > 0) {
186 fiberManager_->timeoutManager_->registerTimeout(timeoutFunc_, timeout);
190 void Baton::TimeoutHandler::cancelTimeout() {
192 fiberManager_->timeoutManager_->cancel(timeoutPtr_);