stdio-lock.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _BITS_STDIO_LOCK_H
  17. #define _BITS_STDIO_LOCK_H 1
  18. #include <bits/libc-lock.h>
  19. #include <lowlevellock.h>
  20. /* The locking here is very inexpensive, even for inlining. */
  21. #define _IO_lock_inexpensive 1
  22. typedef struct { int lock; int cnt; void *owner; } _IO_lock_t;
  23. #define _IO_lock_initializer { LLL_LOCK_INITIALIZER, 0, NULL }
  24. #define _IO_lock_init(_name) \
  25. ((_name) = (_IO_lock_t) _IO_lock_initializer , 0)
  26. #define _IO_lock_fini(_name) \
  27. ((void) 0)
  28. #define _IO_lock_lock(_name) \
  29. do { \
  30. void *__meself = THREAD_SELF; \
  31. if ((_name).owner != __meself) \
  32. { \
  33. lll_lock ((_name).lock, LLL_PRIVATE); \
  34. (_name).owner = __meself; \
  35. } \
  36. ++(_name).cnt; \
  37. } while (0)
  38. #define _IO_lock_trylock(_name) \
  39. ({ \
  40. int __result = 0; \
  41. void *__meself = THREAD_SELF; \
  42. if ((_name).owner != __meself) \
  43. { \
  44. if (lll_trylock ((_name).lock) == 0) \
  45. { \
  46. (_name).owner = __meself; \
  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 && !defined NOT_IN_libc
  71. # ifdef __EXCEPTIONS
  72. # define _IO_acquire_lock(_fp) \
  73. do { \
  74. _IO_FILE *_IO_acquire_lock_file \
  75. __attribute__((cleanup (_IO_acquire_lock_fct))) \
  76. = (_fp); \
  77. _IO_flockfile (_IO_acquire_lock_file);
  78. # define _IO_acquire_lock_clear_flags2(_fp) \
  79. do { \
  80. _IO_FILE *_IO_acquire_lock_file \
  81. __attribute__((cleanup (_IO_acquire_lock_clear_flags2_fct))) \
  82. = (_fp); \
  83. _IO_flockfile (_IO_acquire_lock_file);
  84. # else
  85. # define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
  86. # define _IO_acquire_lock_clear_flags2(_fp) _IO_acquire_lock (_fp)
  87. # endif
  88. # define _IO_release_lock(_fp) ; } while (0)
  89. #endif
  90. #endif /* bits/stdio-lock.h */