Path: blob/main/sys/cddl/compat/opensolaris/kern/opensolaris_atomic.c
48383 views
/*-1* Copyright (c) 2007 Pawel Jakub Dawidek <[email protected]>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 AUTHORS 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 AUTHORS 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#include <sys/param.h>27#include <sys/lock.h>28#include <sys/mutex.h>29#include <sys/atomic.h>3031#if !defined(__LP64__) && !defined(__mips_n32) && \32!defined(ARM_HAVE_ATOMIC64) && !defined(I386_HAVE_ATOMIC64) && \33!defined(HAS_EMULATED_ATOMIC64)3435#ifdef _KERNEL36#include <sys/kernel.h>3738struct mtx atomic_mtx;39MTX_SYSINIT(atomic, &atomic_mtx, "atomic", MTX_DEF);40#else41#include <pthread.h>4243#define mtx_lock(lock) pthread_mutex_lock(lock)44#define mtx_unlock(lock) pthread_mutex_unlock(lock)4546static pthread_mutex_t atomic_mtx;4748static __attribute__((constructor)) void49atomic_init(void)50{51pthread_mutex_init(&atomic_mtx, NULL);52}53#endif5455void56atomic_add_64(volatile uint64_t *target, int64_t delta)57{5859mtx_lock(&atomic_mtx);60*target += delta;61mtx_unlock(&atomic_mtx);62}6364void65atomic_dec_64(volatile uint64_t *target)66{6768mtx_lock(&atomic_mtx);69*target -= 1;70mtx_unlock(&atomic_mtx);71}7273uint64_t74atomic_swap_64(volatile uint64_t *a, uint64_t value)75{76uint64_t ret;7778mtx_lock(&atomic_mtx);79ret = *a;80*a = value;81mtx_unlock(&atomic_mtx);82return (ret);83}8485uint64_t86atomic_load_64(volatile uint64_t *a)87{88uint64_t ret;8990mtx_lock(&atomic_mtx);91ret = *a;92mtx_unlock(&atomic_mtx);93return (ret);94}9596uint64_t97atomic_add_64_nv(volatile uint64_t *target, int64_t delta)98{99uint64_t newval;100101mtx_lock(&atomic_mtx);102newval = (*target += delta);103mtx_unlock(&atomic_mtx);104return (newval);105}106107uint64_t108atomic_cas_64(volatile uint64_t *target, uint64_t cmp, uint64_t newval)109{110uint64_t oldval;111112mtx_lock(&atomic_mtx);113oldval = *target;114if (oldval == cmp)115*target = newval;116mtx_unlock(&atomic_mtx);117return (oldval);118}119#endif120121122