Path: blob/main/sys/contrib/ck/include/gcc/ck_pr.h
48378 views
/*1* Copyright 2010 Samy Al Bahra.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#ifndef CK_PR_GCC_H27#define CK_PR_GCC_H2829#ifndef CK_PR_H30#error Do not include this file directly, use ck_pr.h31#endif3233#include <ck_cc.h>3435CK_CC_INLINE static void36ck_pr_barrier(void)37{3839__asm__ __volatile__("" ::: "memory");40return;41}4243#ifndef CK_F_PR44#define CK_F_PR4546#include <ck_stdbool.h>47#include <ck_stdint.h>4849/*50* The following represent supported atomic operations.51* These operations may be emulated.52*/53#include "ck_f_pr.h"5455#define CK_PR_ACCESS(x) (*(volatile __typeof__(x) *)&(x))5657#define CK_PR_LOAD(S, M, T) \58CK_CC_INLINE static T \59ck_pr_md_load_##S(const M *target) \60{ \61T r; \62ck_pr_barrier(); \63r = CK_PR_ACCESS(*(const T *)target); \64ck_pr_barrier(); \65return (r); \66} \67CK_CC_INLINE static void \68ck_pr_md_store_##S(M *target, T v) \69{ \70ck_pr_barrier(); \71CK_PR_ACCESS(*(T *)target) = v; \72ck_pr_barrier(); \73return; \74}7576CK_CC_INLINE static void *77ck_pr_md_load_ptr(const void *target)78{79void *r;8081ck_pr_barrier();82r = CK_CC_DECONST_PTR(*(volatile void *const*)(target));83ck_pr_barrier();8485return r;86}8788CK_CC_INLINE static void89ck_pr_md_store_ptr(void *target, const void *v)90{9192ck_pr_barrier();93*(volatile void **)target = CK_CC_DECONST_PTR(v);94ck_pr_barrier();95return;96}9798#define CK_PR_LOAD_S(S, T) CK_PR_LOAD(S, T, T)99100CK_PR_LOAD_S(char, char)101CK_PR_LOAD_S(uint, unsigned int)102CK_PR_LOAD_S(int, int)103#ifndef CK_PR_DISABLE_DOUBLE104CK_PR_LOAD_S(double, double)105#endif106CK_PR_LOAD_S(64, uint64_t)107CK_PR_LOAD_S(32, uint32_t)108CK_PR_LOAD_S(16, uint16_t)109CK_PR_LOAD_S(8, uint8_t)110111#undef CK_PR_LOAD_S112#undef CK_PR_LOAD113114CK_CC_INLINE static void115ck_pr_stall(void)116{117118ck_pr_barrier();119}120121/*122* Load and store fences are equivalent to full fences in the GCC port.123*/124#define CK_PR_FENCE(T) \125CK_CC_INLINE static void \126ck_pr_fence_strict_##T(void) \127{ \128__sync_synchronize(); \129}130131CK_PR_FENCE(atomic)132CK_PR_FENCE(atomic_atomic)133CK_PR_FENCE(atomic_load)134CK_PR_FENCE(atomic_store)135CK_PR_FENCE(store_atomic)136CK_PR_FENCE(load_atomic)137CK_PR_FENCE(load)138CK_PR_FENCE(load_load)139CK_PR_FENCE(load_store)140CK_PR_FENCE(store)141CK_PR_FENCE(store_store)142CK_PR_FENCE(store_load)143CK_PR_FENCE(memory)144CK_PR_FENCE(acquire)145CK_PR_FENCE(release)146CK_PR_FENCE(acqrel)147CK_PR_FENCE(lock)148CK_PR_FENCE(unlock)149150#undef CK_PR_FENCE151152/*153* Atomic compare and swap.154*/155#define CK_PR_CAS(S, M, T) \156CK_CC_INLINE static bool \157ck_pr_cas_##S(M *target, T compare, T set) \158{ \159bool z; \160z = __sync_bool_compare_and_swap((T *)target, compare, set); \161return z; \162}163164CK_PR_CAS(ptr, void, void *)165166#define CK_PR_CAS_S(S, T) CK_PR_CAS(S, T, T)167168CK_PR_CAS_S(char, char)169CK_PR_CAS_S(int, int)170CK_PR_CAS_S(uint, unsigned int)171CK_PR_CAS_S(64, uint64_t)172CK_PR_CAS_S(32, uint32_t)173CK_PR_CAS_S(16, uint16_t)174CK_PR_CAS_S(8, uint8_t)175176#undef CK_PR_CAS_S177#undef CK_PR_CAS178179/*180* Compare and swap, set *v to old value of target.181*/182CK_CC_INLINE static bool183ck_pr_cas_ptr_value(void *target, void *compare, void *set, void *v)184{185set = __sync_val_compare_and_swap((void **)target, compare, set);186*(void **)v = set;187return (set == compare);188}189190#define CK_PR_CAS_O(S, T) \191CK_CC_INLINE static bool \192ck_pr_cas_##S##_value(T *target, T compare, T set, T *v) \193{ \194set = __sync_val_compare_and_swap(target, compare, set);\195*v = set; \196return (set == compare); \197}198199CK_PR_CAS_O(char, char)200CK_PR_CAS_O(int, int)201CK_PR_CAS_O(uint, unsigned int)202CK_PR_CAS_O(64, uint64_t)203CK_PR_CAS_O(32, uint32_t)204CK_PR_CAS_O(16, uint16_t)205CK_PR_CAS_O(8, uint8_t)206207#undef CK_PR_CAS_O208209/*210* Atomic fetch-and-add operations.211*/212#define CK_PR_FAA(S, M, T) \213CK_CC_INLINE static T \214ck_pr_faa_##S(M *target, T d) \215{ \216d = __sync_fetch_and_add((T *)target, d); \217return (d); \218}219220CK_PR_FAA(ptr, void, void *)221222#define CK_PR_FAA_S(S, T) CK_PR_FAA(S, T, T)223224CK_PR_FAA_S(char, char)225CK_PR_FAA_S(uint, unsigned int)226CK_PR_FAA_S(int, int)227CK_PR_FAA_S(64, uint64_t)228CK_PR_FAA_S(32, uint32_t)229CK_PR_FAA_S(16, uint16_t)230CK_PR_FAA_S(8, uint8_t)231232#undef CK_PR_FAA_S233#undef CK_PR_FAA234235/*236* Atomic store-only binary operations.237*/238#define CK_PR_BINARY(K, S, M, T) \239CK_CC_INLINE static void \240ck_pr_##K##_##S(M *target, T d) \241{ \242d = __sync_fetch_and_##K((T *)target, d); \243return; \244}245246#define CK_PR_BINARY_S(K, S, T) CK_PR_BINARY(K, S, T, T)247248#define CK_PR_GENERATE(K) \249CK_PR_BINARY(K, ptr, void, void *) \250CK_PR_BINARY_S(K, char, char) \251CK_PR_BINARY_S(K, int, int) \252CK_PR_BINARY_S(K, uint, unsigned int) \253CK_PR_BINARY_S(K, 64, uint64_t) \254CK_PR_BINARY_S(K, 32, uint32_t) \255CK_PR_BINARY_S(K, 16, uint16_t) \256CK_PR_BINARY_S(K, 8, uint8_t)257258CK_PR_GENERATE(add)259CK_PR_GENERATE(sub)260CK_PR_GENERATE(and)261CK_PR_GENERATE(or)262CK_PR_GENERATE(xor)263264#undef CK_PR_GENERATE265#undef CK_PR_BINARY_S266#undef CK_PR_BINARY267268#define CK_PR_UNARY(S, M, T) \269CK_CC_INLINE static void \270ck_pr_inc_##S(M *target) \271{ \272ck_pr_add_##S(target, (T)1); \273return; \274} \275CK_CC_INLINE static void \276ck_pr_dec_##S(M *target) \277{ \278ck_pr_sub_##S(target, (T)1); \279return; \280}281282#define CK_PR_UNARY_S(S, M) CK_PR_UNARY(S, M, M)283284CK_PR_UNARY(ptr, void, void *)285CK_PR_UNARY_S(char, char)286CK_PR_UNARY_S(int, int)287CK_PR_UNARY_S(uint, unsigned int)288CK_PR_UNARY_S(64, uint64_t)289CK_PR_UNARY_S(32, uint32_t)290CK_PR_UNARY_S(16, uint16_t)291CK_PR_UNARY_S(8, uint8_t)292293#undef CK_PR_UNARY_S294#undef CK_PR_UNARY295#endif /* !CK_F_PR */296#endif /* CK_PR_GCC_H */297298299