* elfxx-mips.c (struct mips_elf_link_hash_entry): Update comment.
[binutils.git] / libiberty / fopen_unlocked.c
blob8f9f300d10121b666ef8a46757fae01dac32db69
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 FILE * fopen_unlocked (const char *@var{path}, const char * @var{mode})
33 Opens and returns a @code{FILE} pointer via @code{fopen}. If the
34 operating system supports it, ensure that the stream is setup to avoid
35 any multi-threaded locking. Otherwise return the @code{FILE} pointer
36 unchanged.
38 @end deftypefn
40 @deftypefn Extension FILE * fdopen_unlocked (int @var{fildes}, const char * @var{mode})
42 Opens and returns a @code{FILE} pointer via @code{fdopen}. If the
43 operating system supports it, ensure that the stream is setup to avoid
44 any multi-threaded locking. Otherwise return the @code{FILE} pointer
45 unchanged.
47 @end deftypefn
49 @deftypefn Extension FILE * freopen_unlocked (const char * @var{path}, const char * @var{mode}, FILE * @var{stream})
51 Opens and returns a @code{FILE} pointer via @code{freopen}. If the
52 operating system supports it, ensure that the stream is setup to avoid
53 any multi-threaded locking. Otherwise return the @code{FILE} pointer
54 unchanged.
56 @end deftypefn
60 #ifdef HAVE_CONFIG_H
61 #include "config.h"
62 #endif
63 #include <stdio.h>
64 #ifdef HAVE_STDIO_EXT_H
65 #include <stdio_ext.h>
66 #endif
68 #include "libiberty.h"
70 /* This is an inline helper function to consolidate attempts to unlock
71 a stream. */
73 static inline void
74 unlock_1 (FILE *const fp ATTRIBUTE_UNUSED)
76 #if defined(HAVE___FSETLOCKING) && defined(FSETLOCKING_BYCALLER)
77 if (fp)
78 __fsetlocking (fp, FSETLOCKING_BYCALLER);
79 #endif
82 void
83 unlock_stream(FILE *fp)
85 unlock_1 (fp);
88 FILE *
89 fopen_unlocked (const char *path, const char *mode)
91 FILE *const fp = fopen (path, mode);
92 unlock_1 (fp);
93 return fp;
96 FILE *
97 fdopen_unlocked (int fildes, const char *mode)
99 FILE *const fp = fdopen (fildes, mode);
100 unlock_1 (fp);
101 return fp;
104 FILE *
105 freopen_unlocked (const char *path, const char *mode, FILE *stream)
107 FILE *const fp = freopen (path, mode, stream);
108 unlock_1 (fp);
109 return fp;