This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] __fsetlocking


On 2014-12-15 09:11, Corinna Vinschen wrote:
On Dec 11 12:08, Yaakov Selkowitz wrote:
Another of the stdio_ext functions from Solaris and glibc:

https://docs.oracle.com/cd/E19683-01/816-0213/6m6ne37vm/index.html
http://man7.org/linux/man-pages/man3/stdio_ext.3.html

Patch attached.

Thanks.  This patch introduces a problem for single thread targets.
While _newlib_flockfile_start and friends where no-ops before (given
that _flockfile/_funlockfile are no-ops), they now introduce a redundant
check for a bit in fp->_flags2.  This check *may* be removed by compiler
optimization, but we shouldn't rely on that.  What this patch needs is a
new no-op-like definition of _newlib_flockfile_start and friends for
single thread targets.

Revised patch attached.

--
Yaakov Selkowitz
Associate Software Engineer, ARM
Red Hat, Inc.
2014-12-15  Yaakov Selkowitz  <yselkowi@...>

	* libc/include/stdio.h (__SNLK): Define.
	* libc/include/stdio_ext.h (FSETLOCKING_QUERY, FSETLOCKING_INTERNAL,
	FSETLOCKING_BYCALLER): Define.
	(__fsetlocking): Declare.
	* libc/stdio/Makefile.am: Build fsetlocking.c.
	* libc/stdio/Makefile.in: Regenerate.
	* libc/stdio/fsetlocking.c: New file.
	* libc/stdio/local.h (_newlib_flockfile_start): Make _flockfile
	call dependent on __SNLK flag.
	(_newlib_flockfile_exit, _newlib_flockfile_end): Ditto for
	_funlockfile calls.
	Define all locking macros as empty if __SINGLE_THREAD__.
	* libc/stdio/stdio.tex: Include fsetlocking.def.

Index: libc/stdio/Makefile.am
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/Makefile.am,v
retrieving revision 1.36
diff -u -p -r1.36 Makefile.am
--- libc/stdio/Makefile.am	11 Dec 2014 16:53:32 -0000	1.36
+++ libc/stdio/Makefile.am	15 Dec 2014 16:19:09 -0000
@@ -146,6 +146,7 @@ ELIX_4_SOURCES = \
 	fpurge.c		\
 	fputwc.c		\
 	fputws.c		\
+	fsetlocking.c		\
 	funopen.c		\
 	fwide.c			\
 	fwprintf.c		\
@@ -357,6 +358,7 @@ CHEWOUT_FILES = \
 	fread.def		\
 	freopen.def		\
 	fseek.def		\
+	fsetlocking.def		\
 	fsetpos.def		\
 	ftell.def		\
 	funopen.def		\
@@ -433,6 +435,7 @@ $(lpfx)fputws.$(oext): local.h fvwrite.h
 $(lpfx)fread.$(oext): local.h
 $(lpfx)freopen.$(oext): local.h
 $(lpfx)fseek.$(oext): local.h
+$(lpfx)fsetlocking.$(oext): local.h
 $(lpfx)ftell.$(oext): local.h
 $(lpfx)funopen.$(oext): local.h
 $(lpfx)fvwrite.$(oext): local.h fvwrite.h
Index: libc/stdio/Makefile.in
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/Makefile.in,v
retrieving revision 1.57
diff -u -p -r1.57 Makefile.in
--- libc/stdio/Makefile.in	11 Dec 2014 16:53:32 -0000	1.57
+++ libc/stdio/Makefile.in	15 Dec 2014 16:19:09 -0000
@@ -143,6 +143,7 @@ am__objects_2 = $(am__objects_1) lib_a-c
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	lib_a-fpurge.$(OBJEXT) \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	lib_a-fputwc.$(OBJEXT) \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	lib_a-fputws.$(OBJEXT) \
+@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	lib_a-fsetlocking.$(OBJEXT) \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	lib_a-funopen.$(OBJEXT) \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	lib_a-fwide.$(OBJEXT) \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	lib_a-fwprintf.$(OBJEXT) \
@@ -206,6 +207,7 @@ am__objects_8 = $(am__objects_7) clearer
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fpurge.lo \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fputwc.lo \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fputws.lo \
+@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fsetlocking.lo \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	funopen.lo \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fwide.lo \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fwprintf.lo \
@@ -514,6 +516,7 @@ GENERAL_SOURCES = \
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fpurge.c		\
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fputwc.c		\
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fputws.c		\
+@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fsetlocking.c		\
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	funopen.c		\
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fwide.c			\
 @ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@	fwprintf.c		\
@@ -631,6 +634,7 @@ CHEWOUT_FILES = \
 	fread.def		\
 	freopen.def		\
 	fseek.def		\
+	fsetlocking.def		\
 	fsetpos.def		\
 	ftell.def		\
 	funopen.def		\
@@ -1355,6 +1359,12 @@ lib_a-fputws.o: fputws.c
 lib_a-fputws.obj: fputws.c
 	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fputws.obj `if test -f 'fputws.c'; then $(CYGPATH_W) 'fputws.c'; else $(CYGPATH_W) '$(srcdir)/fputws.c'; fi`
 
+lib_a-fsetlocking.o: fsetlocking.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fsetlocking.o `test -f 'fsetlocking.c' || echo '$(srcdir)/'`fsetlocking.c
+
+lib_a-fsetlocking.obj: fsetlocking.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fsetlocking.obj `if test -f 'fsetlocking.c'; then $(CYGPATH_W) 'fsetlocking.c'; else $(CYGPATH_W) '$(srcdir)/fsetlocking.c'; fi`
+
 lib_a-funopen.o: funopen.c
 	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-funopen.o `test -f 'funopen.c' || echo '$(srcdir)/'`funopen.c
 
@@ -1772,6 +1782,7 @@ $(lpfx)fputws.$(oext): local.h fvwrite.h
 $(lpfx)fread.$(oext): local.h
 $(lpfx)freopen.$(oext): local.h
 $(lpfx)fseek.$(oext): local.h
+$(lpfx)fsetlocking.$(oext): local.h
 $(lpfx)ftell.$(oext): local.h
 $(lpfx)funopen.$(oext): local.h
 $(lpfx)fvwrite.$(oext): local.h fvwrite.h
Index: libc/stdio/fsetlocking.c
===================================================================
RCS file: libc/stdio/fsetlocking.c
diff -N libc/stdio/fsetlocking.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libc/stdio/fsetlocking.c	15 Dec 2014 16:19:09 -0000
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+/*
+FUNCTION
+<<__fsetlocking>>---set or query locking mode on FILE stream
+
+INDEX
+	__fsetlocking
+
+ANSI_SYNOPSIS
+	#include <stdio.h>
+	#include <stdio_ext.h>
+	int __fsetlocking(FILE *<[fp]>, int <[type]>);
+
+DESCRIPTION
+This function sets how the stdio functions handle locking of FILE <[fp]>.
+The following values describe <[type]>:
+
+<<FSETLOCKING_INTERNAL>> is the default state, where stdio functions
+automatically lock and unlock the stream.
+
+<<FSETLOCKING_BYCALLER>> means that automatic locking in stdio functions
+is disabled. Applications which set this take all responsibility for file
+locking themselves.
+
+<<FSETLOCKING_QUERY>> returns the current locking mode without changing it.
+
+RETURNS
+<<__fsetlocking>> returns the current locking mode of <[fp]>.
+
+PORTABILITY
+This function originates from Solaris and is also provided by GNU libc.
+
+No supporting OS subroutines are required.
+*/
+
+#ifndef __rtems__
+
+#include <_ansi.h>
+#include <stdio.h>
+#include <stdio_ext.h>
+#include "local.h"
+
+int
+_DEFUN(__fsetlocking, (fp, type),
+       FILE * fp _AND
+       int type)
+{
+  int result;
+  CHECK_INIT(_REENT, fp);
+  result = (fp->_flags2 & __SNLK) ? FSETLOCKING_BYCALLER : FSETLOCKING_INTERNAL;
+  switch (type)
+    {
+    case FSETLOCKING_BYCALLER:
+      fp->_flags2 |= __SNLK;
+      break;
+    case FSETLOCKING_INTERNAL:
+      fp->_flags2 &= ~__SNLK;
+      break;
+    case FSETLOCKING_QUERY:
+    default:
+      break;
+    }
+  return result;
+}
+
+#endif /* __rtems__ */
Index: libc/stdio/local.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/local.h,v
retrieving revision 1.40
diff -u -p -r1.40 local.h
--- libc/stdio/local.h	4 Apr 2014 21:40:58 -0000	1.40
+++ libc/stdio/local.h	15 Dec 2014 16:19:09 -0000
@@ -68,16 +68,19 @@
 	{ \
 	  int __oldfpcancel; \
 	  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &__oldfpcancel); \
-	  _flockfile (_fp)
+	  if (!(_fp->_flags2 & __SNLK)) \
+	    _flockfile (_fp)
 
 /* Exit from a stream oriented critical section prematurely: */
 # define _newlib_flockfile_exit(_fp) \
-	  _funlockfile (_fp); \
+	  if (!(_fp->_flags2 & __SNLK)) \
+	    _funlockfile (_fp); \
 	  pthread_setcancelstate (__oldfpcancel, &__oldfpcancel);
 
 /* End a stream oriented critical section: */
 # define _newlib_flockfile_end(_fp) \
-	  _funlockfile (_fp); \
+	  if (!(_fp->_flags2 & __SNLK)) \
+	    _funlockfile (_fp); \
 	  pthread_setcancelstate (__oldfpcancel, &__oldfpcancel); \
 	}
 
@@ -99,17 +102,20 @@
 	  pthread_setcancelstate (__oldsfpcancel, &__oldsfpcancel); \
 	}
 
-#else /* !_STDIO_WITH_THREAD_CANCELLATION_SUPPORT */
+#elif !defined(__SINGLE_THREAD__) /* !_STDIO_WITH_THREAD_CANCELLATION_SUPPORT */
 
 # define _newlib_flockfile_start(_fp) \
 	{ \
-		_flockfile(_fp)
+		if (!(_fp->_flags2 & __SNLK)) \
+		  _flockfile (_fp)
 
 # define _newlib_flockfile_exit(_fp) \
-		_funlockfile(_fp); \
+		if (!(_fp->_flags2 & __SNLK)) \
+		  _funlockfile(_fp); \
 
 # define _newlib_flockfile_end(_fp) \
-		_funlockfile(_fp); \
+		if (!(_fp->_flags2 & __SNLK)) \
+		  _funlockfile(_fp); \
 	}
 
 # define _newlib_sfp_lock_start() \
@@ -123,6 +129,15 @@
 		__sfp_lock_release (); \
 	}
 
+#else /* __SINGLE_THREAD__ */
+
+# define _newlib_flockfile_start(_fp)
+# define _newlib_flockfile_exit(_fp)
+# define _newlib_flockfile_end(_fp)
+# define _newlib_sfp_lock_start()
+# define _newlib_sfp_lock_exit()
+# define _newlib_sfp_lock_end()
+
 #endif /* _STDIO_WITH_THREAD_CANCELLATION_SUPPORT */
 
 extern u_char *_EXFUN(__sccl, (char *, u_char *fmt));
Index: libc/stdio/stdio.tex
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/stdio.tex,v
retrieving revision 1.16
diff -u -p -r1.16 stdio.tex
--- libc/stdio/stdio.tex	11 Dec 2014 16:53:32 -0000	1.16
+++ libc/stdio/stdio.tex	15 Dec 2014 16:19:10 -0000
@@ -178,6 +178,9 @@ structure.
 @include stdio/fseek.def
 
 @page
+@include stdio/fsetlocking.def
+
+@page
 @include stdio/fsetpos.def
 
 @page
Index: libc/include/stdio.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/stdio.h,v
retrieving revision 1.72
diff -u -p -r1.72 stdio.h
--- libc/include/stdio.h	11 Dec 2014 17:42:19 -0000	1.72
+++ libc/include/stdio.h	15 Dec 2014 16:19:10 -0000
@@ -83,6 +83,7 @@ typedef _fpos64_t fpos64_t;
 #define	__SL64	0x8000		/* is 64-bit offset large file */
 
 /* _flags2 flags */
+#define	__SNLK  0x0001		/* stdio functions do not lock streams themselves */
 #define	__SWID	0x2000		/* true => stream orientation wide, false => byte, only valid if __SORD in _flags is true */
 
 /*
Index: libc/include/stdio_ext.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/stdio_ext.h,v
retrieving revision 1.4
diff -u -p -r1.4 stdio_ext.h
--- libc/include/stdio_ext.h	11 Dec 2014 16:53:32 -0000	1.4
+++ libc/include/stdio_ext.h	15 Dec 2014 16:19:10 -0000
@@ -13,14 +13,18 @@
 
 #include <stdio.h>
 
+#define	FSETLOCKING_QUERY	0
+#define	FSETLOCKING_INTERNAL	1
+#define	FSETLOCKING_BYCALLER	2
+
 _BEGIN_STD_C
 
 void	 _EXFUN(__fpurge,(FILE *));
+int	 _EXFUN(__fsetlocking,(FILE *, int));
 
 /* TODO:
 
    void _flushlbf (void);
-   int __fsetlocking (FILE *__fp, int __type);
 */
 
 #ifdef  __GNUC__

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]