axel-download-accelerator / axel

Lightweight CLI download accelerator
GNU General Public License v2.0
2.84k stars 258 forks source link

address argument to atomic operation must be a pointer to _Atomic type ('int *' invalid) #397

Closed yurivict closed 3 months ago

yurivict commented 1 year ago
src/random.c:13:8: error: address argument to atomic operation must be a pointer to _Atomic type ('int *' invalid)
                if (!atomic_compare_exchange_strong(&fd, &expect, tmp))
                     ^                              ~~~
/usr/include/stdatomic.h:348:2: note: expanded from macro 'atomic_compare_exchange_strong'
        atomic_compare_exchange_strong_explicit(object, expected,       \
        ^                                       ~~~~~~
/usr/include/stdatomic.h:240:2: note: expanded from macro 'atomic_compare_exchange_strong_explicit'
        __c11_atomic_compare_exchange_strong(object, expected, desired, \
        ^                                    ~~~~~~
1 error generated.

From the manpage:

ATOMIC_VAR_INIT(3)     FreeBSD Library Functions Manual     ATOMIC_VAR_INIT(3)

NAME
     ATOMIC_VAR_INIT, atomic_init, atomic_load, atomic_store, atomic_exchange,
     atomic_compare_exchange_strong, atomic_compare_exchange_weak,
     atomic_fetch_add, atomic_fetch_and, atomic_fetch_or, atomic_fetch_sub,
     atomic_fetch_xor, atomic_is_lock_free – type-generic atomic operations

SYNOPSIS
     #include <stdatomic.h>

     _Atomic(T) v = ATOMIC_VAR_INIT(c);
     void
     atomic_init(_Atomic(T) *object, T value);

     T
     atomic_load(_Atomic(T) *object);

     T
     atomic_load_explicit(_Atomic(T) *object, memory_order order);

     void
     atomic_store(_Atomic(T) *object, T desired);

     void
     atomic_store_explicit(_Atomic(T) *object, T desired, memory_order order);

     T
     atomic_exchange(_Atomic(T) *object, T desired);

     T
     atomic_exchange_explicit(_Atomic(T) *object, T desired,
         memory_order order);

clang-14 FreeBSD 13.1

yurivict commented 1 year ago

This patch solves it:

--- src/random.c.orig   2022-11-25 01:30:10 UTC
+++ src/random.c
@@ -6,7 +6,7 @@
 ssize_t
 axel_rand64(uint64_t *out)
 {
-       static int fd = -1;
+       static atomic_int fd = -1;
        if (fd == -1) {
                int tmp = open("/dev/random", O_RDONLY);
                int expect = -1;
jdblischak commented 3 months ago

@yurivict Thanks so much for diagnosing the problem and providing a patch! I used your patch in https://github.com/conda-forge/axel-feedstock/pull/3 to build a conda binary for axel for macOS