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 04/20] unlocked stdio extensions



Index: libc/stdio/fflush.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fflush.c,v
retrieving revision 1.20
diff -u -p -r1.20 fflush.c
--- libc/stdio/fflush.c	17 Jan 2014 10:55:31 -0000	1.20
+++ libc/stdio/fflush.c	17 Dec 2014 18:26:48 -0000
@@ -17,19 +17,32 @@
 
 /*
 FUNCTION
-<<fflush>>---flush buffered file output
+<<fflush>>, <<fflush_unlocked>>---flush buffered file output
 
 INDEX
 	fflush
 INDEX
+	fflush_unlocked
+INDEX
 	_fflush_r
+INDEX
+	_fflush_unlocked_r
 
 ANSI_SYNOPSIS
 	#include <stdio.h>
 	int fflush(FILE *<[fp]>);
 
+	#define _BSD_SOURCE
+	#include <stdio.h>
+	int fflush_unlocked(FILE *<[fp]>);
+
+	#include <stdio.h>
 	int _fflush_r(struct _reent *<[reent]>, FILE *<[fp]>);
 
+	#define _BSD_SOURCE
+	#include <stdio.h>
+	int _fflush_unlocked_r(struct _reent *<[reent]>, FILE *<[fp]>);
+
 DESCRIPTION
 The <<stdio>> output functions can buffer output before delivering it
 to the host system, in order to minimize the overhead of system calls.
@@ -45,9 +58,18 @@ descriptor, set the position of the file
 unread byte, useful for obeying POSIX semantics when ending a process
 without consuming all input from the stream.
 
-The alternate function <<_fflush_r>> is a reentrant version, where the
-extra argument <[reent]> is a pointer to a reentrancy structure, and
-<[fp]> must not be NULL.
+<<fflush_unlocked>> is a non-thread-safe version of <<fflush>>.
+<<fflush_unlocked>> may only safely be used within a scope
+protected by flockfile() (or ftrylockfile()) and funlockfile().  This
+function may safely be used in a multi-threaded program if and only
+if they are called while the invoking thread owns the (FILE *)
+object, as is the case after a successful call to the flockfile() or
+ftrylockfile() functions.  If threads are disabled, then
+<<fflush_unlocked>> is equivalent to <<fflush>>.
+
+The alternate functions <<_fflush_r>> and <<_fflush_unlocked_r>> are
+reentrant versions, where the extra argument <[reent]> is a pointer to
+a reentrancy structure, and <[fp]> must not be NULL.
 
 RETURNS
 <<fflush>> returns <<0>> unless it encounters a write error; in that
@@ -57,6 +79,8 @@ PORTABILITY
 ANSI C requires <<fflush>>.  The behavior on input streams is only
 specified by POSIX, and not all implementations follow POSIX rules.
 
+<<fflush_unlocked>> is a BSD extension also provided by GNU libc.
+
 No supporting OS subroutines are required.
 */
 
Index: libc/stdio/fflush_u.c
===================================================================
RCS file: libc/stdio/fflush_u.c
diff -N libc/stdio/fflush_u.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libc/stdio/fflush_u.c	17 Dec 2014 18:26:48 -0000
@@ -0,0 +1,75 @@
+/*
+ * 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.
+ */
+
+#include <_ansi.h>
+#include <stdio.h>
+#include <errno.h>
+#include "local.h"
+
+int
+_DEFUN(_fflush_unlocked_r, (ptr, fp),
+       struct _reent *ptr _AND
+       register FILE * fp)
+{
+  int ret;
+
+#ifdef _REENT_SMALL
+  /* For REENT_SMALL platforms, it is possible we are being
+     called for the first time on a std stream.  This std
+     stream can belong to a reentrant struct that is not
+     _REENT.  If CHECK_INIT gets called below based on _REENT,
+     we will end up changing said file pointers to the equivalent
+     std stream off of _REENT.  This causes unexpected behavior if
+     there is any data to flush on the _REENT std stream.  There
+     are two alternatives to fix this:  1) make a reentrant fflush
+     or 2) simply recognize that this file has nothing to flush
+     and return immediately before performing a CHECK_INIT.  Choice
+     2 is implemented here due to its simplicity.  */
+  if (fp->_bf._base == NULL)
+    return 0;
+#endif /* _REENT_SMALL  */
+
+  CHECK_INIT (ptr, fp);
+
+  if (!fp->_flags)
+    return 0;
+
+  return __sflush_r (ptr, fp);
+}
+
+#ifndef _REENT_ONLY
+
+int
+_DEFUN(fflush_unlocked, (fp),
+       register FILE * fp)
+{
+  if (fp == NULL)
+    return _fwalk_reent (_GLOBAL_REENT, _fflush_unlocked_r);
+
+  return _fflush_unlocked_r (_REENT, fp);
+}
+
+#endif /* _REENT_ONLY */

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