fflush.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. /* libc_hidden_proto(fflush_unlocked) */
  9. #ifdef __DO_UNLOCKED
  10. #ifdef __UCLIBC_MJN3_ONLY__
  11. #warning WISHLIST: Add option to test for undefined behavior of fflush.
  12. #endif /* __UCLIBC_MJN3_ONLY__ */
  13. /* Even if the stream is set to user-locking, we still need to lock
  14. * when all (lbf) writing streams are flushed. */
  15. #define __MY_STDIO_THREADLOCK(__stream) \
  16. __UCLIBC_MUTEX_CONDITIONAL_LOCK((__stream)->__lock, \
  17. (_stdio_user_locking != 2))
  18. #define __MY_STDIO_THREADUNLOCK(__stream) \
  19. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK((__stream)->__lock, \
  20. (_stdio_user_locking != 2))
  21. #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
  22. void attribute_hidden _stdio_openlist_dec_use(void)
  23. {
  24. __STDIO_THREADLOCK_OPENLIST_DEL;
  25. if ((_stdio_openlist_use_count == 1) && (_stdio_openlist_del_count > 0)) {
  26. FILE *p = NULL;
  27. FILE *n;
  28. FILE *stream;
  29. #ifdef __UCLIBC_MJN3_ONLY__
  30. #warning REMINDER: As an optimization, we could unlock after we move past the head.
  31. #endif
  32. /* Grab the openlist add lock since we might change the head of the list. */
  33. __STDIO_THREADLOCK_OPENLIST_ADD;
  34. for (stream = _stdio_openlist; stream; stream = n) {
  35. n = stream->__nextopen;
  36. #ifdef __UCLIBC_MJN3_ONLY__
  37. #warning REMINDER: fix for nonatomic
  38. #endif
  39. if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY|__FLAG_FAILED_FREOPEN))
  40. == (__FLAG_READONLY|__FLAG_WRITEONLY)
  41. ) { /* The file was closed and should be removed from the list. */
  42. if (!p) {
  43. _stdio_openlist = n;
  44. } else {
  45. p->__nextopen = n;
  46. }
  47. __STDIO_STREAM_FREE_FILE(stream);
  48. } else {
  49. p = stream;
  50. }
  51. }
  52. __STDIO_THREADUNLOCK_OPENLIST_ADD;
  53. _stdio_openlist_del_count = 0; /* Should be clean now. */
  54. }
  55. --_stdio_openlist_use_count;
  56. __STDIO_THREADUNLOCK_OPENLIST_DEL;
  57. }
  58. #endif
  59. int fflush_unlocked(register FILE *stream)
  60. {
  61. #ifdef __STDIO_BUFFERS
  62. int retval = 0;
  63. #ifdef __UCLIBC_MJN3_ONLY__
  64. #warning REMINDER: should probably define a modeflags type
  65. #endif
  66. unsigned short bufmask = __FLAG_LBF;
  67. #ifndef NDEBUG
  68. if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
  69. __STDIO_STREAM_VALIDATE(stream); /* debugging only */
  70. }
  71. #endif
  72. if (stream == (FILE *) &_stdio_openlist) { /* Flush all lbf streams. */
  73. stream = NULL;
  74. bufmask = 0;
  75. }
  76. if (!stream) { /* Flush all (lbf) writing streams. */
  77. __STDIO_OPENLIST_INC_USE;
  78. __STDIO_THREADLOCK_OPENLIST_ADD;
  79. stream = _stdio_openlist;
  80. __STDIO_THREADUNLOCK_OPENLIST_ADD;
  81. while(stream) {
  82. /* We only care about currently writing streams and do not want to
  83. * block trying to obtain mutexes on non-writing streams. */
  84. #warning fix for nonatomic
  85. #warning unnecessary check if no threads
  86. if (__STDIO_STREAM_IS_WRITING(stream)) { /* ONLY IF ATOMIC!!! */
  87. __MY_STDIO_THREADLOCK(stream);
  88. /* Need to check again once we have the lock. */
  89. if (!(((stream->__modeflags | bufmask)
  90. ^ (__FLAG_WRITING|__FLAG_LBF)
  91. ) & (__FLAG_WRITING|__MASK_BUFMODE))
  92. ) {
  93. if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
  94. __STDIO_STREAM_DISABLE_PUTC(stream);
  95. __STDIO_STREAM_CLEAR_WRITING(stream);
  96. } else {
  97. retval = EOF;
  98. }
  99. }
  100. __MY_STDIO_THREADUNLOCK(stream);
  101. }
  102. stream = stream->__nextopen;
  103. }
  104. __STDIO_OPENLIST_DEC_USE;
  105. } else if (__STDIO_STREAM_IS_WRITING(stream)) {
  106. if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
  107. __STDIO_STREAM_DISABLE_PUTC(stream);
  108. __STDIO_STREAM_CLEAR_WRITING(stream);
  109. } else {
  110. retval = EOF;
  111. }
  112. }
  113. #if 0
  114. else if (stream->__modeflags & (__MASK_READING|__FLAG_READONLY)) {
  115. /* ANSI/ISO says behavior in this case is undefined but also says you
  116. * shouldn't flush a stream you were reading from. As usual, glibc
  117. * caters to broken programs and simply ignores this. */
  118. __UNDEFINED_OR_NONPORTABLE;
  119. __STDIO_STREAM_SET_ERROR(stream);
  120. __set_errno(EBADF);
  121. retval = EOF;
  122. }
  123. #endif
  124. #ifndef NDEBUG
  125. if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
  126. __STDIO_STREAM_VALIDATE(stream); /* debugging only */
  127. }
  128. #endif
  129. return retval;
  130. #else /* __STDIO_BUFFERS --------------------------------------- */
  131. #ifndef NDEBUG
  132. if ((stream != NULL)
  133. #ifdef __STDIO_HAS_OPENLIST
  134. && (stream != (FILE *) &_stdio_openlist)
  135. #endif
  136. ) {
  137. __STDIO_STREAM_VALIDATE(stream); /* debugging only */
  138. }
  139. #endif
  140. #if 0
  141. if (stream && (stream->__modeflags & (__MASK_READING|__FLAG_READONLY))) {
  142. /* ANSI/ISO says behavior in this case is undefined but also says you
  143. * shouldn't flush a stream you were reading from. As usual, glibc
  144. * caters to broken programs and simply ignores this. */
  145. __UNDEFINED_OR_NONPORTABLE;
  146. __STDIO_STREAM_SET_ERROR(stream);
  147. __set_errno(EBADF);
  148. return EOF;
  149. }
  150. #endif
  151. return 0;
  152. #endif /* __STDIO_BUFFERS */
  153. }
  154. libc_hidden_def(fflush_unlocked)
  155. #ifndef __UCLIBC_HAS_THREADS__
  156. /* libc_hidden_proto(fflush) */
  157. strong_alias(fflush_unlocked,fflush)
  158. libc_hidden_def(fflush)
  159. #endif
  160. #elif defined __UCLIBC_HAS_THREADS__
  161. /* libc_hidden_proto(fflush) */
  162. int fflush(register FILE *stream)
  163. {
  164. int retval;
  165. __STDIO_AUTO_THREADLOCK_VAR;
  166. if (stream
  167. #ifdef __STDIO_HAS_OPENLIST
  168. && (stream != (FILE *) &_stdio_openlist)
  169. #endif
  170. ) {
  171. __STDIO_AUTO_THREADLOCK(stream);
  172. retval = fflush_unlocked(stream);
  173. __STDIO_AUTO_THREADUNLOCK(stream);
  174. } else {
  175. retval = fflush_unlocked(stream);
  176. }
  177. return retval;
  178. }
  179. libc_hidden_def(fflush)
  180. #endif