Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metal : replacing spinlock by mutex #7532

Merged
merged 1 commit into from
May 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/runtime/metal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "device_interface.h"
#include "gpu_context_common.h"
#include "printer.h"
#include "scoped_spin_lock.h"
#include "scoped_mutex_lock.h"

#include "objc_support.h"

Expand Down Expand Up @@ -280,7 +280,7 @@ WEAK mtl_device *get_default_mtl_device() {

extern WEAK halide_device_interface_t metal_device_interface;

volatile ScopedSpinLock::AtomicFlag WEAK thread_lock = 0;
WEAK halide_mutex thread_lock;
WEAK mtl_device *device;
WEAK mtl_command_queue *queue;

Expand Down Expand Up @@ -326,7 +326,7 @@ using namespace Halide::Runtime::Internal::Metal;
extern "C" {

// The default implementation of halide_metal_acquire_context uses the global
// pointers above, and serializes access with a spin lock.
// pointers above, and serializes access with a mutex.
// Overriding implementations of acquire/release must implement the following
// behavior:
// - halide_acquire_metal_context should always store a valid device/command
Expand All @@ -337,8 +337,7 @@ extern "C" {
WEAK int halide_metal_acquire_context(void *user_context, mtl_device **device_ret,
mtl_command_queue **queue_ret, bool create) {
halide_debug_assert(user_context, &thread_lock != nullptr);
while (__atomic_test_and_set(&thread_lock, __ATOMIC_ACQUIRE)) {
}
halide_mutex_lock(&thread_lock);

#ifdef DEBUG_RUNTIME
halide_start_clock(user_context);
Expand All @@ -348,7 +347,7 @@ WEAK int halide_metal_acquire_context(void *user_context, mtl_device **device_re
debug(user_context) << "Metal - Allocating: MTLCreateSystemDefaultDevice\n";
device = get_default_mtl_device();
if (device == nullptr) {
__atomic_clear(&thread_lock, __ATOMIC_RELEASE);
halide_mutex_unlock(&thread_lock);
error(user_context) << "halide_metal_acquire_context: cannot allocate system default device.";
return halide_error_code_generic_error;
}
Expand All @@ -357,7 +356,7 @@ WEAK int halide_metal_acquire_context(void *user_context, mtl_device **device_re
if (queue == nullptr) {
release_ns_object(device);
device = nullptr;
__atomic_clear(&thread_lock, __ATOMIC_RELEASE);
halide_mutex_unlock(&thread_lock);
error(user_context) << "halide_metal_acquire_context: cannot allocate command queue.";
return halide_error_code_generic_error;
}
Expand All @@ -376,7 +375,7 @@ WEAK int halide_metal_acquire_context(void *user_context, mtl_device **device_re
}

WEAK int halide_metal_release_context(void *user_context) {
__atomic_clear(&thread_lock, __ATOMIC_RELEASE);
halide_mutex_unlock(&thread_lock);
return halide_error_code_success;
}

Expand Down