-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.cpp
106 lines (91 loc) · 2.12 KB
/
context.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @file context.cpp
* @author Jens Munk Hansen <jmh@debian9laptop.parknet.dk>
* @date Mon Mar 5 21:03:35 2018
*
* @brief
*
* Copyright 2017 Jens Munk Hansen
*
*/
#include <cstdio>
#include <mutex>
#include <sps/resource.hpp>
#include <sps/context.hpp>
#include <sps/threadpool.hpp>
#if 0 // defined(__GNUC__)
# if !defined(__CYGWIN__)
# include <sps/strace.hpp>
# endif
#endif
#if defined(_WIN32)
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ulReasonForCall,
LPVOID lpReserved) {
SPS_UNREFERENCED_PARAMETERS(hModule, ulReasonForCall, lpReserved);
switch (ulReasonForCall) {
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
sps::Context::ThreadPoolDestroy();
break;
}
return TRUE;
}
#endif
namespace sps {
std::mutex g_mutex;
std::atomic<ThreadPool*> Context::g_threadpool{nullptr};
void Context::ThreadPoolInit() {
#if 0 // defined(__GNUC__) && !defined(__CYGWIN__)
# if !defined(NDEBUG)
sps::STrace::Instance().Enable();
# endif
#endif
}
void Context::ThreadPoolDestroy() {
# ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4099)
# endif
if (sps::Context::g_threadpool) {
delete sps::Context::g_threadpool;
}
# ifdef _MSC_VER
# pragma warning(pop)
# endif
}
ThreadPool* Context::DefaultThreadPoolGet() {
ThreadPool* pThreadPool = g_threadpool.load(std::memory_order_acquire);
if (!pThreadPool) {
std::lock_guard<std::mutex> guard(g_mutex);
pThreadPool = g_threadpool.load(std::memory_order_relaxed);
if (!pThreadPool) {
pThreadPool = new ThreadPool(1);
g_threadpool.store(pThreadPool, std::memory_order_release);
}
}
return pThreadPool;
}
ThreadPool* Context::ThreadPoolGet() {
return m_threadpool;
}
int Context::Create(ContextIF** ppContext) {
*ppContext = new Context();
return 0;
}
int Context::Destroy(ContextIF* pContext) {
if (pContext) {
delete pContext;
}
return 0;
}
Context::Context() {
m_threadpool = Context::DefaultThreadPoolGet();
m_id = Resource::UIDCreate();
}
} // namespace sps