Back to index

cell-binutils  2.17cvs20070401
fopen_unlocked.c
Go to the documentation of this file.
00001 /* Implement fopen_unlocked and related functions.
00002    Copyright (C) 2005 Free Software Foundation, Inc.
00003    Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
00004 
00005 This file is part of the libiberty library.
00006 Libiberty is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Library General Public
00008 License as published by the Free Software Foundation; either
00009 version 2 of the License, or (at your option) any later version.
00010 
00011 Libiberty is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Library General Public License for more details.
00015 
00016 You should have received a copy of the GNU Library General Public
00017 License along with libiberty; see the file COPYING.LIB.  If
00018 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019 Boston, MA 02110-1301, USA.  */
00020 
00021 /*
00022 
00023 @deftypefn Extension void unlock_stream (FILE * @var{stream})
00024 
00025 If the OS supports it, ensure that the supplied stream is setup to
00026 avoid any multi-threaded locking.  Otherwise leave the @code{FILE}
00027 pointer unchanged.  If the @var{stream} is @code{NULL} do nothing.
00028 
00029 @end deftypefn
00030 
00031 @deftypefn Extension void unlock_std_streams (void)
00032 
00033 If the OS supports it, ensure that the standard I/O streams,
00034 @code{stdin}, @code{stdout} and @code{stderr} are setup to avoid any
00035 multi-threaded locking.  Otherwise do nothing.
00036 
00037 @end deftypefn
00038 
00039 @deftypefn Extension {FILE *} fopen_unlocked (const char *@var{path}, const char * @var{mode})
00040 
00041 Opens and returns a @code{FILE} pointer via @code{fopen}.  If the
00042 operating system supports it, ensure that the stream is setup to avoid
00043 any multi-threaded locking.  Otherwise return the @code{FILE} pointer
00044 unchanged.
00045 
00046 @end deftypefn
00047 
00048 @deftypefn Extension {FILE *} fdopen_unlocked (int @var{fildes}, const char * @var{mode})
00049 
00050 Opens and returns a @code{FILE} pointer via @code{fdopen}.  If the
00051 operating system supports it, ensure that the stream is setup to avoid
00052 any multi-threaded locking.  Otherwise return the @code{FILE} pointer
00053 unchanged.
00054 
00055 @end deftypefn
00056 
00057 @deftypefn Extension {FILE *} freopen_unlocked (const char * @var{path}, const char * @var{mode}, FILE * @var{stream})
00058 
00059 Opens and returns a @code{FILE} pointer via @code{freopen}.  If the
00060 operating system supports it, ensure that the stream is setup to avoid
00061 any multi-threaded locking.  Otherwise return the @code{FILE} pointer
00062 unchanged.
00063 
00064 @end deftypefn
00065 
00066 */
00067 
00068 #ifdef HAVE_CONFIG_H
00069 #include "config.h"
00070 #endif
00071 #include <stdio.h>
00072 #ifdef HAVE_STDIO_EXT_H
00073 #include <stdio_ext.h>
00074 #endif
00075 
00076 #include "libiberty.h"
00077 
00078 /* This is an inline helper function to consolidate attempts to unlock
00079    a stream.  */
00080 
00081 static inline void
00082 unlock_1 (FILE *const fp ATTRIBUTE_UNUSED)
00083 {
00084 #if defined(HAVE___FSETLOCKING) && defined(FSETLOCKING_BYCALLER)
00085   if (fp)
00086     __fsetlocking (fp, FSETLOCKING_BYCALLER);
00087 #endif
00088 }
00089 
00090 void
00091 unlock_stream (FILE *fp)
00092 {
00093   unlock_1 (fp);
00094 }
00095 
00096 void
00097 unlock_std_streams (void)
00098 {
00099   unlock_1 (stdin);
00100   unlock_1 (stdout);
00101   unlock_1 (stderr);
00102 }
00103 
00104 FILE *
00105 fopen_unlocked (const char *path, const char *mode)            
00106 {
00107   FILE *const fp = fopen (path, mode);
00108   unlock_1 (fp);
00109   return fp;
00110 }
00111 
00112 FILE *
00113 fdopen_unlocked (int fildes, const char *mode)
00114 {
00115   FILE *const fp = fdopen (fildes, mode);
00116   unlock_1 (fp);
00117   return fp;
00118 }
00119 
00120 FILE *
00121 freopen_unlocked (const char *path, const char *mode, FILE *stream)
00122 {
00123   FILE *const fp = freopen (path, mode, stream);
00124   unlock_1 (fp);
00125   return fp;
00126 }