Changeset 4621d23 in mainline for kernel/generic/include/atomic.h


Ignore:
Timestamp:
2018-09-06T19:54:11Z (6 years ago)
Author:
Jiří Zárevúcky <jiri.zarevucky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
508b0df1
Parents:
ffa73c6
git-author:
Jiří Zárevúcky <jiri.zarevucky@…> (2018-08-13 03:00:17)
git-committer:
Jiří Zárevúcky <jiri.zarevucky@…> (2018-09-06 19:54:11)
Message:

Use compiler builtins for kernel atomics

File:
1 edited

Legend:

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

    rffa73c6 r4621d23  
    3636#define KERN_ATOMIC_H_
    3737
     38#include <stdbool.h>
    3839#include <typedefs.h>
    39 #include <arch/atomic.h>
    40 #include <verify.h>
     40#include <stdatomic.h>
    4141
    42 NO_TRACE ATOMIC static inline void atomic_set(atomic_t *val, atomic_count_t i)
    43     WRITES(&val->count)
    44     REQUIRES_EXTENT_MUTABLE(val)
     42typedef size_t atomic_count_t;
     43typedef ssize_t atomic_signed_t;
     44
     45#define PRIua  "zu"          /**< Format for atomic_count_t. */
     46
     47typedef struct {
     48        volatile atomic_size_t count;
     49} atomic_t;
     50
     51static inline void atomic_set(atomic_t *val, atomic_count_t i)
    4552{
    46         val->count = i;
     53        atomic_store(&val->count, i);
    4754}
    4855
    49 NO_TRACE ATOMIC static inline atomic_count_t atomic_get(atomic_t *val)
    50     REQUIRES_EXTENT_MUTABLE(val)
     56static inline atomic_count_t atomic_get(atomic_t *val)
    5157{
    52         return val->count;
     58        return atomic_load(&val->count);
    5359}
    5460
     61static inline size_t atomic_predec(atomic_t *val)
     62{
     63        return atomic_fetch_sub(&val->count, 1) - 1;
     64}
    5565
    56 /*
    57  * If the architecture does not provide operations that are atomic
    58  * only with respect to the local cpu (eg exception handlers) and
    59  * not other cpus, implement these cpu local atomic operations with
    60  * full blown smp-safe atomics.
    61  */
    62 #ifndef local_atomic_exchange
     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}
     90
    6391#define local_atomic_exchange(var_addr, new_val) \
    64         __atomic_exchange_n((var_addr), (new_val), __ATOMIC_RELAXED)
    65 #endif
     92        atomic_exchange_explicit(var_addr, new_val, memory_order_relaxed)
    6693
     94static inline bool test_and_set(atomic_t *val)
     95{
     96        return atomic_exchange(&val->count, 1);
     97}
    6798
    6899
Note: See TracChangeset for help on using the changeset viewer.