Blame sysdeps/nptl/libc-lock.h

Packit 6c4009
/* libc-internal interface for mutex locks.  NPTL version.
Packit 6c4009
   Copyright (C) 1996-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 License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   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; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _LIBC_LOCK_H
Packit 6c4009
#define _LIBC_LOCK_H 1
Packit 6c4009
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#define __need_NULL
Packit 6c4009
#include <stddef.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Mutex type.  */
Packit 6c4009
#if defined _LIBC || defined _IO_MTSAFE_IO
Packit 6c4009
# if (!IS_IN (libc) && !IS_IN (libpthread)) || !defined _LIBC
Packit 6c4009
typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
Packit 6c4009
# else
Packit 6c4009
typedef struct { int lock; int cnt; void *owner; } __libc_lock_recursive_t;
Packit 6c4009
# endif
Packit 6c4009
#else
Packit 6c4009
typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Define a lock variable NAME with storage class CLASS.  The lock must be
Packit 6c4009
   initialized with __libc_lock_init before it can be used (or define it
Packit 6c4009
   with __libc_lock_define_initialized, below).  Use `extern' for CLASS to
Packit 6c4009
   declare a lock defined in another module.  In public structure
Packit 6c4009
   definitions you must use a pointer to the lock structure (i.e., NAME
Packit 6c4009
   begins with a `*'), because its storage size will not be known outside
Packit 6c4009
   of libc.  */
Packit 6c4009
#define __libc_lock_define_recursive(CLASS,NAME) \
Packit 6c4009
  CLASS __libc_lock_recursive_t NAME;
Packit 6c4009
Packit 6c4009
/* Define an initialized recursive lock variable NAME with storage
Packit 6c4009
   class CLASS.  */
Packit 6c4009
#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
Packit 6c4009
# define __libc_lock_define_initialized_recursive(CLASS, NAME) \
Packit 6c4009
  CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
Packit 6c4009
# define _LIBC_LOCK_RECURSIVE_INITIALIZER \
Packit 6c4009
  { LLL_LOCK_INITIALIZER, 0, NULL }
Packit 6c4009
#else
Packit 6c4009
# define __libc_lock_define_initialized_recursive(CLASS,NAME) \
Packit 6c4009
  CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
Packit 6c4009
# define _LIBC_LOCK_RECURSIVE_INITIALIZER \
Packit 6c4009
  {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Initialize a recursive mutex.  */
Packit 6c4009
#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
Packit 6c4009
# define __libc_lock_init_recursive(NAME) \
Packit 6c4009
  ((void) ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER))
Packit 6c4009
#else
Packit 6c4009
# define __libc_lock_init_recursive(NAME) \
Packit 6c4009
  do {									      \
Packit 6c4009
    if (__pthread_mutex_init != NULL)					      \
Packit 6c4009
      {									      \
Packit 6c4009
	pthread_mutexattr_t __attr;					      \
Packit 6c4009
	__pthread_mutexattr_init (&__attr);				      \
Packit 6c4009
	__pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP);    \
Packit 6c4009
	__pthread_mutex_init (&(NAME).mutex, &__attr);			      \
Packit 6c4009
	__pthread_mutexattr_destroy (&__attr);				      \
Packit 6c4009
      }									      \
Packit 6c4009
  } while (0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Finalize recursive named lock.  */
Packit 6c4009
#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
Packit 6c4009
# define __libc_lock_fini_recursive(NAME) ((void) 0)
Packit 6c4009
#else
Packit 6c4009
# define __libc_lock_fini_recursive(NAME) \
Packit 6c4009
  __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Lock the recursive named lock variable.  */
Packit 6c4009
#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
Packit 6c4009
# define __libc_lock_lock_recursive(NAME) \
Packit 6c4009
  do {									      \
Packit 6c4009
    void *self = THREAD_SELF;						      \
Packit 6c4009
    if ((NAME).owner != self)						      \
Packit 6c4009
      {									      \
Packit 6c4009
	lll_lock ((NAME).lock, LLL_PRIVATE);				      \
Packit 6c4009
	(NAME).owner = self;						      \
Packit 6c4009
      }									      \
Packit 6c4009
    ++(NAME).cnt;							      \
Packit 6c4009
  } while (0)
Packit 6c4009
#else
Packit 6c4009
# define __libc_lock_lock_recursive(NAME) \
Packit 6c4009
  __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Try to lock the recursive named lock variable.  */
Packit 6c4009
#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
Packit 6c4009
# define __libc_lock_trylock_recursive(NAME) \
Packit 6c4009
  ({									      \
Packit 6c4009
    int result = 0;							      \
Packit 6c4009
    void *self = THREAD_SELF;						      \
Packit 6c4009
    if ((NAME).owner != self)						      \
Packit 6c4009
      {									      \
Packit 6c4009
	if (lll_trylock ((NAME).lock) == 0)				      \
Packit 6c4009
	  {								      \
Packit 6c4009
	    (NAME).owner = self;					      \
Packit 6c4009
	    (NAME).cnt = 1;						      \
Packit 6c4009
	  }								      \
Packit 6c4009
	else								      \
Packit 6c4009
	  result = EBUSY;						      \
Packit 6c4009
      }									      \
Packit 6c4009
    else								      \
Packit 6c4009
      ++(NAME).cnt;							      \
Packit 6c4009
    result;								      \
Packit 6c4009
  })
Packit 6c4009
#else
Packit 6c4009
# define __libc_lock_trylock_recursive(NAME) \
Packit 6c4009
  __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Unlock the recursive named lock variable.  */
Packit 6c4009
#if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
Packit 6c4009
/* We do no error checking here.  */
Packit 6c4009
# define __libc_lock_unlock_recursive(NAME) \
Packit 6c4009
  do {									      \
Packit 6c4009
    if (--(NAME).cnt == 0)						      \
Packit 6c4009
      {									      \
Packit 6c4009
	(NAME).owner = NULL;						      \
Packit 6c4009
	lll_unlock ((NAME).lock, LLL_PRIVATE);				      \
Packit 6c4009
      }									      \
Packit 6c4009
  } while (0)
Packit 6c4009
#else
Packit 6c4009
# define __libc_lock_unlock_recursive(NAME) \
Packit 6c4009
  __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Note that for I/O cleanup handling we are using the old-style
Packit 6c4009
   cancel handling.  It does not have to be integrated with C++ since
Packit 6c4009
   no C++ code is called in the middle.  The old-style handling is
Packit 6c4009
   faster and the support is not going away.  */
Packit 6c4009
extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
Packit 6c4009
					 void (*routine) (void *), void *arg);
Packit 6c4009
extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
Packit 6c4009
					  int execute);
Packit 6c4009
Packit 6c4009
/* Start critical region with cleanup.  */
Packit 6c4009
#define __libc_cleanup_region_start(DOIT, FCT, ARG) \
Packit 6c4009
  { struct _pthread_cleanup_buffer _buffer;				      \
Packit 6c4009
    int _avail;								      \
Packit 6c4009
    if (DOIT) {								      \
Packit 6c4009
      _avail = PTFAVAIL (_pthread_cleanup_push_defer);			      \
Packit 6c4009
      if (_avail) {							      \
Packit 6c4009
	__libc_ptf_call_always (_pthread_cleanup_push_defer, (&_buffer, FCT,  \
Packit 6c4009
							      ARG));	      \
Packit 6c4009
      } else {								      \
Packit 6c4009
	_buffer.__routine = (FCT);					      \
Packit 6c4009
	_buffer.__arg = (ARG);						      \
Packit 6c4009
      }									      \
Packit 6c4009
    } else {								      \
Packit 6c4009
      _avail = 0;							      \
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
/* End critical region with cleanup.  */
Packit 6c4009
#define __libc_cleanup_region_end(DOIT) \
Packit 6c4009
    if (_avail) {							      \
Packit 6c4009
      __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\
Packit 6c4009
    } else if (DOIT)							      \
Packit 6c4009
      _buffer.__routine (_buffer.__arg);				      \
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Hide the definitions which are only supposed to be used inside libc in
Packit 6c4009
   a separate file.  This file is not present in the installation!  */
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
# include "libc-lockP.h"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#endif	/* libc-lock.h */