From 2b96f89c2a374d72c0a8bc28a7b06ad4db7eae6e Mon Sep 17 00:00:00 2001 From: Nat Goodspeed <nat@lindenlab.com> Date: Tue, 23 Nov 2021 20:39:32 -0500 Subject: [PATCH] SL-16400: Add ThreadPool::start() method, and call it. It's sometimes important to finish other initialization before launching the threads in the ThreadPool, so make that an explicit step. In particular, we were launching the LLImageGL texture thread before initializing the GL context, resulting in all gray textures. --- indra/llcommon/threadpool.cpp | 10 +++++++--- indra/llcommon/threadpool.h | 9 +++++++++ indra/llrender/llimagegl.cpp | 5 +---- indra/llwindow/llwindowwin32.cpp | 1 + 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/indra/llcommon/threadpool.cpp b/indra/llcommon/threadpool.cpp index 06e0dc5bfc2..ba914035e20 100644 --- a/indra/llcommon/threadpool.cpp +++ b/indra/llcommon/threadpool.cpp @@ -23,11 +23,15 @@ LL::ThreadPool::ThreadPool(const std::string& name, size_t threads, size_t capacity): mQueue(name, capacity), - mName("ThreadPool:" + name) + mName("ThreadPool:" + name), + mThreadCount(threads) +{} + +void LL::ThreadPool::start() { - for (size_t i = 0; i < threads; ++i) + for (size_t i = 0; i < mThreadCount; ++i) { - std::string tname{ STRINGIZE(mName << ':' << (i+1) << '/' << threads) }; + std::string tname{ stringize(mName, ':', (i+1), '/', mThreadCount) }; mThreads.emplace_back(tname, [this, tname]() { LL_PROFILER_SET_THREAD_NAME(tname.c_str()); diff --git a/indra/llcommon/threadpool.h b/indra/llcommon/threadpool.h index 1ca24aec58e..b79c9b90903 100644 --- a/indra/llcommon/threadpool.h +++ b/indra/llcommon/threadpool.h @@ -32,6 +32,14 @@ namespace LL ThreadPool(const std::string& name, size_t threads=1, size_t capacity=1024); virtual ~ThreadPool(); + /** + * Launch the ThreadPool. Until this call, a constructed ThreadPool + * launches no threads. That permits coders to derive from ThreadPool, + * or store it as a member of some other class, but refrain from + * launching it until all other construction is complete. + */ + void start(); + /** * ThreadPool listens for application shutdown messages on the "LLApp" * LLEventPump. Call close() to shut down this ThreadPool early. @@ -54,6 +62,7 @@ namespace LL WorkQueue mQueue; std::string mName; + size_t mThreadCount; std::vector<std::pair<std::string, std::thread>> mThreads; }; diff --git a/indra/llrender/llimagegl.cpp b/indra/llrender/llimagegl.cpp index eda61d3c74e..894eb8c7730 100644 --- a/indra/llrender/llimagegl.cpp +++ b/indra/llrender/llimagegl.cpp @@ -2261,6 +2261,7 @@ LLImageGLThread::LLImageGLThread(LLWindow* window) mFinished = false; mContext = mWindow->createSharedContext(); + ThreadPool::start(); } void LLImageGLThread::run() @@ -2268,10 +2269,6 @@ void LLImageGLThread::run() LL_PROFILE_ZONE_SCOPED; // We must perform setup on this thread before actually servicing our // WorkQueue, likewise cleanup afterwards. - while (mContext == nullptr) - { // HACK -- wait for mContext to be initialized since this thread will usually start before mContext is set - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } mWindow->makeContextCurrent(mContext); gGL.init(); ThreadPool::run(); diff --git a/indra/llwindow/llwindowwin32.cpp b/indra/llwindow/llwindowwin32.cpp index 51fd2286193..af7b8d91f06 100644 --- a/indra/llwindow/llwindowwin32.cpp +++ b/indra/llwindow/llwindowwin32.cpp @@ -4495,6 +4495,7 @@ std::vector<std::string> LLWindowWin32::getDynamicFallbackFontList() inline LLWindowWin32::LLWindowWin32Thread::LLWindowWin32Thread() : ThreadPool("Window Thread", 1, MAX_QUEUE_SIZE) { + ThreadPool::start(); } /** -- GitLab