Mark ChangeLog
[official-gcc.git] / libiberty / fopen_unlocked.c
blob49454359095daeded6bff65c814611ed6103b787
1 /* Implement fopen_unlocked and related functions.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 @deftypefn Extension void unlock_stream (FILE * @var{stream})
25 If the OS supports it, ensure that the supplied stream is setup to
26 avoid any multi-threaded locking. Otherwise leave the @code{FILE}
27 pointer unchanged. If the @var{stream} is @code{NULL} do nothing.
29 @end deftypefn
31 @deftypefn Extension void unlock_std_streams (void)
33 If the OS supports it, ensure that the standard I/O streams,
34 @code{stdin}, @code{stdout} and @code{stderr} are setup to avoid any
35 multi-threaded locking. Otherwise do nothing.
37 @end deftypefn
39 @deftypefn Extension FILE * fopen_unlocked (const char *@var{path}, const char * @var{mode})
41 Opens and returns a @code{FILE} pointer via @code{fopen}. If the
42 operating system supports it, ensure that the stream is setup to avoid
43 any multi-threaded locking. Otherwise return the @code{FILE} pointer
44 unchanged.
46 @end deftypefn
48 @deftypefn Extension FILE * fdopen_unlocked (int @var{fildes}, const char * @var{mode})
50 Opens and returns a @code{FILE} pointer via @code{fdopen}. If the
51 operating system supports it, ensure that the stream is setup to avoid
52 any multi-threaded locking. Otherwise return the @code{FILE} pointer
53 unchanged.
55 @end deftypefn
57 @deftypefn Extension FILE * freopen_unlocked (const char * @var{path}, const char * @var{mode}, FILE * @var{stream})
59 Opens and returns a @code{FILE} pointer via @code{freopen}. If the
60 operating system supports it, ensure that the stream is setup to avoid
61 any multi-threaded locking. Otherwise return the @code{FILE} pointer
62 unchanged.
64 @end deftypefn
68 #ifdef HAVE_CONFIG_H
69 #include "config.h"
70 #endif
71 #include <stdio.h>
72 #ifdef HAVE_STDIO_EXT_H
73 #include <stdio_ext.h>
74 #endif
76 #include "libiberty.h"
78 /* This is an inline helper function to consolidate attempts to unlock
79 a stream. */
81 static inline void unlock_1 PARAMS ((FILE *const fp ATTRIBUTE_UNUSED));
82 static inline void
83 unlock_1 (fp)
84 FILE *const fp ATTRIBUTE_UNUSED;
86 #if defined(HAVE___FSETLOCKING) && defined(FSETLOCKING_BYCALLER)
87 if (fp)
88 __fsetlocking (fp, FSETLOCKING_BYCALLER);
89 #endif
92 void
93 unlock_stream (fp)
94 FILE *fp;
96 unlock_1 (fp);
99 void
100 unlock_std_streams ()
102 unlock_1 (stdin);
103 unlock_1 (stdout);
104 unlock_1 (stderr);
107 FILE *
108 fopen_unlocked (path, mode)
109 const char *path;
110 const char *mode;
112 FILE *const fp = fopen (path, mode);
113 unlock_1 (fp);
114 return fp;
117 FILE *
118 fdopen_unlocked (fildes, mode)
119 int fildes;
120 const char *mode;
122 FILE *const fp = fdopen (fildes, mode);
123 unlock_1 (fp);
124 return fp;
127 FILE *
128 freopen_unlocked (path, mode, stream)
129 const char *path;
130 const char *mode;
131 FILE *stream;
133 FILE *const fp = freopen (path, mode, stream);
134 unlock_1 (fp);
135 return fp;