Changeset 7137f74c in mainline


Ignore:
Timestamp:
2018-07-19T21:02:13Z (7 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:
514d561
Parents:
5a50430
Message:

Implement some of GCC's sync_* builtins in using <atomic.h>.

Location:
uspace/lib
Files:
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/Makefile

    r5a50430 r7137f74c  
    5151        generic/libc.c \
    5252        generic/ddi.c \
     53        generic/atomic.c \
    5354        generic/as.c \
    5455        generic/bd.c \
  • uspace/lib/c/arch/arm32/include/libarch/atomic.h

    r5a50430 r7137f74c  
    5252        /*
    5353         * The following instructions between labels 1 and 2 constitute a
    54          * Restartable Atomic Seqeunce. Should the sequence be non-atomic,
     54         * Restartable Atomic Sequence. Should the sequence be non-atomic,
    5555         * the kernel will restart it.
    5656         */
  • uspace/lib/c/generic/atomic.c

    r5a50430 r7137f74c  
    11/*
    2  * Copyright (c) 2018 Jaroslav Jindrak
     2 * Copyright (c) 2018 CZ.NIC, z.s.p.o.
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29 /**
    30  * This file contains glue code that makes different
    31  * architectures pass.
    32  */
     29#include <atomic.h>
    3330
    3431#ifdef PLATFORM_arm32
    3532
    36 /**
    37  * ARM32 does not have GCC atomic operations inlined by
    38  * the compiler, so we need to define stubs for our library
    39  * to compile on this architecture.
    40  * TODO: make this synchronized
     33/*
     34 * Older ARMs don't have atomic instructions, so we need to define a bunch
     35 * of symbols for GCC to use.
    4136 */
    42 extern "C"
     37
     38unsigned __sync_add_and_fetch_4(volatile void *vptr, unsigned val)
    4339{
    44 #define LIBCPP_GLUE_OP_AND_FETCH(NAME, OP, TYPE, SIZE) \
    45     TYPE __sync_##NAME##_and_fetch_##SIZE (volatile void* vptr, TYPE val) \
    46     { \
    47         TYPE* ptr = (TYPE*)vptr; \
    48         *ptr = *ptr OP val; \
    49         return *ptr; \
    50     }
     40        return atomic_add((atomic_t *)vptr, val);
     41}
    5142
    52 LIBCPP_GLUE_OP_AND_FETCH(add, +, unsigned, 4)
    53 LIBCPP_GLUE_OP_AND_FETCH(sub, -, unsigned, 4)
     43unsigned __sync_sub_and_fetch_4(volatile void *vptr, unsigned val)
     44{
     45        return atomic_add((atomic_t *)vptr, -(atomic_signed_t)val);
     46}
    5447
    55 #define LIBCPP_GLUE_CMP_AND_SWAP(TYPE, SIZE) \
    56     TYPE __sync_val_compare_and_swap_##SIZE (TYPE* ptr, TYPE old_val, TYPE new_val) \
    57     { \
    58         if (*ptr == old_val) \
    59             *ptr = new_val; \
    60         return *ptr; \
    61     }
     48bool __sync_bool_compare_and_swap_4(volatile void *ptr, unsigned old_val, unsigned new_val)
     49{
     50        return cas((atomic_t *)ptr, old_val, new_val);
     51}
    6252
    63 LIBCPP_GLUE_CMP_AND_SWAP(unsigned, 4)
     53unsigned __sync_val_compare_and_swap_4(volatile void *ptr, unsigned old_val, unsigned new_val)
     54{
     55        while (true) {
     56                if (__sync_bool_compare_and_swap_4(ptr, old_val, new_val)) {
     57                        return old_val;
     58                }
     59
     60                unsigned current = *(volatile unsigned *)ptr;
     61                if (current != old_val)
     62                        return current;
     63
     64                /* If the current value is the same as old_val, retry. */
     65        }
    6466}
     67
    6568#endif
  • uspace/lib/cpp/Makefile

    r5a50430 r7137f74c  
    5353        src/typeindex.cpp \
    5454        src/typeinfo.cpp \
    55         src/__bits/glue.cpp \
    5655        src/__bits/runtime.cpp \
    5756        src/__bits/trycatch.cpp \
Note: See TracChangeset for help on using the changeset viewer.