From 35f4c9560509abdc1bc1710dfe10bf8cf4f0cdbe Mon Sep 17 00:00:00 2001
From: ghazi <ghazi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Mon, 11 Apr 2005 17:49:47 +0000
Subject: [PATCH] include: 	* libiberty.h (fopen_unlocked,
 fdopen_unlocked, freopen_unlocked): 	Provide prototypes for new functions.

libiberty:
	* Makefile.in (CFILES): Add fopen_unlocked.c.
	(REQUIRED_OFILES): Add ./fopen_unlocked.o.
	Regenerate dependencies.

	* configure.ac: Check for stdio_ext.h and __fsetlocking.

	* fopen_unlocked.c: New file.

	* functions.texi, configure, config.in: Regenerate.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@97977 138bc75d-0d04-0410-961f-82ee72b054a4
---
 include/ChangeLog          |  5 ++
 include/libiberty.h        |  8 ++++
 libiberty/ChangeLog        | 12 +++++
 libiberty/Makefile.in      | 11 ++++-
 libiberty/config.in        |  6 +++
 libiberty/configure        |  8 ++--
 libiberty/configure.ac     |  6 +--
 libiberty/fopen_unlocked.c | 93 ++++++++++++++++++++++++++++++++++++++
 libiberty/functions.texi   | 30 ++++++++++++
 9 files changed, 172 insertions(+), 7 deletions(-)
 create mode 100644 libiberty/fopen_unlocked.c

diff --git a/include/ChangeLog b/include/ChangeLog
index a4ebee08f75a..8a771799ff2f 100644
--- a/include/ChangeLog
+++ b/include/ChangeLog
@@ -1,3 +1,8 @@
+2005-04-11  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+	* libiberty.h (fopen_unlocked, fdopen_unlocked, freopen_unlocked):
+	Provide prototypes for new functions.
+
 2005-03-29  Ian Lance Taylor  <ian@airs.com>
 
 	* libiberty.h: Fix indentation.
diff --git a/include/libiberty.h b/include/libiberty.h
index eb5633b26ec2..80aee8b94606 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -48,6 +48,14 @@ extern "C" {
 
 #include <stdio.h>
 
+/* Open and return a FILE pointer.  If the OS supports it, ensure that
+   the stream is setup to avoid any multi-threaded locking.  Otherwise
+   return the FILE pointer unchanged.  */
+
+extern FILE *fopen_unlocked (const char *path, const char *mode);
+extern FILE *fdopen_unlocked (int fildes, const char *mode);
+extern FILE *freopen_unlocked (const char *path, const char *mode, FILE *stream);
+
 /* Build an argument vector from a string.  Allocates memory using
    malloc.  Use freeargv to free the vector.  */
 
diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog
index 169c821e5ac4..df6106e8c603 100644
--- a/libiberty/ChangeLog
+++ b/libiberty/ChangeLog
@@ -1,3 +1,15 @@
+2005-04-11  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+	* Makefile.in (CFILES): Add fopen_unlocked.c.
+	(REQUIRED_OFILES): Add ./fopen_unlocked.o.
+	Regenerate dependencies.
+
+	* configure.ac: Check for stdio_ext.h and __fsetlocking.
+
+	* fopen_unlocked.c: New file.
+
+	* functions.texi, configure, config.in: Regenerate.
+
 2005-04-04  Ian Lance Taylor  <ian@airs.com>
 
 	* testsuite/test-pexecute.c (TEST_PEX_RUN): Move variable
diff --git a/libiberty/Makefile.in b/libiberty/Makefile.in
index 78957ee46629..680401514756 100644
--- a/libiberty/Makefile.in
+++ b/libiberty/Makefile.in
@@ -132,6 +132,7 @@ CFILES = alloca.c argv.c asprintf.c atexit.c				\
 	 cp-demint.c cplus-dem.c					\
 	dyn-string.c							\
 	fdmatch.c ffs.c fibheap.c floatformat.c fnmatch.c		\
+	 fopen_unlocked.c						\
 	getcwd.c getopt.c getopt1.c getpagesize.c getpwd.c getruntime.c	\
          gettimeofday.c                                                 \
 	hashtab.c hex.c							\
@@ -165,6 +166,7 @@ REQUIRED_OFILES = ./regex.o ./cplus-dem.o ./cp-demangle.o ./md5.o	\
 	./choose-temp.o ./concat.o ./cp-demint.o			\
 	./dyn-string.o							\
 	./fdmatch.o ./fibheap.o ./floatformat.o ./fnmatch.o		\
+	 ./fopen_unlocked.o						\
 	./getopt.o ./getopt1.o ./getpwd.o ./getruntime.o		\
 	./hashtab.o ./hex.o						\
 	./lbasename.o ./lrealpath.o					\
@@ -584,6 +586,13 @@ $(CONFIGURED_OFILES): stamp-picdir
 	else true; fi
 	$(COMPILE.c) $(srcdir)/fnmatch.c $(OUTPUT_OPTION)
 
+./fopen_unlocked.o: $(srcdir)/fopen_unlocked.c config.h $(INCDIR)/ansidecl.h \
+	$(INCDIR)/libiberty.h
+	if [ x"$(PICFLAG)" != x ]; then \
+	  $(COMPILE.c) $(PICFLAG) $(srcdir)/fopen_unlocked.c -o pic/$@; \
+	else true; fi
+	$(COMPILE.c) $(srcdir)/fopen_unlocked.c $(OUTPUT_OPTION)
+
 ./getcwd.o: $(srcdir)/getcwd.c config.h
 	if [ x"$(PICFLAG)" != x ]; then \
 	  $(COMPILE.c) $(PICFLAG) $(srcdir)/getcwd.c -o pic/$@; \
@@ -1041,7 +1050,7 @@ $(CONFIGURED_OFILES): stamp-picdir
 	else true; fi
 	$(COMPILE.c) $(srcdir)/vsprintf.c $(OUTPUT_OPTION)
 
-./waitpid.o: $(srcdir)/waitpid.c config.h
+./waitpid.o: $(srcdir)/waitpid.c config.h $(INCDIR)/ansidecl.h
 	if [ x"$(PICFLAG)" != x ]; then \
 	  $(COMPILE.c) $(PICFLAG) $(srcdir)/waitpid.c -o pic/$@; \
 	else true; fi
diff --git a/libiberty/config.in b/libiberty/config.in
index ad71e76c09a2..d50aff9895e5 100644
--- a/libiberty/config.in
+++ b/libiberty/config.in
@@ -151,6 +151,9 @@
 /* Define to 1 if you have the <stdint.h> header file. */
 #undef HAVE_STDINT_H
 
+/* Define to 1 if you have the <stdio_ext.h> header file. */
+#undef HAVE_STDIO_EXT_H
+
 /* Define to 1 if you have the <stdlib.h> header file. */
 #undef HAVE_STDLIB_H
 
@@ -322,6 +325,9 @@
 /* Define if you have the _system_configuration variable. */
 #undef HAVE__SYSTEM_CONFIGURATION
 
+/* Define to 1 if you have the `__fsetlocking' function. */
+#undef HAVE___FSETLOCKING
+
 /* Define if the host machine stores words of multi-word integers in
    big-endian order. */
 #undef HOST_WORDS_BIG_ENDIAN
diff --git a/libiberty/configure b/libiberty/configure
index 3aa3963ea198..adc096285665 100755
--- a/libiberty/configure
+++ b/libiberty/configure
@@ -3540,7 +3540,8 @@ host_makefile_frag=${frag}
 
 
 
-for ac_header in sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h
+
+for ac_header in sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h
 do
 as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh`
 echo "$as_me:$LINENO: checking for $ac_header" >&5
@@ -4818,7 +4819,7 @@ vars="sys_errlist sys_nerr sys_siglist"
 
 checkfuncs="getrusage on_exit psignal strerror strsignal sysconf times sbrk gettimeofday"
 checkfuncs="$checkfuncs realpath canonicalize_file_name pstat_getstatic pstat_getdynamic sysmp"
-checkfuncs="$checkfuncs getsysinfo table sysctl wait3 wait4"
+checkfuncs="$checkfuncs getsysinfo table sysctl wait3 wait4 __fsetlocking"
 
 # These are neither executed nor required, but they help keep
 # autoheader happy without adding a bunch of text to acconfig.h.
@@ -4888,6 +4889,7 @@ if test "x" = "y"; then
 
 
 
+
 
 
 for ac_func in asprintf atexit basename bcmp bcopy bsearch bzero calloc clock \
@@ -4898,7 +4900,7 @@ for ac_func in asprintf atexit basename bcmp bcopy bsearch bzero calloc clock \
   vsprintf waitpid getrusage on_exit psignal strerror strsignal \
   sysconf times sbrk gettimeofday ffs snprintf vsnprintf \
   pstat_getstatic pstat_getdynamic sysmp getsysinfo table sysctl wait3 wait4 \
-  realpath canonicalize_file_name
+  realpath canonicalize_file_name __fsetlocking
 do
 as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
 echo "$as_me:$LINENO: checking for $ac_func" >&5
diff --git a/libiberty/configure.ac b/libiberty/configure.ac
index 9f4749139062..cd31f6a1a595 100644
--- a/libiberty/configure.ac
+++ b/libiberty/configure.ac
@@ -159,7 +159,7 @@ AC_SUBST_FILE(host_makefile_frag)
 # It's OK to check for header files.  Although the compiler may not be
 # able to link anything, it had better be able to at least compile
 # something.
-AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h)
+AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h)
 AC_HEADER_SYS_WAIT
 AC_HEADER_TIME
 
@@ -268,7 +268,7 @@ vars="sys_errlist sys_nerr sys_siglist"
 
 checkfuncs="getrusage on_exit psignal strerror strsignal sysconf times sbrk gettimeofday"
 checkfuncs="$checkfuncs realpath canonicalize_file_name pstat_getstatic pstat_getdynamic sysmp"
-checkfuncs="$checkfuncs getsysinfo table sysctl wait3 wait4"
+checkfuncs="$checkfuncs getsysinfo table sysctl wait3 wait4 __fsetlocking"
 
 # These are neither executed nor required, but they help keep
 # autoheader happy without adding a bunch of text to acconfig.h.
@@ -281,7 +281,7 @@ if test "x" = "y"; then
   vsprintf waitpid getrusage on_exit psignal strerror strsignal \
   sysconf times sbrk gettimeofday ffs snprintf vsnprintf \
   pstat_getstatic pstat_getdynamic sysmp getsysinfo table sysctl wait3 wait4 \
-  realpath canonicalize_file_name)
+  realpath canonicalize_file_name __fsetlocking)
   AC_DEFINE(HAVE_SYS_ERRLIST, 1, [Define if you have the sys_errlist variable.])
   AC_DEFINE(HAVE_SYS_NERR,    1, [Define if you have the sys_nerr variable.])
   AC_DEFINE(HAVE_SYS_SIGLIST, 1, [Define if you have the sys_siglist variable.])
diff --git a/libiberty/fopen_unlocked.c b/libiberty/fopen_unlocked.c
new file mode 100644
index 000000000000..b193dfd90818
--- /dev/null
+++ b/libiberty/fopen_unlocked.c
@@ -0,0 +1,93 @@
+/* Implement fopen_unlocked and related functions.
+   Copyright (C) 2005 Free Software Foundation, Inc.
+   Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
+
+This file is part of the libiberty library.
+Libiberty is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public
+License as published by the Free Software Foundation; either
+version 2 of the License, or (at your option) any later version.
+
+Libiberty is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with libiberty; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+/*
+
+@deftypefn Extension FILE * fopen_unlocked (const char *@var{path}, const char * @var{mode})
+
+Opens and returns a @code{FILE} pointer via @code{fopen}.  If the
+operating system supports it, ensure that the stream is setup to avoid
+any multi-threaded locking.  Otherwise return the @code{FILE} pointer
+unchanged.
+
+@end deftypefn
+
+@deftypefn Extension FILE * fdopen_unlocked (int @var{fildes}, const char * @var{mode})
+
+Opens and returns a @code{FILE} pointer via @code{fdopen}.  If the
+operating system supports it, ensure that the stream is setup to avoid
+any multi-threaded locking.  Otherwise return the @code{FILE} pointer
+unchanged.
+
+@end deftypefn
+
+@deftypefn Extension FILE * freopen_unlocked (const char * @var{path}, const char * @var{mode}, FILE * @var{stream})
+
+Opens and returns a @code{FILE} pointer via @code{freopen}.  If the
+operating system supports it, ensure that the stream is setup to avoid
+any multi-threaded locking.  Otherwise return the @code{FILE} pointer
+unchanged.
+
+@end deftypefn
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#ifdef HAVE_STDIO_EXT_H
+#include <stdio_ext.h>
+#endif
+
+#include "libiberty.h"
+
+FILE *
+fopen_unlocked (const char *path, const char *mode)		
+{
+  FILE *const fp = fopen (path, mode);
+#if defined(HAVE___FSETLOCKING) && defined(FSETLOCKING_BYCALLER)
+  if (fp)
+    __fsetlocking (fp, FSETLOCKING_BYCALLER);
+#endif
+  return fp;
+}
+
+FILE *
+fdopen_unlocked (int fildes, const char *mode)
+{
+  FILE *const fp = fdopen (fildes, mode);
+#if defined(HAVE___FSETLOCKING) && defined(FSETLOCKING_BYCALLER)
+  if (fp)
+    __fsetlocking (fp, FSETLOCKING_BYCALLER);
+#endif
+  return fp;
+}
+
+FILE *
+freopen_unlocked (const char *path, const char *mode, FILE *stream)
+{
+  FILE *const fp = freopen (path, mode, stream);
+#if defined(HAVE___FSETLOCKING) && defined(FSETLOCKING_BYCALLER)
+  if (fp)
+    __fsetlocking (fp, FSETLOCKING_BYCALLER);
+#endif
+  return fp;
+}
diff --git a/libiberty/functions.texi b/libiberty/functions.texi
index 75f98243a027..47d40ec49f7b 100644
--- a/libiberty/functions.texi
+++ b/libiberty/functions.texi
@@ -362,6 +362,16 @@ and inode numbers.
 
 @end deftypefn
 
+@c fopen_unlocked.c:32
+@deftypefn Extension FILE * fdopen_unlocked (int @var{fildes}, const char * @var{mode})
+
+Opens and returns a @code{FILE} pointer via @code{fdopen}.  If the
+operating system supports it, ensure that the stream is setup to avoid
+any multi-threaded locking.  Otherwise return the @code{FILE} pointer
+unchanged.
+
+@end deftypefn
+
 @c ffs.c:3
 @deftypefn Supplemental int ffs (int @var{valu})
 
@@ -421,6 +431,16 @@ Ignores case when performing the comparison.
 
 @end deftypefn
 
+@c fopen_unlocked.c:23
+@deftypefn Extension FILE * fopen_unlocked (const char *@var{path}, const char * @var{mode})
+
+Opens and returns a @code{FILE} pointer via @code{fopen}.  If the
+operating system supports it, ensure that the stream is setup to avoid
+any multi-threaded locking.  Otherwise return the @code{FILE} pointer
+unchanged.
+
+@end deftypefn
+
 @c argv.c:94
 @deftypefn Extension void freeargv (char **@var{vector})
 
@@ -431,6 +451,16 @@ itself.
 
 @end deftypefn
 
+@c fopen_unlocked.c:41
+@deftypefn Extension FILE * freopen_unlocked (const char * @var{path}, const char * @var{mode}, FILE * @var{stream})
+
+Opens and returns a @code{FILE} pointer via @code{freopen}.  If the
+operating system supports it, ensure that the stream is setup to avoid
+any multi-threaded locking.  Otherwise return the @code{FILE} pointer
+unchanged.
+
+@end deftypefn
+
 @c getruntime.c:82
 @deftypefn Replacement long get_run_time (void)
 
-- 
GitLab