static const uint64_t kMagic8Bytes = 0xfaceb00cfaceb00c;
pid_t localThreadId() {
- static thread_local pid_t threadId = syscall(SYS_gettid);
+ // __thread doesn't allow non-const initialization.
+ // OSX doesn't support thread_local.
+ static FOLLY_TLS pid_t threadId = 0;
+ if (UNLIKELY(threadId == 0)) {
+ threadId = syscall(SYS_gettid);
+ }
return threadId;
}
#include <cassert>
#include <folly/CPortability.h>
+#include <folly/Memory.h>
+#include <folly/Optional.h>
+#include <folly/Portability.h>
+#include <folly/ScopeGuard.h>
+#include <folly/ThreadLocal.h>
#include <folly/experimental/fibers/Baton.h>
#include <folly/experimental/fibers/Fiber.h>
#include <folly/experimental/fibers/LoopController.h>
#include <folly/experimental/fibers/Promise.h>
#include <folly/futures/Try.h>
-#include <folly/Memory.h>
-#include <folly/Optional.h>
-#include <folly/Portability.h>
-#include <folly/ScopeGuard.h>
namespace folly { namespace fibers {
template <typename T>
T& FiberManager::localThread() {
- static thread_local T t;
- return t;
+ static ThreadLocal<T> t;
+ return *t;
}
inline void FiberManager::initLocalData(Fiber& fiber) {
namespace folly {
-void QueuedImmediateExecutor::add(Func callback) {
- thread_local std::queue<Func> q;
+void QueuedImmediateExecutor::addStatic(Func callback) {
+ static folly::ThreadLocal<std::queue<Func>> q_;
- if (q.empty()) {
- q.push(std::move(callback));
- while (!q.empty()) {
- q.front()();
- q.pop();
+ if (q_->empty()) {
+ q_->push(std::move(callback));
+ while (!q_->empty()) {
+ q_->front()();
+ q_->pop();
}
} else {
- q.push(callback);
+ q_->push(callback);
}
}
*/
class QueuedImmediateExecutor : public Executor {
public:
- void add(Func) override;
+ /// There's really only one queue per thread, no matter how many
+ /// QueuedImmediateExecutor objects you may have.
+ static void addStatic(Func);
+
+ void add(Func func) override {
+ addStatic(std::move(func));
+ }
};
} // folly