Fixing compilation errors related to MUSL libc

Fix Musl libc NULL errors and unsupported pthread funcs for compatibility.
Also ensures cleanup and error handling irrespective of CPU affinity override.

Fix submitted by github dev - AngryLoki
https://github.com/ROCm/ROCR-Runtime/issues/181

Change-Id: Ia487315e504112be5d3370756f23f6e23b9ae4be


[ROCm/ROCR-Runtime commit: bc9cac97fe]
This commit is contained in:
Shweta.Khatri
2024-03-26 12:40:55 -04:00
gecommit door Shweta Khatri
bovenliggende ed7cdb88e4
commit 4f4d215196
5 gewijzigde bestanden met toevoegingen van 35 en 15 verwijderingen
@@ -58,7 +58,7 @@ template <uint64_t code, bool multiProcess = false> class Check final {
Check(const Check&) { object_ = uintptr_t(this) ^ uintptr_t(code); }
Check(Check&&) { object_ = uintptr_t(this) ^ uintptr_t(code); }
~Check() { object_ = NULL; }
~Check() { object_ = uintptr_t(NULL); }
const Check& operator=(Check&& rhs) { return *this; }
const Check& operator=(const Check& rhs) { return *this; }
@@ -57,7 +57,7 @@ int BusyWaitSignal::rtti_id_ = 0;
BusyWaitSignal::BusyWaitSignal(SharedSignal* abi_block, bool enableIPC)
: Signal(abi_block, enableIPC) {
signal_.kind = AMD_SIGNAL_KIND_USER;
signal_.event_mailbox_ptr = NULL;
signal_.event_mailbox_ptr = uint64_t(NULL);
}
hsa_signal_value_t BusyWaitSignal::LoadRelaxed() {
@@ -108,31 +108,44 @@ class os_thread {
err = pthread_attr_setstacksize(&attrib, stackSize);
if (err != 0) {
fprintf(stderr, "pthread_attr_setstacksize failed: %s\n", strerror(err));
err = pthread_attr_destroy(&attrib);
if (err != 0) {
fprintf(stderr, "pthread_attr_destroy failed: %s\n", strerror(err));
}
return;
}
}
\
err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get());
int create_err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get());
// Probably a stack size error since system limits can be different from PTHREAD_STACK_MIN
// Attempt to grow the stack within reason.
if ((err == EINVAL) && stackSize != 0) {
if ((create_err == EINVAL) && stackSize != 0) {
while (stackSize < 20 * 1024 * 1024) {
stackSize *= 2;
err = pthread_attr_setstacksize(&attrib, stackSize);
if (err != 0) {
fprintf(stderr, "pthread_attr_setstacksize failed: %s\n", strerror(err));
err = pthread_attr_destroy(&attrib);
if (err != 0) {
fprintf(stderr, "pthread_attr_destroy failed: %s\n", strerror(err));
}
return;
}
err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get());
if (err != EINVAL) break;
create_err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get());
if (create_err != EINVAL) break;
debug_print("pthread_create returned EINVAL, doubling stack size\n");
}
}
int cores = 0;
cpu_set_t* cpuset = nullptr;
if (core::Runtime::runtime_singleton_->flag().override_cpu_affinity()) {
int cores = get_nprocs_conf();
cpu_set_t* cpuset = CPU_ALLOC(cores);
cores = get_nprocs_conf();
cpuset = CPU_ALLOC(cores);
if (cpuset == nullptr) {
fprintf(stderr, "CPU_ALLOC failed: %s\n", strerror(errno));
return;
@@ -144,12 +157,11 @@ class os_thread {
err = pthread_setaffinity_np(thread, CPU_ALLOC_SIZE(cores), cpuset);
CPU_FREE(cpuset);
if (err != 0) {
fprintf(stderr, "pthread_attr_setaffinity_np failed: %s\n", strerror(err));
fprintf(stderr, "pthread_setaffinity_np failed: %s\n", strerror(err));
return;
}
}
if (err == 0)
if (create_err == 0)
args.release();
else
thread = 0;
@@ -642,11 +654,20 @@ SharedMutex CreateSharedMutex() {
fprintf(stderr, "rw lock attribute init failed: %s\n", strerror(err));
return nullptr;
}
#ifdef __GLIBC__
err = pthread_rwlockattr_setkind_np(&attrib, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
if (err != 0) {
fprintf(stderr, "Set rw lock attribute failure: %s\n", strerror(err));
return nullptr;
}
#else
err = pthread_rwlockattr_setkind(&attrib, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
if (err != 0) {
fprintf(stderr, "Set rw lock attribute failure: %s\n", strerror(err));
return nullptr;
}
#endif
pthread_rwlock_t* lock = new pthread_rwlock_t;
err = pthread_rwlock_init(lock, &attrib);
@@ -74,8 +74,7 @@ static __forceinline void* _aligned_malloc(size_t size, size_t alignment) {
return aligned_alloc(alignment, size);
#else
void *mem = NULL;
if (NULL != posix_memalign(&mem, alignment, size))
return NULL;
if (0 != posix_memalign(&mem, alignment, size)) return NULL;
return mem;
#endif
}
@@ -99,7 +99,7 @@ static __forceinline void* _aligned_malloc(size_t size, size_t alignment) {
return aligned_alloc(alignment, size);
#else
void* mem = NULL;
if (NULL != posix_memalign(&mem, alignment, size)) return NULL;
if (0 != posix_memalign(&mem, alignment, size)) return NULL;
return mem;
#endif
}