1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00
samba-mirror/lib/replace/system/threads.h
Stefan Metzmacher 5fa5764f30 replace: add checks for atomic_thread_fence(memory_order_seq_cst) and add possible fallbacks
This implements a full memory barrier.
On ubuntu amd64 with results in an 'mfence' instruction.

This is required to syncronization between threads, where
there's typically only one write of a memory that should be
synced between all threads with the barrier.

Much more details can be found here:
https://gcc.gnu.org/onlinedocs/gcc-7.3.0/gcc/_005f_005fatomic-Builtins.html#g_t_005f_005fatomic-Builtins
https://gcc.gnu.org/onlinedocs/gcc-7.3.0/gcc/_005f_005fsync-Builtins.html#g_t_005f_005fsync-Builtins

The main one we use seems to be in C11 via stdatomic.h,
the oldest fallback is __sync_synchronize(), which is available
since 2005 in gcc.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2018-07-24 17:38:26 +02:00

73 lines
2.4 KiB
C

#ifndef _system_threads_h
#define _system_threads_h
/*
Unix SMB/CIFS implementation.
macros to go along with the lib/replace/ portability layer code
Copyright (C) Volker Lendecke 2012
** NOTE! The following LGPL license applies to the replace
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#if defined(HAVE_PTHREAD_MUTEXATTR_SETROBUST_NP) && \
!defined(HAVE_PTHREAD_MUTEXATTR_SETROBUST)
#define pthread_mutexattr_setrobust pthread_mutexattr_setrobust_np
#endif
#if defined(HAVE_DECL_PTHREAD_MUTEX_ROBUST_NP) && \
!defined(HAVE_DECL_PTHREAD_MUTEX_ROBUST)
#define PTHREAD_MUTEX_ROBUST PTHREAD_MUTEX_ROBUST_NP
#endif
#if defined(HAVE_PTHREAD_MUTEX_CONSISTENT_NP) && \
!defined(HAVE_PTHREAD_MUTEX_CONSISTENT)
#define pthread_mutex_consistent pthread_mutex_consistent_np
#endif
#ifdef HAVE_STDATOMIC_H
#include <stdatomic.h>
#endif
#ifndef HAVE_ATOMIC_THREAD_FENCE
#ifdef HAVE___ATOMIC_THREAD_FENCE
#define atomic_thread_fence(__ignore_order) __atomic_thread_fence(__ATOMIC_SEQ_CST)
#define HAVE_ATOMIC_THREAD_FENCE 1
#endif /* HAVE___ATOMIC_THREAD_FENCE */
#endif /* not HAVE_ATOMIC_THREAD_FENCE */
#ifndef HAVE_ATOMIC_THREAD_FENCE
#ifdef HAVE___SYNC_SYNCHRONIZE
#define atomic_thread_fence(__ignore_order) __sync_synchronize()
#define HAVE_ATOMIC_THREAD_FENCE 1
#endif /* HAVE___SYNC_SYNCHRONIZE */
#endif /* not HAVE_ATOMIC_THREAD_FENCE */
#ifndef HAVE_ATOMIC_THREAD_FENCE
#ifdef HAVE_ATOMIC_THREAD_FENCE_SUPPORT
#error mismatch_error_between_configure_test_and_header
#endif
/* make sure the build fails if someone uses it without checking the define */
#define atomic_thread_fence(__order) \
__function__atomic_thread_fence_not_available_on_this_platform__()
#endif /* not HAVE_ATOMIC_THREAD_FENCE */
#endif