Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/atomic.h

    rc53e813 r4621d23  
    4040#include <stdatomic.h>
    4141
    42 // TODO: Remove.
    43 // Before <stdatomic.h> was available, there was only one atomic type
    44 // equivalent to atomic_size_t. This means that in some places, atomic_t can
    45 // be replaced with a more appropriate type (e.g. atomic_bool for flags or
    46 // a signed type for potentially signed values).
    47 // So atomic_t should be replaced with the most appropriate type on a case by
    48 // case basis, and after there are no more uses, remove this type.
    49 typedef atomic_size_t atomic_t;
     42typedef size_t atomic_count_t;
     43typedef ssize_t atomic_signed_t;
    5044
    51 #define atomic_predec(val) \
    52         (atomic_fetch_sub((val), 1) - 1)
     45#define PRIua  "zu"          /**< Format for atomic_count_t. */
    5346
    54 #define atomic_preinc(val) \
    55         (atomic_fetch_add((val), 1) + 1)
     47typedef struct {
     48        volatile atomic_size_t count;
     49} atomic_t;
    5650
    57 #define atomic_postdec(val) \
    58         atomic_fetch_sub((val), 1)
     51static inline void atomic_set(atomic_t *val, atomic_count_t i)
     52{
     53        atomic_store(&val->count, i);
     54}
    5955
    60 #define atomic_postinc(val) \
    61         atomic_fetch_add((val), 1)
     56static inline atomic_count_t atomic_get(atomic_t *val)
     57{
     58        return atomic_load(&val->count);
     59}
    6260
    63 #define atomic_dec(val) \
    64         ((void) atomic_fetch_sub(val, 1))
     61static inline size_t atomic_predec(atomic_t *val)
     62{
     63        return atomic_fetch_sub(&val->count, 1) - 1;
     64}
    6565
    66 #define atomic_inc(val) \
    67         ((void) atomic_fetch_add(val, 1))
     66static inline size_t atomic_preinc(atomic_t *val)
     67{
     68        return atomic_fetch_add(&val->count, 1) + 1;
     69}
     70
     71static inline size_t atomic_postdec(atomic_t *val)
     72{
     73        return atomic_fetch_sub(&val->count, 1);
     74}
     75
     76static inline size_t atomic_postinc(atomic_t *val)
     77{
     78        return atomic_fetch_add(&val->count, 1);
     79}
     80
     81static inline void atomic_dec(atomic_t *val)
     82{
     83        atomic_fetch_sub(&val->count, 1);
     84}
     85
     86static inline void atomic_inc(atomic_t *val)
     87{
     88        atomic_fetch_add(&val->count, 1);
     89}
    6890
    6991#define local_atomic_exchange(var_addr, new_val) \
    7092        atomic_exchange_explicit(var_addr, new_val, memory_order_relaxed)
     93
     94static inline bool test_and_set(atomic_t *val)
     95{
     96        return atomic_exchange(&val->count, 1);
     97}
     98
    7199
    72100#endif
Note: See TracChangeset for help on using the changeset viewer.