*** empty log message ***
[deliverable/binutils-gdb.git] / libiberty / fopen_unlocked.c
CommitLineData
ac119ae8
DD
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
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21/*
22
7b6f6286
DD
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
ac119ae8
DD
31@deftypefn Extension FILE * fopen_unlocked (const char *@var{path}, const char * @var{mode})
32
33Opens and returns a @code{FILE} pointer via @code{fopen}. If the
34operating system supports it, ensure that the stream is setup to avoid
35any multi-threaded locking. Otherwise return the @code{FILE} pointer
36unchanged.
37
38@end deftypefn
39
40@deftypefn Extension FILE * fdopen_unlocked (int @var{fildes}, const char * @var{mode})
41
42Opens and returns a @code{FILE} pointer via @code{fdopen}. If the
43operating system supports it, ensure that the stream is setup to avoid
44any multi-threaded locking. Otherwise return the @code{FILE} pointer
45unchanged.
46
47@end deftypefn
48
49@deftypefn Extension FILE * freopen_unlocked (const char * @var{path}, const char * @var{mode}, FILE * @var{stream})
50
51Opens and returns a @code{FILE} pointer via @code{freopen}. If the
52operating system supports it, ensure that the stream is setup to avoid
53any multi-threaded locking. Otherwise return the @code{FILE} pointer
54unchanged.
55
56@end deftypefn
57
58*/
59
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
67
68#include "libiberty.h"
69
7b6f6286
DD
70/* This is an inline helper function to consolidate attempts to unlock
71 a stream. */
72
73static inline void
74unlock_1 (FILE *const fp ATTRIBUTE_UNUSED)
ac119ae8 75{
ac119ae8
DD
76#if defined(HAVE___FSETLOCKING) && defined(FSETLOCKING_BYCALLER)
77 if (fp)
78 __fsetlocking (fp, FSETLOCKING_BYCALLER);
79#endif
7b6f6286
DD
80}
81
82void
83unlock_stream(FILE *fp)
84{
85 unlock_1 (fp);
86}
87
88FILE *
89fopen_unlocked (const char *path, const char *mode)
90{
91 FILE *const fp = fopen (path, mode);
92 unlock_1 (fp);
ac119ae8
DD
93 return fp;
94}
95
96FILE *
97fdopen_unlocked (int fildes, const char *mode)
98{
99 FILE *const fp = fdopen (fildes, mode);
7b6f6286 100 unlock_1 (fp);
ac119ae8
DD
101 return fp;
102}
103
104FILE *
105freopen_unlocked (const char *path, const char *mode, FILE *stream)
106{
107 FILE *const fp = freopen (path, mode, stream);
7b6f6286 108 unlock_1 (fp);
ac119ae8
DD
109 return fp;
110}
This page took 0.04789 seconds and 4 git commands to generate.