Ticket #256: stream_panic.diff
File stream_panic.diff, 17.6 KB (added by , 13 years ago) |
---|
-
src/emx/include/emx/io.h
302 302 #define STREAM_LOCK_NOWAIT(f) stream_trylock(f) 303 303 #define STREAM_UNLOCKED(f) ((f)->__uVersion == _FILE_STDIO_VERSION && _fmutex_available(&(f)->__u.__fsem)) 304 304 305 typedef struct __STREAM_XCPTREGREC 306 { 307 void *apv[2]; // EXCEPTIONREGISTRATIONRECORD 308 struct __sFILE *file; 309 } __STREAM_XCPTREGREC, *__STREAM_PXCPTREGREC; 310 311 void __stream_xcpt_init(__STREAM_PXCPTREGREC pRegRec); 312 void __stream_xcpt_done(__STREAM_PXCPTREGREC pRegRec); 313 314 #define STREAM_XCPT_BEGIN { __STREAM_XCPTREGREC __stream_xcptregec; __stream_xcpt_init(&__stream_xcptregec); 315 #define STREAM_XCPT_END __stream_xcpt_done(&__stream_xcptregec); } 316 317 #define STREAM_XCPT_LOCK(f) do { __stream_xcptregec.file = f; stream_lock(f); } while (0) 318 #define STREAM_XCPT_UNLOCK(f) do { stream_unlock(f); __stream_xcptregec.file = NULL; } while (0) 319 #define STREAM_XCPT_LOCK_NOWAIT(f) (stream_trylock(f) ? (__stream_xcptregec.file = f, 1) : 0) 320 #define STREAM_XCPT_UNLOCK_END(f) do { stream_unlock(f); __stream_xcptregec.file = NULL; __stream_xcpt_done(&__stream_xcptregec); } while (0) 321 305 322 #endif /* __FILE_FSEM_DECLARED */ 306 323 307 324 struct __libc_FileHandle; -
src/emx/src/lib/app/stdio.c
1 1 /* stdio.c (emx+gcc) -- Copyright (c) 1990-1996 by Eberhard Mattes 2 2 -- Copyright (c) 2003-2004 by knut st. osmundsen */ 3 3 4 #define INCL_DOSEXCEPTIONS 5 #include <os2emx.h> 6 4 7 #include "libc-alias.h" 5 8 #include <sys/builtin.h> /* For <sys/fmutex.h> */ 6 9 #include <sys/fmutex.h> … … 157 160 _CRT_EXIT1(_exit_streams) 158 161 159 162 163 #if defined (__FILE_FSEM_DECLARED) 160 164 161 165 /** 162 166 * Fatal stream error. … … 180 184 return 0; 181 185 } 182 186 187 /** 188 * Exception handler for STREAM_XCPT_LOCK()/STREAM_XCPT_UNLOCK(). 189 * 190 * Releases the stream lock on thread termination. 191 */ 192 ULONG __stream_exception(EXCEPTIONREPORTRECORD *report, 193 EXCEPTIONREGISTRATIONRECORD *registration, 194 CONTEXTRECORD *context, 195 void *dummy) 196 { 197 __STREAM_PXCPTREGREC pRegRec = (__STREAM_PXCPTREGREC)registration; 198 199 if (report->fHandlerFlags & EH_UNWINDING || 200 report->ExceptionNum == XCPT_ASYNC_PROCESS_TERMINATE || 201 report->ExceptionNum == XCPT_PROCESS_TERMINATE) 202 { 203 if (pRegRec->file) 204 { 205 // make sure we won't be called again 206 DosUnsetExceptionHandler(registration); 207 // unlock the stream mutex 208 STREAM_UNLOCK(pRegRec->file); 209 } 210 } 211 return XCPT_CONTINUE_SEARCH; 212 } 213 214 void __stream_xcpt_init(__STREAM_PXCPTREGREC pRegRec) 215 { 216 ((PEXCEPTIONREGISTRATIONRECORD)pRegRec)->prev_structure = 0; 217 ((PEXCEPTIONREGISTRATIONRECORD)pRegRec)->ExceptionHandler = __stream_exception; 218 pRegRec->file = 0; 219 DosSetExceptionHandler((PEXCEPTIONREGISTRATIONRECORD)pRegRec); 220 } 221 222 void __stream_xcpt_done(__STREAM_PXCPTREGREC pRegRec) 223 { 224 DosUnsetExceptionHandler((PEXCEPTIONREGISTRATIONRECORD)pRegRec); 225 } 226 227 #endif /* __FILE_FSEM_DECLARED */ -
src/emx/src/lib/io/fflush.c
68 68 int result, n; 69 69 struct streamvec *sv; 70 70 71 STREAM_XCPT_BEGIN 72 71 73 result = 0; 72 74 if (stream == NULL) 73 75 { … … 79 81 return result; 80 82 } 81 83 82 STREAM_ LOCK (stream);84 STREAM_XCPT_LOCK (stream); 83 85 result = fflush_unlocked (stream); 84 STREAM_UNLOCK (stream); 86 STREAM_XCPT_UNLOCK (stream); 87 88 STREAM_XCPT_END 89 85 90 return result; 86 91 } -
src/emx/src/lib/io/fgetc.c
15 15 16 16 int _STD(fgetc)(FILE *stream) 17 17 { 18 STREAM_LOCK(stream); 19 int rc = _getc_inline(stream); 20 STREAM_UNLOCK(stream); 18 int rc; 19 20 STREAM_XCPT_BEGIN 21 22 STREAM_XCPT_LOCK(stream); 23 rc = _getc_inline(stream); 24 STREAM_XCPT_UNLOCK(stream); 25 26 STREAM_XCPT_END 27 21 28 return rc; 22 29 } 23 30 -
src/emx/src/lib/io/fgets.c
10 10 11 11 char *_STD(fgets)(char *buffer, int n, FILE *stream) 12 12 { 13 STREAM_LOCK(stream); 14 char *psz = fgets_unlocked(buffer, n, stream); 15 STREAM_UNLOCK(stream); 13 char *psz; 14 15 STREAM_XCPT_BEGIN 16 17 STREAM_XCPT_LOCK(stream); 18 psz = fgets_unlocked(buffer, n, stream); 19 STREAM_XCPT_UNLOCK(stream); 20 21 STREAM_XCPT_END 22 16 23 return psz; 17 24 } 18 25 -
src/emx/src/lib/io/flushall.c
12 12 int i, n; 13 13 struct streamvec *sv; 14 14 15 STREAM_XCPT_BEGIN 16 15 17 /* Ignore locked streams to avoid deadlock on process 16 18 termination. */ 17 19 18 20 n = 0; 19 21 for (sv = _streamvec_head; sv != NULL; sv = sv->pNext) 20 22 for (i = 0; i < sv->cFiles; ++i) 21 if ((sv->aFiles[i]._flags & _IOOPEN) && STREAM_ LOCK_NOWAIT (&sv->aFiles[i]))23 if ((sv->aFiles[i]._flags & _IOOPEN) && STREAM_XCPT_LOCK_NOWAIT (&sv->aFiles[i])) 22 24 { 23 25 ++n; 24 26 fflush_unlocked (&sv->aFiles[i]); 25 STREAM_ UNLOCK (&sv->aFiles[i]);27 STREAM_XCPT_UNLOCK (&sv->aFiles[i]); 26 28 } 29 30 STREAM_XCPT_END 31 27 32 return n; 28 33 } -
src/emx/src/lib/io/fprintf.c
14 14 int result; 15 15 void *tb; 16 16 17 STREAM_XCPT_BEGIN 18 17 19 va_start (arg_ptr, format); 18 STREAM_ LOCK (stream);20 STREAM_XCPT_LOCK (stream); 19 21 if (nbuf (stream)) 20 22 _fbuf (stream); 21 23 _tmpbuf (stream, tb); 22 24 result = _output (stream, format, arg_ptr); 23 25 if (_endbuf (stream) != 0) 24 26 result = -1; 25 STREAM_ UNLOCK (stream);27 STREAM_XCPT_UNLOCK (stream); 26 28 va_end (arg_ptr); 29 30 STREAM_XCPT_END 31 27 32 return result; 28 33 } -
src/emx/src/lib/io/fputc.c
16 16 { 17 17 int r; 18 18 19 STREAM_LOCK(stream); 19 STREAM_XCPT_BEGIN 20 21 STREAM_XCPT_LOCK(stream); 20 22 r = _putc_inline(c, stream); 21 STREAM_UNLOCK(stream); 23 STREAM_XCPT_UNLOCK(stream); 24 25 STREAM_XCPT_END 26 22 27 return r; 23 28 } 24 29 … … 31 36 { 32 37 int r; 33 38 34 STREAM_LOCK(stream); 39 STREAM_XCPT_BEGIN 40 41 STREAM_XCPT_LOCK(stream); 35 42 r = _putc_inline(c, stream); 36 STREAM_UNLOCK(stream); 43 STREAM_XCPT_UNLOCK(stream); 44 45 STREAM_XCPT_END 46 37 47 return r; 38 48 } -
src/emx/src/lib/io/fread.c
13 13 14 14 size_t _STD(fread)(void *buffer, size_t size, size_t count, FILE *stream) 15 15 { 16 STREAM_LOCK(stream); 17 size_t cb = fread_unlocked(buffer, size, count, stream); 18 STREAM_UNLOCK(stream); 16 size_t cb; 17 18 STREAM_XCPT_BEGIN 19 20 STREAM_XCPT_LOCK(stream); 21 cb = fread_unlocked(buffer, size, count, stream); 22 STREAM_XCPT_UNLOCK(stream); 23 24 STREAM_XCPT_END 25 19 26 return cb; 20 27 } 21 28 -
src/emx/src/lib/io/fscanf.c
13 13 va_list arg_ptr; 14 14 int result; 15 15 16 STREAM_XCPT_BEGIN 17 16 18 va_start (arg_ptr, format); 17 STREAM_ LOCK (stream);19 STREAM_XCPT_LOCK (stream); 18 20 result = _input (stream, format, arg_ptr); 19 STREAM_ UNLOCK (stream);21 STREAM_XCPT_UNLOCK (stream); 20 22 va_end (arg_ptr); 23 24 STREAM_XCPT_END 25 21 26 return result; 22 27 } -
src/emx/src/lib/io/fseek.c
182 182 { 183 183 int result; 184 184 185 STREAM_LOCK (stream); 185 STREAM_XCPT_BEGIN 186 187 STREAM_XCPT_LOCK (stream); 186 188 result = _fseek_unlocked (stream, offset, origin); 187 STREAM_UNLOCK (stream); 189 STREAM_XCPT_UNLOCK (stream); 190 191 STREAM_XCPT_END 192 188 193 return result; 189 194 } 190 195 -
src/emx/src/lib/io/ftell.c
102 102 { 103 103 off_t off; 104 104 105 STREAM_LOCK (stream); 105 STREAM_XCPT_BEGIN 106 107 STREAM_XCPT_LOCK (stream); 106 108 off = _ftello_unlocked (stream); 107 STREAM_UNLOCK (stream); 109 STREAM_XCPT_UNLOCK (stream); 110 111 STREAM_XCPT_END 112 108 113 return off; 109 114 } 110 115 -
src/emx/src/lib/io/fwide.c
48 48 LIBCLOG_ENTER("pStream=%p iOrientation=%d\n", (void *)pStream, iOrientation); 49 49 int iRet = -1; 50 50 #if 0 /** @todo LIBC07 */ 51 STREAM_LOCK(pStream); 51 STREAM_XCPT_BEGIN 52 STREAM_XCPT_LOCK(pStream); 52 53 if (iOrientation != 0 && pStream->__iOrientation == 0) 53 54 pStream->__iOrientation = iOrientation > 0 ? 1 : -1; 54 55 iRet = pStream->__iOrientation; 55 STREAM_UNLOCK(pStream); 56 STREAM_XCPT_UNLOCK(pStream); 57 STREAM_XCPT_END 56 58 #endif 57 59 LIBCLOG_RETURN_INT(iRet); 58 60 } -
src/emx/src/lib/io/fwrite.c
168 168 { 169 169 size_t r; 170 170 171 STREAM_LOCK (stream); 171 STREAM_XCPT_BEGIN 172 173 STREAM_XCPT_LOCK (stream); 172 174 r = fwrite_unlocked (buffer, size, count, stream); 173 STREAM_UNLOCK (stream); 175 STREAM_XCPT_UNLOCK (stream); 176 177 STREAM_XCPT_END 178 174 179 return r; 175 180 } -
src/emx/src/lib/io/getdelim.c
76 76 *pcchString = 128; 77 77 } 78 78 79 /*80 * Lock the stream and start reading.81 */82 STREAM_LOCK(pStream);83 79 ssize_t rc = 0; 84 80 char *psz = *ppszString; 85 81 char *pszEnd = psz + *pcchString - 1; 82 83 STREAM_XCPT_BEGIN 84 85 /* 86 * Lock the stream and start reading. 87 */ 88 STREAM_XCPT_LOCK(pStream); 86 89 for (;;) 87 90 { 88 91 /** @todo this can be done faster by accessing what's in the buffer directly. … … 117 120 if (ch == chDelim) 118 121 break; 119 122 } 120 STREAM_UNLOCK(pStream); 123 STREAM_XCPT_UNLOCK(pStream); 124 125 STREAM_XCPT_END 121 126 122 127 if (!rc) 123 128 rc = psz - *ppszString; -
src/emx/src/lib/io/gets.c
36 36 37 37 char *_STD(gets)(char *buffer) 38 38 { 39 STREAM_LOCK(stdin); 40 char *psz = gets_unlocked(buffer); 41 STREAM_UNLOCK(stdin); 39 char *psz; 40 41 STREAM_XCPT_BEGIN 42 43 STREAM_XCPT_LOCK(stdin); 44 psz = gets_unlocked(buffer); 45 STREAM_XCPT_UNLOCK(stdin); 46 47 STREAM_XCPT_END 48 42 49 return psz; 43 50 } 44 51 -
src/emx/src/lib/io/printf.c
14 14 int result; 15 15 void *tb; 16 16 17 STREAM_XCPT_BEGIN 18 17 19 va_start (arg_ptr, format); 18 STREAM_ LOCK (stdout);20 STREAM_XCPT_LOCK (stdout); 19 21 if (nbuf (stdout)) 20 22 _fbuf (stdout); 21 23 _tmpbuf (stdout, tb); 22 24 result = _output (stdout, format, arg_ptr); 23 25 if (_endbuf (stdout) != 0) 24 26 result = -1; 25 STREAM_ UNLOCK (stdout);27 STREAM_XCPT_UNLOCK (stdout); 26 28 va_end (arg_ptr); 29 30 STREAM_XCPT_END 31 27 32 return result; 28 33 } -
src/emx/src/lib/io/puts.c
11 11 12 12 int _STD(puts) (const char *string) 13 13 { 14 STREAM_LOCK(stdout); 15 int rc = puts_unlocked(string); 16 STREAM_UNLOCK(stdout); 14 int rc; 15 16 STREAM_XCPT_BEGIN 17 18 STREAM_XCPT_LOCK(stdout); 19 rc = puts_unlocked(string); 20 STREAM_XCPT_UNLOCK(stdout); 21 22 STREAM_XCPT_END 23 17 24 return rc; 18 25 } 19 26 -
src/emx/src/lib/io/rewind.c
9 9 10 10 void _STD(rewind) (FILE *stream) 11 11 { 12 STREAM_LOCK (stream); 12 STREAM_XCPT_BEGIN 13 STREAM_XCPT_LOCK (stream); 13 14 fflush_unlocked (stream); 14 15 _fseek_unlocked (stream, 0, SEEK_SET); 15 16 stream->_flags &= ~_IOERR; 16 STREAM_UNLOCK (stream); 17 STREAM_XCPT_UNLOCK (stream); 18 STREAM_XCPT_END 17 19 } -
src/emx/src/lib/io/scanf.c
13 13 va_list arg_ptr; 14 14 int result; 15 15 16 STREAM_XCPT_BEGIN 17 16 18 va_start (arg_ptr, format); 17 STREAM_ LOCK (stdin);19 STREAM_XCPT_LOCK (stdin); 18 20 result = _input (stdin, format, arg_ptr); 19 STREAM_ UNLOCK (stdin);21 STREAM_XCPT_UNLOCK (stdin); 20 22 va_end (arg_ptr); 23 24 STREAM_XCPT_END 25 21 26 return result; 22 27 } -
src/emx/src/lib/io/setvbuf.c
13 13 || (mode != _IONBF && mode != _IOFBF && mode != _IOLBF)) 14 14 return EOF; 15 15 16 STREAM_LOCK (stream); 16 STREAM_XCPT_BEGIN 17 18 STREAM_XCPT_LOCK (stream); 17 19 fflush_unlocked (stream); 18 20 if ((stream->_flags & _IOBUFMASK) == _IOBUFLIB) 19 21 free (stream->_buffer); … … 48 50 buffer = malloc (size); 49 51 if (buffer == NULL) 50 52 { 51 STREAM_ UNLOCK(stream);53 STREAM_XCPT_UNLOCK_END (stream); 52 54 return EOF; 53 55 } 54 56 stream->_buf_size = size; … … 58 60 stream->_ptr = stream->_buffer; 59 61 stream->_rcount = 0; 60 62 stream->_wcount = 0; 61 STREAM_UNLOCK (stream); 63 STREAM_XCPT_UNLOCK (stream); 64 65 STREAM_XCPT_END 66 62 67 return 0; 63 68 } -
src/emx/src/lib/io/ungetc.c
153 153 { 154 154 int result; 155 155 156 STREAM_LOCK (stream); 156 STREAM_XCPT_BEGIN 157 158 STREAM_XCPT_LOCK (stream); 157 159 result = _ungetc_nolock (c, stream); 158 STREAM_UNLOCK (stream); 160 STREAM_XCPT_UNLOCK (stream); 161 162 STREAM_XCPT_END 163 159 164 return result; 160 165 } -
src/emx/src/lib/io/vfprintf.c
12 12 int result; 13 13 void *tb; 14 14 15 STREAM_LOCK (stream); 15 STREAM_XCPT_BEGIN 16 17 STREAM_XCPT_LOCK (stream); 16 18 if (nbuf (stream)) 17 19 _fbuf (stream); 18 20 _tmpbuf (stream, tb); 19 21 result = _output (stream, format, arg_ptr); 20 22 if (_endbuf (stream) != 0) 21 23 result = -1; 22 STREAM_UNLOCK (stream); 24 STREAM_XCPT_UNLOCK (stream); 25 26 STREAM_XCPT_END 27 23 28 return result; 24 29 } -
src/emx/src/lib/io/vfscanf.c
12 12 { 13 13 int r; 14 14 15 STREAM_LOCK (stream); 15 STREAM_XCPT_BEGIN 16 17 STREAM_XCPT_LOCK (stream); 16 18 r = _input (stream, format, arg_ptr); 17 STREAM_UNLOCK (stream); 19 STREAM_XCPT_UNLOCK (stream); 20 21 STREAM_XCPT_END 22 18 23 return r; 19 24 } -
src/emx/src/lib/io/vprintf.c
12 12 int result; 13 13 void *tb; 14 14 15 STREAM_LOCK (stdout); 15 STREAM_XCPT_BEGIN 16 17 STREAM_XCPT_LOCK (stdout); 16 18 if (nbuf (stdout)) 17 19 _fbuf (stdout); 18 20 _tmpbuf (stdout, tb); 19 21 result = _output (stdout, format, arg_ptr); 20 22 if (_endbuf (stdout) != 0) 21 23 result = -1; 22 STREAM_UNLOCK (stdout); 24 STREAM_XCPT_UNLOCK (stdout); 25 26 STREAM_XCPT_END 27 23 28 return result; 24 29 } -
src/emx/src/lib/io/vscanf.c
12 12 { 13 13 int r; 14 14 15 STREAM_LOCK (stdin); 15 STREAM_XCPT_BEGIN 16 17 STREAM_XCPT_LOCK (stdin); 16 18 r = _input (stdin, format, arg_ptr); 17 STREAM_UNLOCK (stdin); 19 STREAM_XCPT_UNLOCK (stdin); 20 21 STREAM_XCPT_END 22 18 23 return r; 19 24 }