void ThreadLocalTester::run() { // Wait until finished is true while (!isStopping()) { Command command = this->command; switch (Command(this->command)) { case SETDATA: data.set(setData); // Reset to no command this->command = NONE; break; case GETDATA: getData = *data.get(); // Reset to no command this->command = NONE; break; case NONE: // Sleep for 30 ms std::this_thread::sleep_for(std::chrono::milliseconds(30)); break; } } }
virtual RequestHandler *createRequestHandler() { s_rpc_request_handler->setServerInfo(m_serverInfo); if (s_rpc_request_handler->needReset() || s_rpc_request_handler->incRequest() > m_serverInfo->getMaxRequest()) { s_rpc_request_handler.reset(); s_rpc_request_handler->setServerInfo(m_serverInfo); s_rpc_request_handler->incRequest(); } return s_rpc_request_handler.get(); }
void ThreadLocalTest::testAccessors() { ThreadLocal<TLTestStruct> ts; ts->i = 100; ts->s = "foo"; assert ((*ts).i == 100); assert ((*ts).s == "foo"); assert (ts.get().i == 100); assert (ts.get().s == "foo"); }
// Tests that the threadlocal data is unique between threads TEST(TestThreadLocal, MainThread) { // Initialize ThreadLocal storage ThreadLocal<int> data; // Set value to 123 data.set(123); // Confirm that it equals ASSERT_EQ(*data.get(), 123); }
inline void operator()() { staticThinggy = new Thinggy; staticThreadLocal.set(staticThinggy); waiting = true; gate.Set(); waiter.Wait(); waiting = false; threadLocalHadValue = staticThreadLocal.get() != NULL; gate.Set(); }
TEST(TestThreadLocal, Simple) { GlobalThreadLocal runnable; thread t(runnable); gate.Wait(); EXPECT_TRUE(runnable.waiting); EXPECT_TRUE(staticThinggy != NULL); EXPECT_TRUE(staticThreadLocal.get() == NULL); waiter.Set(); gate.Wait(); EXPECT_TRUE(runnable.threadLocalHadValue); EXPECT_TRUE(!destructorCalled); delete staticThinggy; EXPECT_TRUE(destructorCalled); cleanup(); }
BackgroundHangThread* BackgroundHangThread::FindThread() { if (sTlsKey.initialized()) { // Use TLS if available return sTlsKey.get(); } // If TLS is unavailable, we can search through the thread list RefPtr<BackgroundHangManager> manager(BackgroundHangManager::sInstance); MOZ_ASSERT(manager, "Creating BackgroundHangMonitor after shutdown"); PRThread* threadID = PR_GetCurrentThread(); // Lock thread list for traversal MonitorAutoLock autoLock(manager->mLock); for (BackgroundHangThread* thread = manager->mHangThreads.getFirst(); thread; thread = thread->getNext()) { if (thread->mThreadID == threadID) { return thread; } } // Current thread is not initialized return nullptr; }
void Random::secureRandom(void* data, size_t size) { static ThreadLocal<BufferedRandomDevice> bufferedRandomDevice; bufferedRandomDevice->get(data, size); }
int get() { return value.get(); }
void increment() { value.set(value.get() + 1); }
NestedDiagnosticContext& NestedDiagnosticContext::current() { static ThreadLocal<NestedDiagnosticContext> ndc; return ndc.get(); }
TEST(ThreadLocalStorage, NotSetReturnsNull) { static ThreadLocal<int> number; EXPECT_EQ(nullptr, number.get()); }
TEST(ThreadLocal, Unthreaded) { EXPECT_EQ(sVar.get(), 0); sVar = 10; EXPECT_EQ(sVar.get(), 10); }