stdio-lock.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Thread package specific definitions of stream lock type. NPTL version.
  2. Copyright (C) 2000, 2001, 2002, 2003, 2007 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 _BITS_STDIO_LOCK_H
  16. #define _BITS_STDIO_LOCK_H 1
  17. #include <bits/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_initializer { LLL_LOCK_INITIALIZER, 0, NULL }
  23. #define _IO_lock_init(_name) \
  24. ((void) ((_name) = (_IO_lock_t) _IO_lock_initializer))
  25. #define _IO_lock_fini(_name) \
  26. ((void) 0)
  27. #define _IO_lock_lock(_name) \
  28. do { \
  29. void *__meself = THREAD_SELF; \
  30. if ((_name).owner != __meself) \
  31. { \
  32. lll_lock ((_name).lock, LLL_PRIVATE); \
  33. (_name).owner = __meself; \
  34. } \
  35. ++(_name).cnt; \
  36. } while (0)
  37. #define _IO_lock_trylock(_name) \
  38. ({ \
  39. int __result = 0; \
  40. void *__meself = THREAD_SELF; \
  41. if ((_name).owner != __meself) \
  42. { \
  43. if (lll_trylock ((_name).lock) == 0) \
  44. { \
  45. (_name).owner = __meself; \
  46. (_name).cnt = 1; \
  47. } \
  48. else \
  49. __result = EBUSY; \
  50. } \
  51. else \
  52. ++(_name).cnt; \
  53. __result; \
  54. })
  55. #define _IO_lock_unlock(_name) \
  56. do { \
  57. if (--(_name).cnt == 0) \
  58. { \
  59. (_name).owner = NULL; \
  60. lll_unlock ((_name).lock, LLL_PRIVATE); \
  61. } \
  62. } while (0)
  63. #define _IO_cleanup_region_start(_fct, _fp) \
  64. __libc_cleanup_region_start (((_fp)->_flags & _IO_USER_LOCK) == 0, _fct, _fp)
  65. #define _IO_cleanup_region_start_noarg(_fct) \
  66. __libc_cleanup_region_start (1, _fct, NULL)
  67. #define _IO_cleanup_region_end(_doit) \
  68. __libc_cleanup_region_end (_doit)
  69. #if defined _LIBC && !defined NOT_IN_libc
  70. # ifdef __EXCEPTIONS
  71. # define _IO_acquire_lock(_fp) \
  72. do { \
  73. _IO_FILE *_IO_acquire_lock_file \
  74. __attribute__((cleanup (_IO_acquire_lock_fct))) \
  75. = (_fp); \
  76. _IO_flockfile (_IO_acquire_lock_file);
  77. # define _IO_acquire_lock_clear_flags2(_fp) \
  78. do { \
  79. _IO_FILE *_IO_acquire_lock_file \
  80. __attribute__((cleanup (_IO_acquire_lock_clear_flags2_fct))) \
  81. = (_fp); \
  82. _IO_flockfile (_IO_acquire_lock_file);
  83. # else
  84. # define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
  85. # define _IO_acquire_lock_clear_flags2(_fp) _IO_acquire_lock (_fp)
  86. # endif
  87. # define _IO_release_lock(_fp) ; } while (0)
  88. #endif
  89. #endif /* bits/stdio-lock.h */