116 lines
3.6 KiB
C
116 lines
3.6 KiB
C
|
|
/*
|
||
|
|
* Copyright (c) 1991, 1993
|
||
|
|
* The Regents of the University of California. All rights reserved.
|
||
|
|
*
|
||
|
|
* This code is derived from software contributed to Berkeley by
|
||
|
|
* Berkeley Software Design, Inc.
|
||
|
|
*
|
||
|
|
* Redistribution and use in source and binary forms, with or without
|
||
|
|
* modification, are permitted provided that the following conditions
|
||
|
|
* are met:
|
||
|
|
* 1. Redistributions of source code must retain the above copyright
|
||
|
|
* notice, this list of conditions and the following disclaimer.
|
||
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||
|
|
* notice, this list of conditions and the following disclaimer in the
|
||
|
|
* documentation and/or other materials provided with the distribution.
|
||
|
|
* 3. Neither the name of the University nor the names of its contributors
|
||
|
|
* may be used to endorse or promote products derived from this software
|
||
|
|
* without specific prior written permission.
|
||
|
|
*
|
||
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||
|
|
* SUCH DAMAGE.
|
||
|
|
*
|
||
|
|
* @(#)cdefs.h 8.8 (Berkeley) 1/9/95
|
||
|
|
*/
|
||
|
|
|
||
|
|
#ifndef _UTILS_H_
|
||
|
|
#define _UTILS_H_
|
||
|
|
|
||
|
|
#include <assert.h>
|
||
|
|
|
||
|
|
/*
|
||
|
|
* A regular assert (debug/diagnostic only).
|
||
|
|
*/
|
||
|
|
#if defined(DEBUG)
|
||
|
|
#define ASSERT assert
|
||
|
|
#else
|
||
|
|
#define ASSERT(x)
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Minimum, maximum and rounding macros.
|
||
|
|
*/
|
||
|
|
|
||
|
|
#ifndef MIN
|
||
|
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#ifndef MAX
|
||
|
|
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Branch prediction macros.
|
||
|
|
*/
|
||
|
|
#ifndef __predict_true
|
||
|
|
#define __predict_true(x) __builtin_expect((x) != 0, 1)
|
||
|
|
#define __predict_false(x) __builtin_expect((x) != 0, 0)
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Atomic operations and memory barriers. If C11 API is not available,
|
||
|
|
* then wrap the GCC builtin routines.
|
||
|
|
*
|
||
|
|
* Note: This atomic_compare_exchange_weak does not do the C11 thing of
|
||
|
|
* filling *(expected) with the actual value, because we don't need
|
||
|
|
* that here.
|
||
|
|
*/
|
||
|
|
#ifndef atomic_compare_exchange_weak
|
||
|
|
#define atomic_compare_exchange_weak(ptr, expected, desired) \
|
||
|
|
__sync_bool_compare_and_swap(ptr, *(expected), desired)
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#ifndef atomic_thread_fence
|
||
|
|
#define memory_order_relaxed __ATOMIC_RELAXED
|
||
|
|
#define memory_order_acquire __ATOMIC_ACQUIRE
|
||
|
|
#define memory_order_release __ATOMIC_RELEASE
|
||
|
|
#define memory_order_seq_cst __ATOMIC_SEQ_CST
|
||
|
|
#define atomic_thread_fence(m) __atomic_thread_fence(m)
|
||
|
|
#endif
|
||
|
|
#ifndef atomic_store_explicit
|
||
|
|
#define atomic_store_explicit __atomic_store_n
|
||
|
|
#endif
|
||
|
|
#ifndef atomic_load_explicit
|
||
|
|
#define atomic_load_explicit __atomic_load_n
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Exponential back-off for the spinning paths.
|
||
|
|
*/
|
||
|
|
#define SPINLOCK_BACKOFF_MIN 4
|
||
|
|
#define SPINLOCK_BACKOFF_MAX 128
|
||
|
|
#if defined(__x86_64__) || defined(__i386__)
|
||
|
|
#define SPINLOCK_BACKOFF_HOOK __asm volatile("pause" ::: "memory")
|
||
|
|
#else
|
||
|
|
#define SPINLOCK_BACKOFF_HOOK
|
||
|
|
#endif
|
||
|
|
#define SPINLOCK_BACKOFF(count) \
|
||
|
|
do { \
|
||
|
|
for (int __i = (count); __i != 0; __i--) { \
|
||
|
|
SPINLOCK_BACKOFF_HOOK; \
|
||
|
|
} \
|
||
|
|
if ((count) < SPINLOCK_BACKOFF_MAX) \
|
||
|
|
(count) += (count); \
|
||
|
|
} while (/* CONSTCOND */ 0);
|
||
|
|
|
||
|
|
#endif
|