hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sysdeps/unix/sysv/linux/powerpc/force-elision.h

Packit 6c4009
/* force-elision.h: Automatic enabling of elision for mutexes
Packit 6c4009
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Automatically enable elision for existing user lock kinds.  */
Packit 6c4009
#define FORCE_ELISION(m, s)						\
Packit 5e5144
  if (__pthread_force_elision)						\
Packit 6c4009
    {									\
Packit 5e5144
      /* See concurrency notes regarding __kind in			\
Packit 5e5144
	 struct __pthread_mutex_s in					\
Packit 5e5144
	 sysdeps/nptl/bits/thread-shared-types.h.			\
Packit 5e5144
									\
Packit 5e5144
	 There are the following cases for the kind of a mutex		\
Packit 5e5144
	 (The mask PTHREAD_MUTEX_ELISION_FLAGS_NP covers the flags	\
Packit 5e5144
	 PTHREAD_MUTEX_ELISION_NP and PTHREAD_MUTEX_NO_ELISION_NP where	\
Packit 5e5144
	 only one of both flags can be set):				\
Packit 5e5144
	 - both flags are not set:					\
Packit 5e5144
	 This is the first lock operation for this mutex.  Enable	\
Packit 5e5144
	 elision as it is not enabled so far.				\
Packit 5e5144
	 Note: It can happen that multiple threads are calling e.g.	\
Packit 5e5144
	 pthread_mutex_lock at the same time as the first lock		\
Packit 5e5144
	 operation for this mutex.  Then elision is enabled for this	\
Packit 5e5144
	 mutex by multiple threads.  Storing with relaxed MO is enough	\
Packit 5e5144
	 as all threads will store the same new value for the kind of	\
Packit 5e5144
	 the mutex.  But we have to ensure that we always use the	\
Packit 5e5144
	 elision path regardless if this thread has enabled elision or	\
Packit 5e5144
	 another one.							\
Packit 5e5144
									\
Packit 5e5144
	 - PTHREAD_MUTEX_ELISION_NP flag is set:			\
Packit 5e5144
	 Elision was already enabled for this mutex by a previous lock	\
Packit 5e5144
	 operation.  See case above.  Just use the elision path.	\
Packit 5e5144
									\
Packit 5e5144
	 - PTHREAD_MUTEX_NO_ELISION_NP flag is set:			\
Packit 5e5144
	 Elision was explicitly disabled by pthread_mutexattr_settype.	\
Packit 5e5144
	 Do not use the elision path.					\
Packit 5e5144
	 Note: The flag PTHREAD_MUTEX_NO_ELISION_NP will never be	\
Packit 5e5144
	 changed after mutex initialization.  */			\
Packit 5e5144
      int mutex_kind = atomic_load_relaxed (&((m)->__data.__kind));	\
Packit 5e5144
      if ((mutex_kind & PTHREAD_MUTEX_ELISION_FLAGS_NP) == 0)		\
Packit 5e5144
	{								\
Packit 5e5144
	  mutex_kind |= PTHREAD_MUTEX_ELISION_NP;			\
Packit 5e5144
	  atomic_store_relaxed (&((m)->__data.__kind), mutex_kind);	\
Packit 5e5144
	}								\
Packit 5e5144
      if ((mutex_kind & PTHREAD_MUTEX_ELISION_NP) != 0)			\
Packit 5e5144
	{								\
Packit 5e5144
	  s;								\
Packit 5e5144
	}								\
Packit 6c4009
    }