-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathglobals.cpp
More file actions
32 lines (25 loc) · 1017 Bytes
/
globals.cpp
File metadata and controls
32 lines (25 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "globals.h"
#include "threadglobals.h"
Globals globals;
/**
* Normally there's 'ThreadGlobals::getThreadData()', but there are places where we can be called
* from non-worker-threads, like plugin custom threads. This gives us the worker's thread one, or
* a deterministic other one. Determinism is important to ensure order, when publishing for instance.
*
* Note that there is overhead here, so use only when required.
*/
CheckedSharedPtr<ThreadData> Globals::GlobalsData::getDeterministicThreadData()
{
CheckedSharedPtr<ThreadData> result = ThreadGlobals::getThreadData();
if (result)
return result;
static thread_local std::weak_ptr<ThreadData> thread_data_copy_weak;
std::shared_ptr<ThreadData> result2 = thread_data_copy_weak.lock();
if (result2)
return result2;
static std::atomic<size_t> index {0};
auto locked = threadDatas.lock();
result2 = locked->at(index++ % locked->size());
thread_data_copy_weak = result2;
return result2;
}