]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/fopen_unlocked.c
* MAINTAINERS: Update my email address.
[thirdparty/gcc.git] / libiberty / fopen_unlocked.c
CommitLineData
78a7dc90
KG
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>.
4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If
ee58dffd
NC
18not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19Boston, MA 02110-1301, USA. */
78a7dc90
KG
20
21/*
22
6feaa084
KG
23@deftypefn Extension void unlock_stream (FILE * @var{stream})
24
25If the OS supports it, ensure that the supplied stream is setup to
26avoid any multi-threaded locking. Otherwise leave the @code{FILE}
27pointer unchanged. If the @var{stream} is @code{NULL} do nothing.
28
29@end deftypefn
30
32e82bd8
KG
31@deftypefn Extension void unlock_std_streams (void)
32
33If the OS supports it, ensure that the standard I/O streams,
34@code{stdin}, @code{stdout} and @code{stderr} are setup to avoid any
35multi-threaded locking. Otherwise do nothing.
36
37@end deftypefn
38
a37103a4 39@deftypefn Extension {FILE *} fopen_unlocked (const char *@var{path}, const char * @var{mode})
78a7dc90
KG
40
41Opens and returns a @code{FILE} pointer via @code{fopen}. If the
42operating system supports it, ensure that the stream is setup to avoid
43any multi-threaded locking. Otherwise return the @code{FILE} pointer
44unchanged.
45
46@end deftypefn
47
a37103a4 48@deftypefn Extension {FILE *} fdopen_unlocked (int @var{fildes}, const char * @var{mode})
78a7dc90
KG
49
50Opens and returns a @code{FILE} pointer via @code{fdopen}. If the
51operating system supports it, ensure that the stream is setup to avoid
52any multi-threaded locking. Otherwise return the @code{FILE} pointer
53unchanged.
54
55@end deftypefn
56
a37103a4 57@deftypefn Extension {FILE *} freopen_unlocked (const char * @var{path}, const char * @var{mode}, FILE * @var{stream})
78a7dc90
KG
58
59Opens and returns a @code{FILE} pointer via @code{freopen}. If the
60operating system supports it, ensure that the stream is setup to avoid
61any multi-threaded locking. Otherwise return the @code{FILE} pointer
62unchanged.
63
64@end deftypefn
65
66*/
67
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
75
76#include "libiberty.h"
77
6feaa084
KG
78/* This is an inline helper function to consolidate attempts to unlock
79 a stream. */
80
81static inline void
82unlock_1 (FILE *const fp ATTRIBUTE_UNUSED)
78a7dc90 83{
78a7dc90
KG
84#if defined(HAVE___FSETLOCKING) && defined(FSETLOCKING_BYCALLER)
85 if (fp)
86 __fsetlocking (fp, FSETLOCKING_BYCALLER);
87#endif
6feaa084
KG
88}
89
90void
32e82bd8 91unlock_stream (FILE *fp)
6feaa084
KG
92{
93 unlock_1 (fp);
94}
95
32e82bd8
KG
96void
97unlock_std_streams (void)
98{
99 unlock_1 (stdin);
100 unlock_1 (stdout);
101 unlock_1 (stderr);
102}
103
6feaa084
KG
104FILE *
105fopen_unlocked (const char *path, const char *mode)
106{
107 FILE *const fp = fopen (path, mode);
108 unlock_1 (fp);
78a7dc90
KG
109 return fp;
110}
111
112FILE *
113fdopen_unlocked (int fildes, const char *mode)
114{
115 FILE *const fp = fdopen (fildes, mode);
6feaa084 116 unlock_1 (fp);
78a7dc90
KG
117 return fp;
118}
119
120FILE *
121freopen_unlocked (const char *path, const char *mode, FILE *stream)
122{
123 FILE *const fp = freopen (path, mode, stream);
6feaa084 124 unlock_1 (fp);
78a7dc90
KG
125 return fp;
126}