Skip to content

Commit

Permalink
sync: atomic counters
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real authored Jun 26, 2020
1 parent 195f4d0 commit def99be
Show file tree
Hide file tree
Showing 5 changed files with 951 additions and 0 deletions.
54 changes: 54 additions & 0 deletions thirdparty/stdatomic/nix/atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Compability header for stdatomic.h that works for all compilers supported
by V. For TCC the atomic features missing are implemented using mutex locks
*/
#ifndef __cplusplus
// If C just use stdatomic.h
#ifndef __TINYC__
#include <stdatomic.h>
#endif
#else
// CPP wrapper for atomic operations that are compatible with C
#include "atomic_cpp.h"
#endif

#ifdef __TINYC__
#include <pthread.h>

typedef intptr_t atomic_llong;
typedef intptr_t atomic_ullong;

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

/*
Wrapper for TCC to use mutex locks since it lacks the atomic functions
*/
static inline intptr_t atomic_fetch_add_explicit(intptr_t *x, size_t offset, int mo)
{
pthread_mutex_lock(&lock);

intptr_t old_value = *x;
*x = *x + offset;

pthread_mutex_unlock(&lock);

return old_value;
}

/*
Wrapper for TCC to use mutex locks since it lacks the atomic functions
*/
static inline intptr_t atomic_fetch_sub_explicit(intptr_t *x, size_t offset, int mo)
{
pthread_mutex_lock(&lock);

intptr_t old_value = *x;
*x = *x - offset;

pthread_mutex_unlock(&lock);

return old_value;
}

#endif
Loading

0 comments on commit def99be

Please sign in to comment.