Blame sysdeps/nptl/stdio-lock.h

Packit 6c4009
/* Thread package specific definitions of stream lock type.  NPTL version.
Packit 6c4009
   Copyright (C) 2000-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
#ifndef _STDIO_LOCK_H
Packit 6c4009
#define _STDIO_LOCK_H 1
Packit 6c4009
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <lowlevellock.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The locking here is very inexpensive, even for inlining.  */
Packit 6c4009
#define _IO_lock_inexpensive	1
Packit 6c4009
Packit 6c4009
typedef struct { int lock; int cnt; void *owner; } _IO_lock_t;
Packit 6c4009
#define _IO_lock_t_defined 1
Packit 6c4009
Packit 6c4009
#define _IO_lock_initializer { LLL_LOCK_INITIALIZER, 0, NULL }
Packit 6c4009
Packit 6c4009
#define _IO_lock_init(_name) \
Packit 6c4009
  ((void) ((_name) = (_IO_lock_t) _IO_lock_initializer))
Packit 6c4009
Packit 6c4009
#define _IO_lock_fini(_name) \
Packit 6c4009
  ((void) 0)
Packit 6c4009
Packit 6c4009
#define _IO_lock_lock(_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
Packit 6c4009
#define _IO_lock_trylock(_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
Packit 6c4009
#define _IO_lock_unlock(_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
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define _IO_cleanup_region_start(_fct, _fp) \
Packit 6c4009
  __libc_cleanup_region_start (((_fp)->_flags & _IO_USER_LOCK) == 0, _fct, _fp)
Packit 6c4009
#define _IO_cleanup_region_start_noarg(_fct) \
Packit 6c4009
  __libc_cleanup_region_start (1, _fct, NULL)
Packit 6c4009
#define _IO_cleanup_region_end(_doit) \
Packit 6c4009
  __libc_cleanup_region_end (_doit)
Packit 6c4009
Packit 6c4009
#if defined _LIBC && IS_IN (libc)
Packit 6c4009
Packit 6c4009
# ifdef __EXCEPTIONS
Packit 6c4009
#  define _IO_acquire_lock(_fp) \
Packit 6c4009
  do {									      \
Packit 6c4009
    FILE *_IO_acquire_lock_file						      \
Packit 6c4009
	__attribute__((cleanup (_IO_acquire_lock_fct)))			      \
Packit 6c4009
	= (_fp);							      \
Packit 6c4009
    _IO_flockfile (_IO_acquire_lock_file);
Packit 6c4009
#  define _IO_acquire_lock_clear_flags2(_fp) \
Packit 6c4009
  do {									      \
Packit 6c4009
    FILE *_IO_acquire_lock_file						      \
Packit 6c4009
	__attribute__((cleanup (_IO_acquire_lock_clear_flags2_fct)))	      \
Packit 6c4009
	= (_fp);							      \
Packit 6c4009
    _IO_flockfile (_IO_acquire_lock_file);
Packit 6c4009
# else
Packit 6c4009
#  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
Packit 6c4009
#  define _IO_acquire_lock_clear_flags2(_fp) _IO_acquire_lock (_fp)
Packit 6c4009
# endif
Packit 6c4009
# define _IO_release_lock(_fp) ; } while (0)
Packit 6c4009
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#endif /* stdio-lock.h */