forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_handler.hpp
More file actions
121 lines (103 loc) · 4.13 KB
/
Copy pathglobal_handler.hpp
File metadata and controls
121 lines (103 loc) · 4.13 KB
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
//==--------- 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 {
__SYCL_INLINE_VER_NAMESPACE(_V1) {
namespace detail {
class platform_impl;
class context_impl;
class Scheduler;
class ProgramManager;
class Sync;
class plugin;
class device_filter_list;
class ods_target_list;
class XPTIRegistry;
class ThreadPool;
using PlatformImplPtr = std::shared_ptr<platform_impl>;
using ContextImplPtr = std::shared_ptr<context_impl>;
/// 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;
void registerSchedulerUsage(bool ModifyCounter = true);
Scheduler &getScheduler();
ProgramManager &getProgramManager();
Sync &getSync();
std::vector<PlatformImplPtr> &getPlatformCache();
std::unordered_map<PlatformImplPtr, ContextImplPtr> &
getPlatformToDefaultContextCache();
std::mutex &getPlatformToDefaultContextCacheMutex();
std::mutex &getPlatformMapMutex();
std::mutex &getFilterMutex();
std::vector<plugin> &getPlugins();
device_filter_list &getDeviceFilterList(const std::string &InitValue);
ods_target_list &getOneapiDeviceSelectorTargets(const std::string &InitValue);
XPTIRegistry &getXPTIRegistry();
ThreadPool &getHostTaskThreadPool();
static void registerDefaultContextReleaseHandler();
void unloadPlugins();
void releaseDefaultContexts();
void drainThreadPool();
// For testing purposes only
void attachScheduler(Scheduler *Scheduler);
private:
friend void shutdown();
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<PlatformImplPtr>> MPlatformCache;
InstWithLock<std::unordered_map<PlatformImplPtr, ContextImplPtr>>
MPlatformToDefaultContextCache;
InstWithLock<std::mutex> MPlatformToDefaultContextCacheMutex;
InstWithLock<std::mutex> MPlatformMapMutex;
InstWithLock<std::mutex> MFilterMutex;
InstWithLock<std::vector<plugin>> MPlugins;
InstWithLock<device_filter_list> MDeviceFilterList;
InstWithLock<ods_target_list> MOneapiDeviceSelectorTargets;
InstWithLock<XPTIRegistry> MXPTIRegistry;
// Thread pool for host task and event callbacks execution
InstWithLock<ThreadPool> MHostTaskThreadPool;
};
} // namespace detail
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
} // namespace sycl