fflush.c 4.9 KB

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