stdio-lock.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Thread package specific definitions of stream lock type. NPTL version.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _STDIO_LOCK_H
  16. #define _STDIO_LOCK_H 1
  17. #include <libc-lock.h>
  18. #include <lowlevellock.h>
  19. /* The locking here is very inexpensive, even for inlining. */
  20. #define _IO_lock_inexpensive 1
  21. typedef struct { int lock; int cnt; void *owner; } _IO_lock_t;
  22. #define _IO_lock_t_defined 1
  23. #define _IO_lock_initializer { LLL_LOCK_INITIALIZER, 0, NULL }
  24. #define _IO_lock_init(_name) \
  25. ((void) ((_name) = (_IO_lock_t) _IO_lock_initializer))
  26. #define _IO_lock_fini(_name) \
  27. ((void) 0)
  28. #define _IO_lock_lock(_name) \
  29. do { \
  30. void *__self = THREAD_SELF; \
  31. if ((_name).owner != __self) \
  32. { \
  33. lll_lock ((_name).lock, LLL_PRIVATE); \
  34. (_name).owner = __self; \
  35. } \
  36. ++(_name).cnt; \
  37. } while (0)
  38. #define _IO_lock_trylock(_name) \
  39. ({ \
  40. int __result = 0; \
  41. void *__self = THREAD_SELF; \
  42. if ((_name).owner != __self) \
  43. { \
  44. if (lll_trylock ((_name).lock) == 0) \
  45. { \
  46. (_name).owner = __self; \
  47. (_name).cnt = 1; \
  48. } \
  49. else \
  50. __result = EBUSY; \
  51. } \
  52. else \
  53. ++(_name).cnt; \
  54. __result; \
  55. })
  56. #define _IO_lock_unlock(_name) \
  57. do { \
  58. if (--(_name).cnt == 0) \
  59. { \
  60. (_name).owner = NULL; \
  61. lll_unlock ((_name).lock, LLL_PRIVATE); \
  62. } \
  63. } while (0)
  64. #define _IO_cleanup_region_start(_fct, _fp) \
  65. __libc_cleanup_region_start (((_fp)->_flags & _IO_USER_LOCK) == 0, _fct, _fp)
  66. #define _IO_cleanup_region_start_noarg(_fct) \
  67. __libc_cleanup_region_start (1, _fct, NULL)
  68. #define _IO_cleanup_region_end(_doit) \
  69. __libc_cleanup_region_end (_doit)
  70. #if defined _LIBC && IS_IN (libc)
  71. # ifdef __EXCEPTIONS
  72. # define _IO_acquire_lock(_fp) \
  73. do { \
  74. FILE *_IO_acquire_lock_file \
  75. __attribute__((cleanup (_IO_acquire_lock_fct))) \
  76. = (_fp); \
  77. _IO_flockfile (_IO_acquire_lock_file);
  78. # else
  79. # define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
  80. # endif
  81. # define _IO_release_lock(_fp) ; } while (0)
  82. #endif
  83. #endif /* stdio-lock.h */