Skip to content

Commit

Permalink
Emulate posix atomics for systems that do not have gnu builtins
Browse files Browse the repository at this point in the history
This is probably not very fast but if the compiler doesn't support the
gnu builtins, then speed is probably not a priority.
  • Loading branch information
aganm authored and SanderMertens committed May 4, 2023
1 parent f006bdc commit 8ae2081
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 16 deletions.
47 changes: 39 additions & 8 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -19464,6 +19464,13 @@ void ecs_set_os_api_impl(void) {
#include <time.h>
#endif

/* This mutex is used to emulate atomic operations when the gnu builtins are
* not supported. This is probably not very fast but if the compiler doesn't
* support the gnu built-ins, then speed is probably not a priority. */
#ifndef __GNUC__
static pthread_mutex_t atomic_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

static
ecs_os_thread_t posix_thread_new(
ecs_os_thread_callback_t callback,
Expand Down Expand Up @@ -19504,8 +19511,14 @@ int32_t posix_ainc(
value = __sync_add_and_fetch (count, 1);
return value;
#else
/* Unsupported */
abort();
if (pthread_mutex_lock(&atomic_mutex)) {
abort();
}
value = (*count) += 1;
if (pthread_mutex_unlock(&atomic_mutex)) {
abort();
}
return value;
#endif
}

Expand All @@ -19518,8 +19531,14 @@ int32_t posix_adec(
value = __sync_sub_and_fetch (count, 1);
return value;
#else
/* Unsupported */
abort();
if (pthread_mutex_lock(&atomic_mutex)) {
abort();
}
value = (*count) -= 1;
if (pthread_mutex_unlock(&atomic_mutex)) {
abort();
}
return value;
#endif
}

Expand All @@ -19532,8 +19551,14 @@ int64_t posix_lainc(
value = __sync_add_and_fetch (count, 1);
return value;
#else
/* Unsupported */
abort();
if (pthread_mutex_lock(&atomic_mutex)) {
abort();
}
value = (*count) += 1;
if (pthread_mutex_unlock(&atomic_mutex)) {
abort();
}
return value;
#endif
}

Expand All @@ -19546,8 +19571,14 @@ int64_t posix_ladec(
value = __sync_sub_and_fetch (count, 1);
return value;
#else
/* Unsupported */
abort();
if (pthread_mutex_lock(&atomic_mutex)) {
abort();
}
value = (*count) -= 1;
if (pthread_mutex_unlock(&atomic_mutex)) {
abort();
}
return value;
#endif
}

Expand Down
47 changes: 39 additions & 8 deletions src/addons/os_api_impl/posix_impl.inl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
#include <time.h>
#endif

/* This mutex is used to emulate atomic operations when the gnu builtins are
* not supported. This is probably not very fast but if the compiler doesn't
* support the gnu built-ins, then speed is probably not a priority. */
#ifndef __GNUC__
static pthread_mutex_t atomic_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

static
ecs_os_thread_t posix_thread_new(
ecs_os_thread_callback_t callback,
Expand Down Expand Up @@ -53,8 +60,14 @@ int32_t posix_ainc(
value = __sync_add_and_fetch (count, 1);
return value;
#else
/* Unsupported */
abort();
if (pthread_mutex_lock(&atomic_mutex)) {
abort();
}
value = (*count) += 1;
if (pthread_mutex_unlock(&atomic_mutex)) {
abort();
}
return value;
#endif
}

Expand All @@ -67,8 +80,14 @@ int32_t posix_adec(
value = __sync_sub_and_fetch (count, 1);
return value;
#else
/* Unsupported */
abort();
if (pthread_mutex_lock(&atomic_mutex)) {
abort();
}
value = (*count) -= 1;
if (pthread_mutex_unlock(&atomic_mutex)) {
abort();
}
return value;
#endif
}

Expand All @@ -81,8 +100,14 @@ int64_t posix_lainc(
value = __sync_add_and_fetch (count, 1);
return value;
#else
/* Unsupported */
abort();
if (pthread_mutex_lock(&atomic_mutex)) {
abort();
}
value = (*count) += 1;
if (pthread_mutex_unlock(&atomic_mutex)) {
abort();
}
return value;
#endif
}

Expand All @@ -95,8 +120,14 @@ int64_t posix_ladec(
value = __sync_sub_and_fetch (count, 1);
return value;
#else
/* Unsupported */
abort();
if (pthread_mutex_lock(&atomic_mutex)) {
abort();
}
value = (*count) -= 1;
if (pthread_mutex_unlock(&atomic_mutex)) {
abort();
}
return value;
#endif
}

Expand Down

0 comments on commit 8ae2081

Please sign in to comment.