To avoid undefined references in examples:
May 11 01:58:40 ld.lld-17: error: undefined symbol: DB::Context::hasTraceCollector() const
May 11 01:58:40 >>> referenced by ThreadStatus.cpp:132 (/build/src/Common/ThreadStatus.cpp:132)
May 11 01:58:40 >>> ThreadStatus.cpp.o:(DB::ThreadStatus::initGlobalProfiler(unsigned long, unsigned long)) in archive src/libclickhouse_common_iod.a
May 11 01:58:40 clang++-17: error: linker command failed with exit code 1 (use -v to see invocation)
Move it firstly into ThreadStatusExt and then do not try to use it from
the ThreadPool.
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
There are some places that make it possible to trigger use-after-free
from threads because some global variable had been destroyed before, for
example some logger.
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
There are lots of thread pools and simple local-vs-global is not enough
already, it is good to know which one in particular uses threads.
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
ThreadPool::setMaxThreads() can now start new threads (if there are scheduled jobs in the queue for them);
ThreadPool::setMaxFreeThreads() can now finish free threads.
- lots of static_cast
- add safe_cast
- types adjustments
- config
- IStorage::read/watch
- ...
- some TODO's (to convert types in future)
P.S. That was quite a journey...
v2: fixes after rebase
v3: fix conflicts after #42308 merged
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
As found by @KochetovNicolai before this patch, lambda in
ThreadFromGlobalPool() ctor assigns value only to a copy of the
thread_id value, and so check in joinable() had been working
incorrectly, fix this by changing the value not the shared_ptr itself.
Also it is not safe to assign thread_id w/o atomics, since this can be
racy, so wrap id with std::atomic<>
Fixes: #28431
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
joinable() should be used only outside, since internally it is enough to
know `state` to know that something is wrong.
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>