Skip to content

Commit a332c2d

Browse files
committed
rocr: Dynamically allocate static global memory
To allow non-POD global variables to last until the last thread has exited, use "new" to allocate the memory instead of static allocation. Change-Id: Ica571b61ff8068a52e472c49cb1c44917e60c8c8
1 parent b410c40 commit a332c2d

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

runtime/hsa-runtime/core/inc/amd_aql_queue.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,18 @@ class AqlQueue : public core::Queue, private core::LocalSignal, public core::Doo
337337
}
338338
// Queue count - used to ref count queue_event_
339339
static __forceinline std::atomic<uint32_t>& queue_count() {
340-
static std::atomic<uint32_t> queue_count_(0);
341-
return queue_count_;
340+
// This allocation is meant to last until the last thread has exited.
341+
// It is intentionally not freed.
342+
static std::atomic<uint32_t>* queue_count_ = new std::atomic<uint32_t>(0);
343+
return *queue_count_;
342344
}
343345

344346
// Mutex for queue_event_ manipulation
345-
static __forceinline KernelMutex& queue_lock() {
346-
static KernelMutex queue_lock_;
347-
return queue_lock_;
347+
KernelMutex& queue_lock() {
348+
// This allocation is meant to last until the last thread has exited.
349+
// It is intentionally not freed.
350+
static KernelMutex* queue_lock_ = new KernelMutex();
351+
return *queue_lock_;
348352
}
349353
// Async scratch single limit - may be modified after init
350354
size_t async_scratch_single_limit_;

runtime/hsa-runtime/core/inc/host_queue.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ class HostQueue : public Queue {
189189
// Host queue id counter, starting from 0x80000000 to avoid overlaping
190190
// with aql queue id.
191191
static __forceinline std::atomic<uint32_t>& queue_count() {
192-
static std::atomic<uint32_t> queue_count_;
193-
return queue_count_;
192+
// This allocation is meant to last until the last thread has exited.
193+
// It is intentionally not freed.
194+
static std::atomic<uint32_t>* queue_count_ = new std::atomic<uint32_t>();
195+
return *queue_count_;
194196
}
195197

196198
DISALLOW_COPY_AND_ASSIGN(HostQueue);

runtime/hsa-runtime/core/inc/runtime.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,10 @@ class Runtime {
584584
// Will be created before any user could call hsa_init but also could be
585585
// destroyed before incorrectly written programs call hsa_shutdown.
586586
static __forceinline KernelMutex& bootstrap_lock() {
587-
static KernelMutex bootstrap_lock_;
588-
return bootstrap_lock_;
587+
// This allocation is meant to last until the last thread has exited.
588+
// It is intentionally not freed.
589+
static KernelMutex* bootstrap_lock_ = new KernelMutex;
590+
return *bootstrap_lock_;
589591
}
590592
Runtime();
591593

runtime/hsa-runtime/image/image_runtime.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,17 @@ class ImageRuntime {
164164

165165
/// Pointer to singleton object.
166166
static __forceinline std::atomic<ImageRuntime*>& get_instance() {
167-
static std::atomic<ImageRuntime*> instance_(NULL);
168-
return instance_;
167+
// This allocation is meant to last until the last thread has exited.
168+
// It is intentionally not freed.
169+
static std::atomic<ImageRuntime*>* instance_ = new std::atomic<ImageRuntime*>();
170+
return *instance_;
169171
}
170172

171173
static __forceinline std::mutex& instance_mutex() {
172-
static std::mutex instance_mutex_;
173-
return instance_mutex_;
174+
// This allocation is meant to last until the last thread has exited.
175+
// It is intentionally not freed.
176+
static std::mutex* instance_mutex_ = new std::mutex();
177+
return *instance_mutex_;
174178
}
175179

176180
/// @brief Contains mapping of agent and its corresponding ::ImageManager

runtime/hsa-runtime/pcs/pcs_runtime.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,16 @@ class PcsRuntime {
153153

154154
/// Pointer to singleton object.
155155
static __forceinline std::atomic<PcsRuntime*>& get_instance() {
156-
static std::atomic<PcsRuntime*> instance_(nullptr);
157-
return instance_;
156+
// This allocation is meant to last until the last thread has exited.
157+
// It is intentionally not freed.
158+
static std::atomic<PcsRuntime*>* instance_ = new std::atomic<PcsRuntime*>();
159+
return *instance_;
158160
}
159161
static __forceinline std::mutex& instance_mutex() {
160-
static std::mutex instance_mutex_;
161-
return instance_mutex_;
162+
// This allocation is meant to last until the last thread has exited.
163+
// It is intentionally not freed.
164+
static std::mutex* instance_mutex_ = new std::mutex();
165+
return *instance_mutex_;
162166
}
163167
// Map of pc sampling sessions indexed by hsa_ven_amd_pcs_t handle
164168
std::map<uint64_t, PcSamplingSession> pc_sampling_;

0 commit comments

Comments
 (0)