OSDN Git Service

rework internal uClibc mutexes to support futex locking, and nptl
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / bits / uClibc_stdio.h
1 /* Copyright (C) 2002-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
8 #ifndef _STDIO_H
9 #error Always include <stdio.h> rather than <bits/uClibc_stdio.h>
10 #endif
11
12 /**********************************************************************/
13
14 #define __STDIO_BUFFERS
15 /* ANSI/ISO mandate at least 256. */
16 #if defined(__UCLIBC_HAS_STDIO_BUFSIZ_NONE__)
17 /* Fake this because some apps use stdio.h BUFSIZ. */
18 #define __STDIO_BUFSIZ                  256
19 #undef __STDIO_BUFFERS
20 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_256__)
21 #define __STDIO_BUFSIZ                  256
22 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_512__)
23 #define __STDIO_BUFSIZ                  512
24 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_1024__)
25 #define __STDIO_BUFSIZ             1024
26 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_2048__)
27 #define __STDIO_BUFSIZ             2048
28 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_4096__)
29 #define __STDIO_BUFSIZ             4096
30 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_8192__)
31 #define __STDIO_BUFSIZ             8192
32 #else
33 #error config seems to be out of sync regarding bufsiz options
34 #endif
35
36 #ifdef __UCLIBC_HAS_STDIO_BUFSIZ_NONE__
37 #define __STDIO_BUILTIN_BUF_SIZE                0
38 #else  /* __UCLIBC_HAS_STDIO_BUFSIZ_NONE__ */
39 #if defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_NONE__)
40 #define __STDIO_BUILTIN_BUF_SIZE                0
41 #elif defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_4__)
42 #define __STDIO_BUILTIN_BUF_SIZE                4
43 #elif defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_8__)
44 #define __STDIO_BUILTIN_BUF_SIZE                8
45 #else
46 #error config seems to be out of sync regarding builtin buffer size
47 #endif
48 #endif
49
50 #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__) || defined(__UCLIBC_HAS_THREADS__)
51 #define __STDIO_HAS_OPENLIST 1
52 #else
53 #undef __STDIO_HAS_OPENLIST
54 #endif
55
56 /**********************************************************************/
57 /* Make sure defines related to large files are consistent. */
58 #ifdef _LIBC
59
60 #ifdef __UCLIBC_HAS_LFS__
61 #undef __USE_LARGEFILE
62 #undef __USE_LARGEFILE64
63 #undef __USE_FILE_OFFSET64
64 /* If we're actually building uClibc with large file support, only define... */
65 #define __USE_LARGEFILE64       1
66 #endif /* __UCLIBC_HAS_LFS__ */
67
68 #else  /* not _LIBC */
69
70 #ifndef __UCLIBC_HAS_LFS__
71 #if defined(__LARGEFILE64_SOURCE) || defined(__USE_LARGEFILE64) || defined(__USE_FILE_OFFSET64)
72 #error Sorry... uClibc was built without large file support!
73 #endif
74 #endif /* __UCLIBC_HAS_LFS__ */
75
76 #endif /* _LIBC */
77 /**********************************************************************/
78 #ifdef __UCLIBC_HAS_WCHAR__
79
80 #define __need_wchar_t
81 #include <stddef.h>
82
83 /* Note: we don't really need mbstate for 8-bit locales.  We do for UTF-8.
84  * For now, always use it. */
85 #define __STDIO_MBSTATE
86 #define __need_mbstate_t
87 #include <wchar.h>
88
89 #endif
90 /**********************************************************************/
91 /* Currently unimplemented/untested */
92 /* #define __STDIO_FLEXIBLE_SETVBUF */
93
94 #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
95 #define __STDIO_GETC_MACRO
96 #endif
97
98 #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
99 #define __STDIO_PUTC_MACRO
100 #endif
101
102
103 /* These are consistency checks on the different options */
104
105 #ifndef __STDIO_BUFFERS
106 #undef __STDIO_GETC_MACRO
107 #undef __STDIO_PUTC_MACRO
108 #endif
109
110 #ifdef __BCC__
111 #undef __UCLIBC_HAS_LFS__
112 #endif
113
114 #ifndef __UCLIBC_HAS_LFS__
115 #undef __UCLIBC_HAS_FOPEN_LARGEFILE_MODE__
116 #endif
117
118 /**********************************************************************/
119 #include <bits/uClibc_mutex.h>
120
121 /* user_locking
122  * 0 : do auto locking/unlocking
123  * 1 : user does locking/unlocking
124  * 2 : initial state prior to thread initialization
125  *     with no auto locking/unlocking
126  *
127  * When threading is initialized, walk the stdio open stream list
128  * and do  "if (user_locking == 2) user_locking = 0;".
129  *
130  * This way, we avoid calling the weak lock/unlock functions.
131  */
132
133 #define __STDIO_AUTO_THREADLOCK_VAR                                             \
134         __UCLIBC_MUTEX_AUTO_LOCK_VAR(__infunc_user_locking)
135
136 #define __STDIO_AUTO_THREADLOCK(__stream)                                       \
137         __UCLIBC_IO_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking,  \
138         (__stream)->__user_locking)
139
140 #define __STDIO_AUTO_THREADUNLOCK(__stream)                                     \
141         __UCLIBC_IO_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
142
143 #define __STDIO_ALWAYS_THREADLOCK(__stream)                                     \
144         __UCLIBC_IO_MUTEX_LOCK((__stream)->__lock)
145
146 #define __STDIO_ALWAYS_THREADUNLOCK(__stream)                                   \
147         __UCLIBC_IO_MUTEX_UNLOCK((__stream)->__lock)
148
149 #define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream)                       \
150         __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
151
152 #define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream)                    \
153         __UCLIBC_IO_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
154
155 #define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream)                     \
156         __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE((__stream)->__lock)
157
158 #ifdef __UCLIBC_HAS_THREADS__
159 #define __STDIO_SET_USER_LOCKING(__stream)      ((__stream)->__user_locking = 1)
160 #else
161 #define __STDIO_SET_USER_LOCKING(__stream)              ((void)0)
162 #endif
163
164 #ifdef __UCLIBC_HAS_THREADS__
165 #ifdef __USE_STDIO_FUTEXES__
166 #define STDIO_INIT_MUTEX(M) _IO_lock_init(M)
167 #else
168 #define STDIO_INIT_MUTEX(M) __stdio_init_mutex(& M)
169 #endif
170 #endif
171
172 /**********************************************************************/
173
174 #define __STDIO_IOFBF 0         /* Fully buffered.  */
175 #define __STDIO_IOLBF 1         /* Line buffered.  */
176 #define __STDIO_IONBF 2         /* No buffering.  */
177
178 typedef struct {
179         __off_t __pos;
180 #ifdef __STDIO_MBSTATE
181         __mbstate_t __mbstate;
182 #endif
183 #ifdef __UCLIBC_HAS_WCHAR__
184         int __mblen_pending;
185 #endif
186 } __STDIO_fpos_t;
187
188 #ifdef __UCLIBC_HAS_LFS__
189 typedef struct {
190         __off64_t __pos;
191 #ifdef __STDIO_MBSTATE
192         __mbstate_t __mbstate;
193 #endif
194 #ifdef __UCLIBC_HAS_WCHAR__
195         int __mblen_pending;
196 #endif
197 } __STDIO_fpos64_t;
198 #endif
199
200 /**********************************************************************/
201 #ifdef __UCLIBC_HAS_LFS__
202 typedef __off64_t __offmax_t;           /* TODO -- rename this? */
203 #else
204 typedef __off_t __offmax_t;             /* TODO -- rename this? */
205 #endif
206
207 /**********************************************************************/
208 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
209
210 typedef __ssize_t __io_read_fn(void *__cookie, char *__buf, size_t __bufsize);
211 typedef __ssize_t __io_write_fn(void *__cookie,
212                                         __const char *__buf, size_t __bufsize);
213 /* NOTE: GLIBC difference!!! -- fopencookie seek function
214  * For glibc, the type of pos is always (__off64_t *) but in our case
215  * it is type (__off_t *) when the lib is built without large file support.
216  */
217 typedef int __io_seek_fn(void *__cookie, __offmax_t *__pos, int __whence);
218 typedef int __io_close_fn(void *__cookie);
219
220 typedef struct {
221         __io_read_fn  *read;
222         __io_write_fn *write;
223         __io_seek_fn  *seek;
224         __io_close_fn *close;
225 } _IO_cookie_io_functions_t;
226
227 #if defined(_LIBC) || defined(_GNU_SOURCE)
228
229 typedef __io_read_fn cookie_read_function_t;
230 typedef __io_write_fn cookie_write_function_t;
231 typedef __io_seek_fn cookie_seek_function_t;
232 typedef __io_close_fn cookie_close_function_t;
233
234 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
235
236 #endif
237
238 #endif
239 /**********************************************************************/
240
241 struct __STDIO_FILE_STRUCT {
242         unsigned short __modeflags;
243         /* There could be a hole here, but modeflags is used most.*/
244 #ifdef __UCLIBC_HAS_WCHAR__
245         unsigned char __ungot_width[2]; /* 0: current (building) char; 1: scanf */
246         /* Move the following futher down to avoid problems with getc/putc
247          * macros breaking shared apps when wchar config support is changed. */
248         /* wchar_t ungot[2]; */
249 #else  /* __UCLIBC_HAS_WCHAR__ */
250         unsigned char __ungot[2];
251 #endif /* __UCLIBC_HAS_WCHAR__ */
252         int __filedes;
253 #ifdef __STDIO_BUFFERS
254         unsigned char *__bufstart;      /* pointer to buffer */
255         unsigned char *__bufend;        /* pointer to 1 past end of buffer */
256         unsigned char *__bufpos;
257         unsigned char *__bufread; /* pointer to 1 past last buffered read char */
258
259 #ifdef __STDIO_GETC_MACRO
260         unsigned char *__bufgetc_u;     /* 1 past last readable by getc_unlocked */
261 #endif /* __STDIO_GETC_MACRO */
262 #ifdef __STDIO_PUTC_MACRO
263         unsigned char *__bufputc_u;     /* 1 past last writeable by putc_unlocked */
264 #endif /* __STDIO_PUTC_MACRO */
265
266 #endif /* __STDIO_BUFFERS */
267
268 #ifdef __STDIO_HAS_OPENLIST
269         struct __STDIO_FILE_STRUCT *__nextopen;
270 #endif
271 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
272         void *__cookie;
273         _IO_cookie_io_functions_t __gcs;
274 #endif
275 #ifdef __UCLIBC_HAS_WCHAR__
276         wchar_t __ungot[2];
277 #endif
278 #ifdef __STDIO_MBSTATE
279         __mbstate_t __state;
280 #endif
281 #ifdef __UCLIBC_HAS_XLOCALE__
282         void *__unused;                         /* Placeholder for codeset binding. */
283 #endif
284 #ifdef __UCLIBC_HAS_THREADS__
285         int __user_locking;
286         __UCLIBC_IO_MUTEX(__lock);
287 #endif
288 /* Everything after this is unimplemented... and may be trashed. */
289 #if __STDIO_BUILTIN_BUF_SIZE > 0
290         unsigned char __builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
291 #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
292 };
293
294
295 /***********************************************************************/
296 /* Having ungotten characters implies the stream is reading.
297  * The scheme used here treats the least significant 2 bits of
298  * the stream's modeflags member as follows:
299  *   0 0   Not currently reading.
300  *   0 1   Reading, but no ungetc() or scanf() push back chars.
301  *   1 0   Reading with one ungetc() char (ungot[1] is 1)
302  *         or one scanf() pushed back char (ungot[1] is 0).
303  *   1 1   Reading with both an ungetc() char and a scanf()
304  *         pushed back char.  Note that this must be the result
305  *         of a scanf() push back (in ungot[0]) _followed_ by
306  *         an ungetc() call (in ungot[1]).
307  *
308  * Notes:
309  *   scanf() can NOT use ungetc() to push back characters.
310  *     (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
311  */
312
313 #define __MASK_READING          0x0003U /* (0x0001 | 0x0002) */
314 #define __FLAG_READING          0x0001U
315 #define __FLAG_UNGOT            0x0002U
316 #define __FLAG_EOF              0x0004U
317 #define __FLAG_ERROR            0x0008U
318 #define __FLAG_WRITEONLY        0x0010U
319 #define __FLAG_READONLY         0x0020U /* (__FLAG_WRITEONLY << 1) */
320 #define __FLAG_WRITING          0x0040U
321 #define __FLAG_NARROW           0x0080U
322
323 #define __FLAG_FBF              0x0000U /* must be 0 */
324 #define __FLAG_LBF              0x0100U
325 #define __FLAG_NBF              0x0200U /* (__FLAG_LBF << 1) */
326 #define __MASK_BUFMODE          0x0300U /* (__FLAG_LBF|__FLAG_NBF) */
327 #define __FLAG_APPEND           0x0400U /* fixed! == O_APPEND for linux */
328 #define __FLAG_WIDE             0x0800U
329 /* available slot               0x1000U */
330 #define __FLAG_FREEFILE         0x2000U
331 #define __FLAG_FREEBUF          0x4000U
332 #define __FLAG_LARGEFILE        0x8000U /* fixed! == 0_LARGEFILE for linux */
333 #define __FLAG_FAILED_FREOPEN   __FLAG_LARGEFILE
334
335 /* Note: In no-buffer mode, it would be possible to pack the necessary
336  * flags into one byte.  Since we wouldn't be buffering and there would
337  * be no support for wchar, the only flags we would need would be:
338  *   2 bits : ungot count
339  *   2 bits : eof + error
340  *   2 bits : readonly + writeonly
341  *   1 bit  : freefile
342  *   1 bit  : appending
343  * So, for a very small system (< 128 files) we might have a
344  * 4-byte FILE struct of:
345  *   unsigned char flags;
346  *   signed char filedes;
347  *   unsigned char ungot[2];
348  */
349 /**********************************************************************
350  * PROTOTYPES OF INTERNAL FUNCTIONS
351  **********************************************************************/
352 #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
353
354 extern void _stdio_init(void) attribute_hidden;
355 extern void _stdio_term(void) attribute_hidden;
356
357 #ifdef __STDIO_HAS_OPENLIST
358
359 extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
360
361 #ifdef __UCLIBC_HAS_THREADS__
362 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_add_lock);
363 #ifdef __STDIO_BUFFERS
364 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_del_lock);
365 extern volatile int _stdio_openlist_use_count; /* _stdio_openlist_del_lock */
366 extern int _stdio_openlist_del_count; /* _stdio_openlist_del_lock */
367 #endif
368 extern int _stdio_user_locking;
369 extern void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m) attribute_hidden;
370 #endif
371
372 #endif
373
374 #endif
375 /**********************************************************************/
376
377 #define __CLEARERR_UNLOCKED(__stream)                                   \
378         ((void)((__stream)->__modeflags &= ~(__FLAG_EOF|__FLAG_ERROR)))
379 #define __FEOF_UNLOCKED(__stream)       ((__stream)->__modeflags & __FLAG_EOF)
380 #define __FERROR_UNLOCKED(__stream)     ((__stream)->__modeflags & __FLAG_ERROR)
381
382 #ifdef __UCLIBC_HAS_THREADS__
383 # define __CLEARERR(__stream)           (clearerr)(__stream)
384 # define __FERROR(__stream)             (ferror)(__stream)
385 # define __FEOF(__stream)               (feof)(__stream)
386 #else
387 # define __CLEARERR(__stream)           __CLEARERR_UNLOCKED(__stream)
388 # define __FERROR(__stream)             __FERROR_UNLOCKED(__stream)
389 # define __FEOF(__stream)               __FEOF_UNLOCKED(__stream)
390 #endif
391
392 extern int __fgetc_unlocked(FILE *__stream);
393 libc_hidden_proto(__fgetc_unlocked)
394 extern int __fputc_unlocked(int __c, FILE *__stream);
395 libc_hidden_proto(__fputc_unlocked)
396
397 /* First define the default definitions.
398    They are overridden below as necessary. */
399 #define __FGETC_UNLOCKED(__stream)              (__fgetc_unlocked)((__stream))
400 #define __FGETC(__stream)                       (fgetc)((__stream))
401 #define __GETC_UNLOCKED_MACRO(__stream)         (__fgetc_unlocked)((__stream))
402 #define __GETC_UNLOCKED(__stream)               (__fgetc_unlocked)((__stream))
403 #define __GETC(__stream)                        (fgetc)((__stream))
404
405 #define __FPUTC_UNLOCKED(__c, __stream)         (__fputc_unlocked)((__c),(__stream))
406 #define __FPUTC(__c, __stream)                  (fputc)((__c),(__stream))
407 #define __PUTC_UNLOCKED_MACRO(__c, __stream)    (__fputc_unlocked)((__c),(__stream))
408 #define __PUTC_UNLOCKED(__c, __stream)          (__fputc_unlocked)((__c),(__stream))
409 #define __PUTC(__c, __stream)                   (fputc)((__c),(__stream))
410
411
412 #ifdef __STDIO_GETC_MACRO
413
414 extern FILE *__stdin;                   /* For getchar() macro. */
415
416 # undef  __GETC_UNLOCKED_MACRO
417 # define __GETC_UNLOCKED_MACRO(__stream)                                \
418                 ( ((__stream)->__bufpos < (__stream)->__bufgetc_u)      \
419                   ? (*(__stream)->__bufpos++)                           \
420                   : __fgetc_unlocked(__stream) )
421
422 # if 0
423         /* Classic macro approach.  getc{_unlocked} can have side effects. */
424 #  undef  __GETC_UNLOCKED
425 #  define __GETC_UNLOCKED(__stream)             __GETC_UNLOCKED_MACRO((__stream))
426 #  ifndef __UCLIBC_HAS_THREADS__
427 #   undef  __GETC
428 #   define __GETC(__stream)                             __GETC_UNLOCKED_MACRO((__stream))
429 #  endif
430
431 # else
432         /* Using gcc extension for safety and additional inlining. */
433 #  undef  __FGETC_UNLOCKED
434 #  define __FGETC_UNLOCKED(__stream)                                    \
435                 (__extension__ ({                                       \
436                         FILE *__S = (__stream);                         \
437                         __GETC_UNLOCKED_MACRO(__S);                     \
438                 }) )
439
440 #  undef  __GETC_UNLOCKED
441 #  define __GETC_UNLOCKED(__stream)             __FGETC_UNLOCKED((__stream))
442
443 #  ifdef __UCLIBC_HAS_THREADS__
444 #   undef  __FGETC
445 #   define __FGETC(__stream)                                            \
446                 (__extension__ ({                                       \
447                         FILE *__S = (__stream);                         \
448                         ((__S->__user_locking )                         \
449                          ? __GETC_UNLOCKED_MACRO(__S)                   \
450                          : (fgetc)(__S));                               \
451                 }) )
452
453 #   undef  __GETC
454 #   define __GETC(__stream)                     __FGETC((__stream))
455
456 #  else
457
458 #   undef  __FGETC
459 #   define __FGETC(__stream)                    __FGETC_UNLOCKED((__stream))
460 #   undef  __GETC
461 #   define __GETC(__stream)                     __FGETC_UNLOCKED((__stream))
462
463 #  endif
464 # endif
465
466 #else
467
468 #endif /* __STDIO_GETC_MACRO */
469
470
471 #ifdef __STDIO_PUTC_MACRO
472
473 extern FILE *__stdout;                  /* For putchar() macro. */
474
475 # undef  __PUTC_UNLOCKED_MACRO
476 # define __PUTC_UNLOCKED_MACRO(__c, __stream)                           \
477                 ( ((__stream)->__bufpos < (__stream)->__bufputc_u)      \
478                   ? (*(__stream)->__bufpos++) = (__c)                   \
479                   : __fputc_unlocked((__c),(__stream)) )
480
481 # if 0
482         /* Classic macro approach.  putc{_unlocked} can have side effects.*/
483 #  undef  __PUTC_UNLOCKED
484 #  define __PUTC_UNLOCKED(__c, __stream)                                \
485                                         __PUTC_UNLOCKED_MACRO((__c), (__stream))
486 #  ifndef __UCLIBC_HAS_THREADS__
487 #   undef  __PUTC
488 #   define __PUTC(__c, __stream)        __PUTC_UNLOCKED_MACRO((__c), (__stream))
489 #  endif
490
491 # else
492         /* Using gcc extension for safety and additional inlining. */
493
494 #  undef  __FPUTC_UNLOCKED
495 #  define __FPUTC_UNLOCKED(__c, __stream)                               \
496                 (__extension__ ({                                       \
497                         FILE *__S = (__stream);                         \
498                         __PUTC_UNLOCKED_MACRO((__c),__S);               \
499                 }) )
500
501 #  undef  __PUTC_UNLOCKED
502 #  define __PUTC_UNLOCKED(__c, __stream)        __FPUTC_UNLOCKED((__c), (__stream))
503
504 #  ifdef __UCLIBC_HAS_THREADS__
505 #   undef  __FPUTC
506 #   define __FPUTC(__c, __stream)                                       \
507                 (__extension__ ({                                       \
508                         FILE *__S = (__stream);                         \
509                         ((__S->__user_locking)                          \
510                          ? __PUTC_UNLOCKED_MACRO((__c),__S)             \
511                          : (fputc)((__c),__S));                         \
512                 }) )
513
514 #   undef  __PUTC
515 #   define __PUTC(__c, __stream)                __FPUTC((__c), (__stream))
516
517 #  else
518
519 #   undef  __FPUTC
520 #   define __FPUTC(__c, __stream)               __FPUTC_UNLOCKED((__c),(__stream))
521 #   undef  __PUTC
522 #   define __PUTC(__c, __stream)                __FPUTC_UNLOCKED((__c),(__stream))
523
524 #  endif
525 # endif
526
527 #endif /* __STDIO_PUTC_MACRO */