Skip to content

Commit

Permalink
Expose atomic refcounts to be easier to port.
Browse files Browse the repository at this point in the history
This code was duplicate in a few places.
  • Loading branch information
bakpakin committed Oct 1, 2023
1 parent 26aa622 commit 7cdd7cf
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file.

## Unreleased - ???
- Expose atomic refcount abstraction in janet.h
- Add `array/weak` for weak references in arrays
- Add support for weak tables via `table/weak`, `table/weak-keys`, and `table/weak-values`.
- Fix compiler bug with using the result of `(break x)` expression in some contexts.
Expand Down
20 changes: 2 additions & 18 deletions src/core/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ size_t janet_os_rwlock_size(void) {
return sizeof(void *);
}

static int32_t janet_incref(JanetAbstractHead *ab) {
return InterlockedIncrement((LONG volatile *) &ab->gc.data.refcount);
}

static int32_t janet_decref(JanetAbstractHead *ab) {
return InterlockedDecrement((LONG volatile *) &ab->gc.data.refcount);
}

void janet_os_mutex_init(JanetOSMutex *mutex) {
InitializeCriticalSection((CRITICAL_SECTION *) mutex);
}
Expand Down Expand Up @@ -157,14 +149,6 @@ size_t janet_os_rwlock_size(void) {
return sizeof(pthread_rwlock_t);
}

static int32_t janet_incref(JanetAbstractHead *ab) {
return __atomic_add_fetch(&ab->gc.data.refcount, 1, __ATOMIC_RELAXED);
}

static int32_t janet_decref(JanetAbstractHead *ab) {
return __atomic_add_fetch(&ab->gc.data.refcount, -1, __ATOMIC_RELAXED);
}

void janet_os_mutex_init(JanetOSMutex *mutex) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
Expand Down Expand Up @@ -212,11 +196,11 @@ void janet_os_rwlock_wunlock(JanetOSRWLock *rwlock) {
#endif

int32_t janet_abstract_incref(void *abst) {
return janet_incref(janet_abstract_head(abst));
return janet_atomic_inc(&janet_abstract_head(abst)->gc.data.refcount);
}

int32_t janet_abstract_decref(void *abst) {
return janet_decref(janet_abstract_head(abst));
return janet_atomic_dec(&janet_abstract_head(abst)->gc.data.refcount);
}

#endif
34 changes: 34 additions & 0 deletions src/core/capi.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,40 @@ void *janet_optabstract(const Janet *argv, int32_t argc, int32_t n, const JanetA
return janet_getabstract(argv, n, at);
}

/* Atomic refcounts */

JanetAtomicInt janet_atomic_inc(JanetAtomicInt volatile *x) {
#ifdef JANET_WINDOWS
return InterlockedIncrement(x);
#else
return __atomic_add_fetch(x, 1, __ATOMIC_RELAXED);
#endif
}

JanetAtomicInt janet_atomic_dec(JanetAtomicInt volatile *x) {
#ifdef JANET_WINDOWS
return InterlockedDecrement(x);
#else
return __atomic_add_fetch(x, -1, __ATOMIC_RELAXED);
#endif
}

JanetAtomicInt64 janet_atomic64_inc(JanetAtomicInt64 volatile *x) {
#ifdef JANET_WINDOWS
return InterlockedDecrement(x);
#else
return __atomic_add_fetch(x, 1, __ATOMIC_RELAXED);
#endif
}

JanetAtomicInt64 janet_atomic64_dec(JanetAtomicInt64 volatile *x) {
#ifdef JANET_WINDOWS
return InterlockedDecrement64(x);
#else
return __atomic_add_fetch(x, -1, __ATOMIC_RELAXED);
#endif
}

/* Some definitions for function-like macros */

JANET_API JanetStructHead *(janet_struct_head)(JanetStruct st) {
Expand Down
26 changes: 5 additions & 21 deletions src/core/ev.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,27 +633,11 @@ void janet_addtimeout(double sec) {
}

void janet_ev_inc_refcount(void) {
#ifdef JANET_WINDOWS
#ifdef JANET_64
InterlockedIncrement64((int64_t volatile *) &janet_vm.extra_listeners);
#else
InterlockedIncrement((int32_t volatile *) &janet_vm.extra_listeners);
#endif
#else
__atomic_add_fetch(&janet_vm.extra_listeners, 1, __ATOMIC_RELAXED);
#endif
janet_atomic64_inc(&janet_vm.listener_count);
}

void janet_ev_dec_refcount(void) {
#ifdef JANET_WINDOWS
#ifdef JANET_64
InterlockedDecrement64((int64_t volatile *) &janet_vm.extra_listeners);
#else
InterlockedDecrement((int32_t volatile *) &janet_vm.extra_listeners);
#endif
#else
__atomic_add_fetch(&janet_vm.extra_listeners, -1, __ATOMIC_RELAXED);
#endif
janet_atomic64_dec(&janet_vm.listener_count);
}

/* Channels */
Expand Down Expand Up @@ -1334,7 +1318,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout);
int janet_loop_done(void) {
return !((janet_vm.spawn.head != janet_vm.spawn.tail) ||
janet_vm.tq_count ||
janet_vm.extra_listeners);
janet_vm.listener_count);
}

JanetFiber *janet_loop1(void) {
Expand Down Expand Up @@ -1396,7 +1380,7 @@ JanetFiber *janet_loop1(void) {
}

/* Poll for events */
if (janet_vm.tq_count || janet_vm.extra_listeners) {
if (janet_vm.tq_count || janet_vm.listener_count) {
JanetTimeout to;
memset(&to, 0, sizeof(to));
int has_timeout;
Expand All @@ -1415,7 +1399,7 @@ JanetFiber *janet_loop1(void) {
break;
}
/* Run polling implementation only if pending timeouts or pending events */
if (janet_vm.tq_count || janet_vm.extra_listeners) {
if (janet_vm.tq_count || janet_vm.listener_count) {
janet_loop1_impl(has_timeout, to.when);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ struct JanetVM {
JanetQueue spawn;
JanetTimeout *tq;
JanetRNG ev_rng;
volatile size_t extra_listeners; /* used in signal handler, must be volatile */
volatile JanetAtomicInt64 listener_count; /* used in signal handler, must be volatile */
JanetTable threaded_abstracts; /* All abstract types that can be shared between threads (used in this thread) */
JanetTable active_tasks; /* All possibly live task fibers - used just for tracking */
JanetArray *listeners; /* For GC */
Expand Down
16 changes: 15 additions & 1 deletion src/include/janet.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,20 @@ struct JanetListenerState {
};
#endif

/* Janet uses atomic integers in several places for synchronization between threads and
* signals. Define them here */
#ifdef JANET_WINDOWS
typedef long JanetAtomicInt;
typedef long long JanetAtomicInt64;
#else
typedef int32_t JanetAtomicInt;
typedef int64_t JanetAtomicInt64;
#endif
JANET_API JanetAtomicInt janet_atomic_inc(JanetAtomicInt volatile *x);
JANET_API JanetAtomicInt janet_atomic_dec(JanetAtomicInt volatile *x);
JANET_API JanetAtomicInt64 janet_atomic64_inc(JanetAtomicInt64 volatile *x);
JANET_API JanetAtomicInt64 janet_atomic64_dec(JanetAtomicInt64 volatile *x);

/* We provide three possible implementations of Janets. The preferred
* nanboxing approach, for 32 or 64 bits, and the standard C version. Code in the rest of the
* application must interact through exposed interface. */
Expand Down Expand Up @@ -896,7 +910,7 @@ struct JanetGCObject {
int32_t flags;
union {
JanetGCObject *next;
int32_t refcount; /* For threaded abstract types */
volatile JanetAtomicInt refcount; /* For threaded abstract types */
} data;
};

Expand Down

0 comments on commit 7cdd7cf

Please sign in to comment.