This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: [PATCH 05/20] unlocked stdio extensions
- From: Yaakov Selkowitz <yselkowi at redhat dot com>
- To: newlib at sourceware dot org
- Date: Wed, 17 Dec 2014 12:34:12 -0600
- Subject: Re: [PATCH 05/20] unlocked stdio extensions
- Authentication-results: sourceware.org; auth=none
- References: <548F3554 dot 5010204 at redhat dot com> <20141216112826 dot GJ10824 at calimero dot vinschen dot de> <5491C810 dot 6080305 at redhat dot com> <20141217182008 dot GX10824 at calimero dot vinschen dot de>
Index: libc/stdio/fgetc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fgetc.c,v
retrieving revision 1.10
diff -u -p -r1.10 fgetc.c
--- libc/stdio/fgetc.c 29 Apr 2013 21:06:23 -0000 1.10
+++ libc/stdio/fgetc.c 17 Dec 2014 18:26:50 -0000
@@ -17,30 +17,53 @@
/*
FUNCTION
-<<fgetc>>---get a character from a file or stream
+<<fgetc>>, <<fgetc_unlocked>>---get a character from a file or stream
INDEX
fgetc
INDEX
+ fgetc_unlocked
+INDEX
_fgetc_r
+INDEX
+ _fgetc_unlocked_r
ANSI_SYNOPSIS
#include <stdio.h>
int fgetc(FILE *<[fp]>);
+ #define _BSD_SOURCE
+ #include <stdio.h>
+ int fgetc_unlocked(FILE *<[fp]>);
+
#include <stdio.h>
int _fgetc_r(struct _reent *<[ptr]>, FILE *<[fp]>);
+ #define _BSD_SOURCE
+ #include <stdio.h>
+ int _fgetc_unlocked_r(struct _reent *<[ptr]>, FILE *<[fp]>);
+
TRAD_SYNOPSIS
#include <stdio.h>
int fgetc(<[fp]>)
FILE *<[fp]>;
+ #define _BSD_SOURCE
+ #include <stdio.h>
+ int fgetc_unlocked(<[fp]>)
+ FILE *<[fp]>;
+
#include <stdio.h>
int _fgetc_r(<[ptr]>, <[fp]>)
struct _reent *<[ptr]>;
FILE *<[fp]>;
+ #define _BSD_SOURCE
+ #include <stdio.h>
+ int _fgetc_unlocked_r(<[ptr]>, <[fp]>)
+ struct _reent *<[ptr]>;
+ FILE *<[fp]>;
+
DESCRIPTION
Use <<fgetc>> to get the next single character from the file or stream
identified by <[fp]>. As a side effect, <<fgetc>> advances the file's
@@ -48,9 +71,18 @@ current position indicator.
For a macro version of this function, see <<getc>>.
-The function <<_fgetc_r>> is simply a reentrant version of
-<<fgetc>> that is passed the additional reentrant structure
-pointer argument: <[ptr]>.
+<<fgetc_unlocked>> is a non-thread-safe version of <<fgetc>>.
+<<fgetc_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
+<<fgetc_unlocked>> is equivalent to <<fgetc>>.
+
+The functions <<_fgetc_r>> and <<_fgetc_unlocked_r>> are simply reentrant
+versions that are passed the additional reentrant structure pointer
+argument: <[ptr]>.
RETURNS
The next character (read as an <<unsigned char>>, and cast to
@@ -63,6 +95,8 @@ using the <<ferror>> and <<feof>> functi
PORTABILITY
ANSI C requires <<fgetc>>.
+<<fgetc_unlocked>> is a BSD extension also provided by GNU libc.
+
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
*/
Index: libc/stdio/fgetc_u.c
===================================================================
RCS file: libc/stdio/fgetc_u.c
diff -N libc/stdio/fgetc_u.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ libc/stdio/fgetc_u.c 17 Dec 2014 18:26:50 -0000
@@ -0,0 +1,56 @@
+/*
+ * 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 "local.h"
+
+int
+_DEFUN(_fgetc_unlocked_r, (ptr, fp),
+ struct _reent * ptr _AND
+ FILE * fp)
+{
+ CHECK_INIT(ptr, fp);
+ return __sgetc_r (ptr, fp);
+}
+
+#ifndef _REENT_ONLY
+
+int
+_DEFUN(fgetc_unlocked, (fp),
+ FILE * fp)
+{
+#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
+ struct _reent *reent = _REENT;
+
+ CHECK_INIT(reent, fp);
+ return __sgetc_r (reent, fp);
+#else
+ return _fgetc_unlocked_r (_REENT, fp);
+#endif
+}
+
+#endif /* !_REENT_ONLY */