Skip to content

Commit 84fdf33

Browse files
committed
Make erlang:unique_integer use ref_ticks
AtomVM has already an internal unique integer that is stored in GlobalContext, use it, rather than having another unique_integer. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 2677e9d commit 84fdf33

File tree

1 file changed

+2
-43
lines changed

1 file changed

+2
-43
lines changed

src/libAtomVM/bif.c

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -344,50 +344,9 @@ term bif_erlang_map_get_2(Context *ctx, uint32_t fail_label, term arg1, term arg
344344
return term_get_map_value(arg2, pos);
345345
}
346346

347-
#ifdef AVM_NO_SMP
348-
static int64_t get_unique_monotonic_integer(void)
349-
{
350-
static int64_t unique = 0;
351-
352-
if (UNLIKELY(unique == INT64_MAX)) {
353-
AVM_ABORT();
354-
}
355-
return unique++;
356-
}
357-
358-
#elif ATOMIC_LLONG_LOCK_FREE == 2
359-
360-
static int64_t get_unique_monotonic_integer(void)
361-
{
362-
static int64_t ATOMIC unique = 0;
363-
364-
if (UNLIKELY(unique == INT64_MAX)) {
365-
AVM_ABORT();
366-
}
367-
return unique++;
368-
}
369-
#else
370-
371-
static int64_t get_unique_monotonic_integer(void)
372-
{
373-
static int64_t unique = 0;
374-
static SpinLock unique_spinlock;
375-
376-
if (UNLIKELY(unique == INT64_MAX)) {
377-
AVM_ABORT();
378-
}
379-
380-
smp_spinlock_lock(&unique_spinlock);
381-
int64_t value = unique++;
382-
smp_spinlock_unlock(&unique_spinlock);
383-
return value;
384-
}
385-
386-
#endif
387-
388347
term bif_erlang_unique_integer_0(Context *ctx)
389348
{
390-
int64_t value = get_unique_monotonic_integer();
349+
int64_t value = globalcontext_get_ref_ticks(ctx->global);
391350
return term_make_maybe_boxed_int64(value, &ctx->heap);
392351
}
393352

@@ -408,7 +367,7 @@ term bif_erlang_unique_integer_1(Context *ctx, uint32_t fail_label, term arg1)
408367
//
409368
// Right now the implementation is backed by increasing counter
410369
// that always covers both options
411-
int64_t value = get_unique_monotonic_integer();
370+
int64_t value = globalcontext_get_ref_ticks(ctx->global);
412371
return term_make_maybe_boxed_int64(value, &ctx->heap);
413372
}
414373

0 commit comments

Comments
 (0)