-
Notifications
You must be signed in to change notification settings - Fork 769
/
Copy pathglobal_handler.hpp
134 lines (113 loc) · 4.4 KB
/
global_handler.hpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//==--------- global_handler.hpp --- Global objects handler ----------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#pragma once
#include <sycl/detail/spinlock.hpp>
#include <sycl/detail/util.hpp>
#include <memory>
#include <unordered_map>
namespace sycl {
inline namespace _V1 {
namespace detail {
class platform_impl;
class context_impl;
class Scheduler;
class ProgramManager;
class Sync;
class Adapter;
class ods_target_list;
class XPTIRegistry;
class ThreadPool;
using ContextImplPtr = std::shared_ptr<context_impl>;
using AdapterPtr = std::shared_ptr<Adapter>;
/// Wrapper class for global data structures with non-trivial destructors.
///
/// As user code can call SYCL Runtime functions from destructor of global
/// objects, it is not safe for the runtime library to have global objects with
/// non-trivial destructors. Such destructors can be called any time after
/// exiting main, which may result in user application crashes. Instead,
/// complex global objects must be wrapped into GlobalHandler. Its instance
/// is stored on heap, and deallocated when the runtime library is being
/// unloaded.
///
/// There's no need to store trivial globals here, as no code for their
/// construction or destruction is generated anyway.
class GlobalHandler {
public:
/// \return a reference to a GlobalHandler singleton instance. Memory for
/// storing objects is allocated on first call. The reference is valid as long
/// as runtime library is loaded (i.e. untill `DllMain` or
/// `__attribute__((destructor))` is called).
static GlobalHandler &instance();
GlobalHandler(const GlobalHandler &) = delete;
GlobalHandler(GlobalHandler &&) = delete;
GlobalHandler &operator=(const GlobalHandler &) = delete;
void registerSchedulerUsage(bool ModifyCounter = true);
Scheduler &getScheduler();
bool isSchedulerAlive() const;
ProgramManager &getProgramManager();
Sync &getSync();
std::vector<std::shared_ptr<platform_impl>> &getPlatformCache();
std::unordered_map<platform_impl *, ContextImplPtr> &
getPlatformToDefaultContextCache();
std::mutex &getPlatformToDefaultContextCacheMutex();
std::mutex &getPlatformMapMutex();
std::mutex &getFilterMutex();
std::vector<AdapterPtr> &getAdapters();
ods_target_list &getOneapiDeviceSelectorTargets(const std::string &InitValue);
XPTIRegistry &getXPTIRegistry();
ThreadPool &getHostTaskThreadPool();
static void registerEarlyShutdownHandler();
bool isOkToDefer() const;
void endDeferredRelease();
void unloadAdapters();
void releaseDefaultContexts();
void drainThreadPool();
void prepareSchedulerToRelease(bool Blocking);
void InitXPTI();
void TraceEventXPTI(const char *Message);
// For testing purposes only
void attachScheduler(Scheduler *Scheduler);
private:
#ifdef XPTI_ENABLE_INSTRUMENTATION
void *GSYCLCallEvent = nullptr;
#endif
bool OkToDefer = true;
friend void shutdown_win();
friend void shutdown_early();
friend void shutdown_late();
friend class ObjectUsageCounter;
static GlobalHandler *&getInstancePtr();
static SpinLock MSyclGlobalHandlerProtector;
// Constructor and destructor are declared out-of-line to allow incomplete
// types as template arguments to unique_ptr.
GlobalHandler();
~GlobalHandler();
template <typename T> struct InstWithLock {
std::unique_ptr<T> Inst;
SpinLock Lock;
};
template <typename T, typename... Types>
T &getOrCreate(InstWithLock<T> &IWL, Types &&...Args);
InstWithLock<Scheduler> MScheduler;
InstWithLock<ProgramManager> MProgramManager;
InstWithLock<Sync> MSync;
InstWithLock<std::vector<std::shared_ptr<platform_impl>>> MPlatformCache;
InstWithLock<std::unordered_map<platform_impl *, ContextImplPtr>>
MPlatformToDefaultContextCache;
InstWithLock<std::mutex> MPlatformToDefaultContextCacheMutex;
InstWithLock<std::mutex> MPlatformMapMutex;
InstWithLock<std::mutex> MFilterMutex;
InstWithLock<std::vector<AdapterPtr>> MAdapters;
InstWithLock<ods_target_list> MOneapiDeviceSelectorTargets;
InstWithLock<XPTIRegistry> MXPTIRegistry;
// Thread pool for host task and event callbacks execution
InstWithLock<ThreadPool> MHostTaskThreadPool;
};
} // namespace detail
} // namespace _V1
} // namespace sycl