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] Secure newlib stdio functions against thread cancellation (was Re: newlib stdio and thread cancellation)


On 05/29/2012 01:25 PM, Corinna Vinschen wrote:
On May 29 09:27, Corinna Vinschen wrote:
On May 25 19:31, Steven Abner wrote:

On May 23, 2012, at 9:08 AM, Corinna Vinschen wrote:


Consider a scenario where multiple threads call printf.  The vfprintf
function is secured against concurrent usage of the same descriptor
by calling _flockfile/_funlockfile.

Corinna, I have a question. I am not sure why concurrent or serialization of parsing is needed. Wouldn't that defeat the purpose of threading? I can see actual syscall write locking a descriptor, but what if 3 threads use vfprintf on one descriptor, does it matter which thread writes first, or in the case of one being canceled, that only 2 write in a certain sequence? I ask in earnest, because I viewed from a different position. I saw vfprintf as a userland function which should perform its operation of parsing and once done, to send off for writing.

vfprintf is just one example of a whole bunch of stdio functions calling _flockfile/_funlockfile. The idea of this lock is not to avoid multiple threads writing concurrently on a stream in the first place, but to secure the FILE structure of a stream against concurrent, conflicting manipulation. Without this, the stdio functions are not thread-safe, and per POSIX they are supposed to be.

The problem we still have to fix is that these functions, while being
generally thread-safe, are not safe against thread cancellation.  If a
thread holding a FILE lock is cancelled, the lock is not unlocked and
all subsequent I/O on this stream will hang trying to lock the stream.

Below is a patch to fix the problem. I had a look into the NetBSD code and it turned out that NetBSD does not push and pop thread cleanup handlers, but rather it recursively disables thread cancellation for the duration of the stdio function.

The below patch accomplishes thread cancellation safety the same way.  I
defined new macros to encapsulate the _flockfile/_funlockfile calls, as
well as the __sfp_lock_acquire/__sfp_lock_release calls, since they
suffer the same problem.  Then I replaced the calls throughout without
changing the structure of the code.

The macros are defined differently depending on the values of
__SINGLE_THREAD__ and _POSIX_THREADS.  Only if both are defined, the
pthread cancel state is changed, otherwise the macros are defined to
just call the usual lock/unlock functions.

The only functions left as an excercise are the __fp_lock_all and
__fp_unlock_all functions.  In case of using them, the *caller* has to
make sure that it handles thread cancellation correctly.  However,
__fp_lock_all/__fp_unlock_all are not used in newlib anyway, and the
only consumer I know of is Cygwin's fork implementation.


Jeff, is that patch ok to apply?



Looks fine. The comments for the macros could add a brief clause that the start macro and end macros add starting and closing braces so must appear at the same nesting level. The exit macros are for deeper nesting levels such as failing clauses and require an earlier start macro be used.


-- Jeff J.


Thanks, Corinna


* libc/stdio/local.h (_newlib_flockfile_start): New macro to secure stream related critical section against thread cancellation. (_newlib_flockfile_exit): Ditto. (_newlib_sfp_lock_end): Ditto. (_newlib_sfp_lock_start): Ditto for the list of streams. (_newlib_sfp_lock_exit): Ditto. (_newlib_sfp_lock_end): Ditto. Use aforementioned macros in place of _flockfile/_funlockfile and __sfp_lock_acquire/__sfp_lock_release throughout the code.


Index: libc/stdio/local.h =================================================================== RCS file: /cvs/src/src/newlib/libc/stdio/local.h,v retrieving revision 1.31 diff -u -p -r1.31 local.h --- libc/stdio/local.h 14 Jun 2011 03:56:05 -0000 1.31 +++ libc/stdio/local.h 29 May 2012 17:18:38 -0000 @@ -32,6 +32,70 @@ # include<io.h> #endif

+#if !defined (__SINGLE_THREAD__)&&  defined (_POSIX_THREADS)
+#include<pthread.h>
+
+/* Start a stream oriented critical section: */
+# define _newlib_flockfile_start(_fp) \
+	{ \
+		int __oldcancel; \
+		pthread_setcancelstate (PTHREAD_CANCEL_DISABLE,&__oldcancel); \
+		_flockfile (_fp)
+
+/* Exit from a stream oriented critical section prematurely: */
+# define _newlib_flockfile_exit(_fp) \
+		_funlockfile (_fp); \
+		pthread_setcancelstate (__oldcancel,&__oldcancel);
+
+/* End a stream oriented critical section: */
+# define _newlib_flockfile_end(_fp) \
+		_funlockfile (_fp); \
+		pthread_setcancelstate (__oldcancel,&__oldcancel); \
+	}
+
+/* Start a stream list oriented critical section: */
+# define _newlib_sfp_lock_start() \
+	{ \
+		int __oldcancel; \
+		pthread_setcancelstate (PTHREAD_CANCEL_DISABLE,&__oldcancel); \
+		__sfp_lock_acquire ()
+
+/* Exit from a stream list oriented critical section prematurely: */
+# define _newlib_sfp_lock_exit() \
+		__sfp_lock_release (); \
+		pthread_setcancelstate (__oldcancel,&__oldcancel);
+
+/* End a stream list oriented critical section: */
+# define _newlib_sfp_lock_end() \
+		__sfp_lock_release (); \
+		pthread_setcancelstate (__oldcancel,&__oldcancel); \
+	}
+
+#else /* __SINGLE_THREAD__ || !_POSIX_THREADS */
+
+# define _newlib_flockfile_start(_fp) \
+	{ \
+		_flockfile(_fp)
+
+# define _newlib_flockfile_exit(_fp) \
+		_funlockfile(_fp); \
+
+# define _newlib_flockfile_end(_fp) \
+		_funlockfile(_fp); \
+	}
+
+# define _newlib_sfp_lock_start() \
+	{ \
+		__sfp_lock_acquire ()
+
+# define _newlib_sfp_lock_end() \
+		__sfp_lock_release ();
+
+# define _newlib_sfp_lock_end() \
+		__sfp_lock_release (); \
+	}
+
+#endif /* !__SINGLE_THREAD__&&  _POSIX_THREADS */

  extern u_char *_EXFUN(__sccl, (char *, u_char *fmt));
  extern int    _EXFUN(__svfscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
Index: libc/stdio/clearerr.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/clearerr.c,v
retrieving revision 1.5
diff -u -p -r1.5 clearerr.c
--- libc/stdio/clearerr.c	26 Sep 2006 21:22:19 -0000	1.5
+++ libc/stdio/clearerr.c	29 May 2012 17:18:37 -0000
@@ -65,7 +65,7 @@ _DEFUN(clearerr, (fp),
         FILE * fp)
  {
    CHECK_INIT(_REENT, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    __sclearerr (fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
  }
Index: libc/stdio/fclose.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fclose.c,v
retrieving revision 1.16
diff -u -p -r1.16 fclose.c
--- libc/stdio/fclose.c	28 Jan 2011 10:49:11 -0000	1.16
+++ libc/stdio/fclose.c	29 May 2012 17:18:37 -0000
@@ -76,11 +76,11 @@ _DEFUN(_fclose_r, (rptr, fp),

CHECK_INIT (rptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    if (fp->_flags == 0)		/* not open! */
      {
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return (0);
      }
    /* Unconditionally flush to allow special handling for seekable read
@@ -95,14 +95,14 @@ _DEFUN(_fclose_r, (rptr, fp),
      FREEUB (rptr, fp);
    if (HASLB (fp))
      FREELB (rptr, fp);
-  __sfp_lock_acquire ();
+  _newlib_sfp_lock_start ();
    fp->_flags = 0;		/* release this FILE for reuse */
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
  #ifndef __SINGLE_THREAD__
    __lock_close_recursive (fp->_lock);
  #endif

-  __sfp_lock_release ();
+  _newlib_sfp_lock_end ();

    return (r);
  }
Index: libc/stdio/fdopen.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fdopen.c,v
retrieving revision 1.9
diff -u -p -r1.9 fdopen.c
--- libc/stdio/fdopen.c	31 Jul 2007 20:49:40 -0000	1.9
+++ libc/stdio/fdopen.c	29 May 2012 17:18:37 -0000
@@ -93,7 +93,7 @@ _DEFUN(_fdopen_r, (ptr, fd, mode),
    if ((fp = __sfp (ptr)) == 0)
      return 0;

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    fp->_flags = flags;
    /* POSIX recommends setting the O_APPEND bit on fd to match append
@@ -127,7 +127,7 @@ _DEFUN(_fdopen_r, (ptr, fd, mode),
      fp->_flags |= __SCLE;
  #endif

-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return fp;
  }

Index: libc/stdio/feof.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/feof.c,v
retrieving revision 1.5
diff -u -p -r1.5 feof.c
--- libc/stdio/feof.c	26 Sep 2006 21:22:19 -0000	1.5
+++ libc/stdio/feof.c	29 May 2012 17:18:37 -0000
@@ -58,8 +58,8 @@ _DEFUN(feof, (fp),
  {
    int result;
    CHECK_INIT(_REENT, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    result = __sfeof (fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  }
Index: libc/stdio/ferror.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/ferror.c,v
retrieving revision 1.5
diff -u -p -r1.5 ferror.c
--- libc/stdio/ferror.c	26 Sep 2006 21:22:19 -0000	1.5
+++ libc/stdio/ferror.c	29 May 2012 17:18:37 -0000
@@ -67,8 +67,8 @@ _DEFUN(ferror, (fp),
  {
    int result;
    CHECK_INIT(_REENT, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    result = __sferror (fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  }
Index: libc/stdio/fflush.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fflush.c,v
retrieving revision 1.16
diff -u -p -r1.16 fflush.c
--- libc/stdio/fflush.c	28 Jan 2011 10:49:11 -0000	1.16
+++ libc/stdio/fflush.c	29 May 2012 17:18:37 -0000
@@ -226,9 +226,9 @@ _DEFUN(_fflush_r, (ptr, fp),
    if (!fp->_flags)
      return 0;

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ret = __sflush_r (ptr, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return ret;
  }

Index: libc/stdio/fgetc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fgetc.c,v
retrieving revision 1.8
diff -u -p -r1.8 fgetc.c
--- libc/stdio/fgetc.c	28 Jan 2011 10:49:11 -0000	1.8
+++ libc/stdio/fgetc.c	29 May 2012 17:18:37 -0000
@@ -78,9 +78,9 @@ _DEFUN(_fgetc_r, (ptr, fp),
  {
    int result;
    CHECK_INIT(ptr, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    result = __sgetc_r (ptr, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  }

@@ -93,9 +93,9 @@ _DEFUN(fgetc, (fp),
  #if !defined(PREFER_SIZE_OVER_SPEED)&&  !defined(__OPTIMIZE_SIZE__)
    int result;
    CHECK_INIT(_REENT, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    result = __sgetc_r (_REENT, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  #else
    return _fgetc_r (_REENT, fp);
Index: libc/stdio/fgets.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fgets.c,v
retrieving revision 1.9
diff -u -p -r1.9 fgets.c
--- libc/stdio/fgets.c	28 Jan 2011 10:49:11 -0000	1.9
+++ libc/stdio/fgets.c	29 May 2012 17:18:37 -0000
@@ -98,7 +98,7 @@ _DEFUN(_fgets_r, (ptr, buf, n, fp),

CHECK_INIT(ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
  #ifdef __SCLE
    if (fp->_flags&  __SCLE)
      {
@@ -112,11 +112,11 @@ _DEFUN(_fgets_r, (ptr, buf, n, fp),
  	}
        if (c == EOF&&  s == buf)
          {
-          _funlockfile (fp);
+          _newlib_flockfile_exit (fp);
            return NULL;
          }
        *s = 0;
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return buf;
      }
  #endif
@@ -134,7 +134,7 @@ _DEFUN(_fgets_r, (ptr, buf, n, fp),
  	      /* EOF: stop with partial or no line */
  	      if (s == buf)
                  {
-                  _funlockfile (fp);
+                  _newlib_flockfile_exit (fp);
                    return 0;
                  }
  	      break;
@@ -159,7 +159,7 @@ _DEFUN(_fgets_r, (ptr, buf, n, fp),
  	  fp->_p = t;
  	  _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
  	  s[len] = 0;
-          _funlockfile (fp);
+          _newlib_flockfile_exit (fp);
  	  return (buf);
  	}
        fp->_r -= len;
@@ -169,7 +169,7 @@ _DEFUN(_fgets_r, (ptr, buf, n, fp),
      }
    while ((n -= len) != 0);
    *s = 0;
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return buf;
  }

Index: libc/stdio/fgetwc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fgetwc.c,v
retrieving revision 1.4
diff -u -p -r1.4 fgetwc.c
--- libc/stdio/fgetwc.c	28 Jan 2011 10:49:11 -0000	1.4
+++ libc/stdio/fgetwc.c	29 May 2012 17:18:37 -0000
@@ -164,10 +164,10 @@ _DEFUN(_fgetwc_r, (ptr, fp),
  {
    wint_t r;

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ORIENT(fp, 1);
    r = __fgetwc (ptr, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return r;
  }

Index: libc/stdio/fgetws.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fgetws.c,v
retrieving revision 1.4
diff -u -p -r1.4 fgetws.c
--- libc/stdio/fgetws.c	28 Jan 2011 10:49:11 -0000	1.4
+++ libc/stdio/fgetws.c	29 May 2012 17:18:37 -0000
@@ -93,7 +93,7 @@ _DEFUN(_fgetws_r, (ptr, ws, n, fp),
    const char *src;
    unsigned char *nl;

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ORIENT (fp, 1);

    if (n<= 0)
@@ -142,11 +142,11 @@ _DEFUN(_fgetws_r, (ptr, ws, n, fp),
      /* Incomplete character */
      goto error;
    *wsp++ = L'\0';
-  _funlockfile (fp);
+  _newlib_flockfile_exit (fp);
    return ws;

  error:
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return NULL;
  }

Index: libc/stdio/fileno.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fileno.c,v
retrieving revision 1.5
diff -u -p -r1.5 fileno.c
--- libc/stdio/fileno.c	26 Sep 2006 21:22:19 -0000	1.5
+++ libc/stdio/fileno.c	29 May 2012 17:18:37 -0000
@@ -55,8 +55,8 @@ _DEFUN(fileno, (f),
  {
    int result;
    CHECK_INIT (_REENT, f);
-  _flockfile (f);
+  _newlib_flockfile_start (f);
    result = __sfileno (f);
-  _funlockfile (f);
+  _newlib_flockfile_end (f);
    return result;
  }
Index: libc/stdio/findfp.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/findfp.c,v
retrieving revision 1.22
diff -u -p -r1.22 findfp.c
--- libc/stdio/findfp.c	26 Mar 2012 10:43:06 -0000	1.22
+++ libc/stdio/findfp.c	29 May 2012 17:18:37 -0000
@@ -108,7 +108,7 @@ _DEFUN(__sfp, (d),
    int n;
    struct _glue *g;

-  __sfp_lock_acquire ();
+  _newlib_sfp_lock_start ();

    if (!_GLOBAL_REENT->__sdidinit)
      __sinit (_GLOBAL_REENT);
@@ -121,7 +121,7 @@ _DEFUN(__sfp, (d),
  	  (g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
  	break;
      }
-  __sfp_lock_release ();
+  _newlib_sfp_lock_exit ();
    d->_errno = ENOMEM;
    return NULL;

@@ -132,7 +132,7 @@ found:
  #ifndef __SINGLE_THREAD__
    __lock_init_recursive (fp->_lock);
  #endif
-  __sfp_lock_release ();
+  _newlib_sfp_lock_end ();

    fp->_p = NULL;		/* no current pointer */
    fp->_w = 0;			/* nothing to read or write */
Index: libc/stdio/fmemopen.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fmemopen.c,v
retrieving revision 1.2
diff -u -p -r1.2 fmemopen.c
--- libc/stdio/fmemopen.c	22 Feb 2011 15:38:14 -0000	1.2
+++ libc/stdio/fmemopen.c	29 May 2012 17:18:38 -0000
@@ -291,12 +291,12 @@ _DEFUN(_fmemopen_r, (ptr, buf, size, mod
    if ((c = (fmemcookie *) _malloc_r (ptr, sizeof *c + (buf ? 0 : size)))
        == NULL)
      {
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* release */
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        return NULL;
      }

@@ -343,7 +343,7 @@ _DEFUN(_fmemopen_r, (ptr, buf, size, mod
  	}
      }

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    fp->_file = -1;
    fp->_flags = flags;
    fp->_cookie = c;
@@ -355,7 +355,7 @@ _DEFUN(_fmemopen_r, (ptr, buf, size, mod
    fp->_flags |= __SL64;
  #endif
    fp->_close = fmemcloser;
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return fp;
  }

Index: libc/stdio/fopen.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fopen.c,v
retrieving revision 1.9
diff -u -p -r1.9 fopen.c
--- libc/stdio/fopen.c	11 Jun 2004 20:37:10 -0000	1.9
+++ libc/stdio/fopen.c	29 May 2012 17:18:38 -0000
@@ -140,16 +140,16 @@ _DEFUN(_fopen_r, (ptr, file, mode),

    if ((f = _open_r (ptr, file, oflags, 0666))<  0)
      {
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* release */
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        return NULL;
      }

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    fp->_file = f;
    fp->_flags = flags;
@@ -167,7 +167,7 @@ _DEFUN(_fopen_r, (ptr, file, mode),
      fp->_flags |= __SCLE;
  #endif

-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return fp;
  }

Index: libc/stdio/fopencookie.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fopencookie.c,v
retrieving revision 1.4
diff -u -p -r1.4 fopencookie.c
--- libc/stdio/fopencookie.c	31 Oct 2008 21:08:03 -0000	1.4
+++ libc/stdio/fopencookie.c	29 May 2012 17:18:38 -0000
@@ -219,16 +219,16 @@ _DEFUN(_fopencookie_r, (ptr, cookie, mod
      return NULL;
    if ((c = (fccookie *) _malloc_r (ptr, sizeof *c)) == NULL)
      {
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* release */
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        return NULL;
      }

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    fp->_file = -1;
    fp->_flags = flags;
    c->cookie = cookie;
@@ -246,7 +246,7 @@ _DEFUN(_fopencookie_r, (ptr, cookie, mod
  #endif
    c->closefn = functions.close;
    fp->_close = fccloser;
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return fp;
  }

Index: libc/stdio/fpurge.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fpurge.c,v
retrieving revision 1.2
diff -u -p -r1.2 fpurge.c
--- libc/stdio/fpurge.c	19 May 2011 07:21:42 -0000	1.2
+++ libc/stdio/fpurge.c	29 May 2012 17:18:38 -0000
@@ -68,13 +68,13 @@ _DEFUN(_fpurge_r, (ptr, fp),

CHECK_INIT (ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    t = fp->_flags;
    if (!t)
      {
        ptr->_errno = EBADF;
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return EOF;
      }
    fp->_p = fp->_bf._base;
@@ -86,7 +86,7 @@ _DEFUN(_fpurge_r, (ptr, fp),
      }
    else
      fp->_w = t&  (__SLBF | __SNBF) ? 0 : fp->_bf._size;
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return 0;
  }

Index: libc/stdio/fputc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fputc.c,v
retrieving revision 1.6
diff -u -p -r1.6 fputc.c
--- libc/stdio/fputc.c	26 Sep 2006 21:22:19 -0000	1.6
+++ libc/stdio/fputc.c	29 May 2012 17:18:38 -0000
@@ -83,9 +83,9 @@ _DEFUN(_fputc_r, (ptr, ch, file),
  {
    int result;
    CHECK_INIT(ptr, file);
-   _flockfile (file);
+   _newlib_flockfile_start (file);
    result = _putc_r (ptr, ch, file);
-  _funlockfile (file);
+  _newlib_flockfile_end (file);
    return result;
  }

@@ -98,9 +98,9 @@ _DEFUN(fputc, (ch, file),
  #if !defined(__OPTIMIZE_SIZE__)&&  !defined(PREFER_SIZE_OVER_SPEED)
    int result;
    CHECK_INIT(_REENT, file);
-   _flockfile (file);
+   _newlib_flockfile_start (file);
    result = _putc_r (_REENT, ch, file);
-  _funlockfile (file);
+  _newlib_flockfile_end (file);
    return result;
  #else
    return _fputc_r (_REENT, ch, file);
Index: libc/stdio/fputs.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fputs.c,v
retrieving revision 1.7
diff -u -p -r1.7 fputs.c
--- libc/stdio/fputs.c	10 Dec 2008 23:43:12 -0000	1.7
+++ libc/stdio/fputs.c	29 May 2012 17:18:38 -0000
@@ -88,10 +88,10 @@ _DEFUN(_fputs_r, (ptr, s, fp),

CHECK_INIT(ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ORIENT (fp, -1);
    result = __sfvwrite_r (ptr, fp,&uio);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  }

Index: libc/stdio/fputwc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fputwc.c,v
retrieving revision 1.2
diff -u -p -r1.2 fputwc.c
--- libc/stdio/fputwc.c	16 Feb 2009 15:04:34 -0000	1.2
+++ libc/stdio/fputwc.c	29 May 2012 17:18:38 -0000
@@ -160,10 +160,10 @@ _DEFUN(_fputwc_r, (ptr, wc, fp),
  {
    wint_t r;

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ORIENT(fp, 1);
    r = __fputwc(ptr, wc, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return r;
  }

Index: libc/stdio/fputws.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fputws.c,v
retrieving revision 1.2
diff -u -p -r1.2 fputws.c
--- libc/stdio/fputws.c	12 Dec 2008 17:21:08 -0000	1.2
+++ libc/stdio/fputws.c	29 May 2012 17:18:38 -0000
@@ -87,7 +87,7 @@ _DEFUN(_fputws_r, (ptr, ws, fp),
    struct __suio uio;
    struct __siov iov;

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ORIENT (fp, 1);
    if (cantwrite (ptr, fp) != 0)
      goto error;
@@ -104,11 +104,11 @@ _DEFUN(_fputws_r, (ptr, ws, fp),
  	goto error;
      }
    while (ws != NULL);
-  _funlockfile (fp);
+  _newlib_flockfile_exit (fp);
    return (0);

  error:
-  _funlockfile(fp);
+  _newlib_flockfile_end(fp);
    return (-1);
  }

Index: libc/stdio/fread.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fread.c,v
retrieving revision 1.20
diff -u -p -r1.20 fread.c
--- libc/stdio/fread.c	28 Jan 2011 10:49:11 -0000	1.20
+++ libc/stdio/fread.c	29 May 2012 17:18:38 -0000
@@ -146,7 +146,7 @@ _DEFUN(_fread_r, (ptr, buf, size, count,

CHECK_INIT(ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ORIENT (fp, -1);
    if (fp->_r<  0)
      fp->_r = 0;
@@ -195,11 +195,11 @@ _DEFUN(_fread_r, (ptr, buf, size, count,
  #ifdef __SCLE
                if (fp->_flags&  __SCLE)
  	        {
-	          _funlockfile (fp);
+	          _newlib_flockfile_exit (fp);
  	          return crlf_r (ptr, fp, buf, total-resid, 1) / size;
  	        }
  #endif
-	      _funlockfile (fp);
+	      _newlib_flockfile_exit (fp);
  	      return (total - resid) / size;
  	    }
  	}
@@ -220,11 +220,11 @@ _DEFUN(_fread_r, (ptr, buf, size, count,
  #ifdef __SCLE
  	      if (fp->_flags&  __SCLE)
  		{
-		  _funlockfile (fp);
+		  _newlib_flockfile_exit (fp);
  		  return crlf_r (ptr, fp, buf, total-resid, 1) / size;
  		}
  #endif
-	      _funlockfile (fp);
+	      _newlib_flockfile_exit (fp);
  	      return (total - resid) / size;
  	    }
  	}
@@ -237,11 +237,11 @@ _DEFUN(_fread_r, (ptr, buf, size, count,
  #ifdef __SCLE
    if (fp->_flags&  __SCLE)
      {
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return crlf_r(ptr, fp, buf, total, 0) / size;
      }
  #endif
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return count;
  }

Index: libc/stdio/freopen.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/freopen.c,v
retrieving revision 1.25
diff -u -p -r1.25 freopen.c
--- libc/stdio/freopen.c	28 Jan 2011 10:49:11 -0000	1.25
+++ libc/stdio/freopen.c	29 May 2012 17:18:38 -0000
@@ -100,11 +100,11 @@ _DEFUN(_freopen_r, (ptr, file, mode, fp)

CHECK_INIT (ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    if ((flags = __sflags (ptr, mode,&oflags)) == 0)
      {
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        _fclose_r (ptr, fp);
        return NULL;
      }
@@ -205,14 +205,14 @@ _DEFUN(_freopen_r, (ptr, file, mode, fp)

    if (f<  0)
      {				/* did not get it after all */
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* set it free */
        ptr->_errno = e;		/* restore in case _close clobbered */
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        return NULL;
      }

@@ -229,7 +229,7 @@ _DEFUN(_freopen_r, (ptr, file, mode, fp)
      fp->_flags |= __SCLE;
  #endif

-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return fp;
  }

Index: libc/stdio/fseek.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fseek.c,v
retrieving revision 1.24
diff -u -p -r1.24 fseek.c
--- libc/stdio/fseek.c	28 Jan 2011 10:49:11 -0000	1.24
+++ libc/stdio/fseek.c	29 May 2012 17:18:38 -0000
@@ -138,7 +138,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whenc

CHECK_INIT (ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    /* If we've been doing some writing, and we're in append mode
       then we don't really know where the filepos is.  */
@@ -154,7 +154,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whenc
    if ((seekfn = fp->_seek) == NULL)
      {
        ptr->_errno = ESPIPE;	/* ??? */
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return EOF;
      }

@@ -179,7 +179,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whenc
  	  curoff = seekfn (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
  	  if (curoff == -1L)
  	    {
-	      _funlockfile (fp);
+	      _newlib_flockfile_exit (fp);
  	      return EOF;
  	    }
  	}
@@ -204,7 +204,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whenc

      default:
        ptr->_errno = EINVAL;
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return (EOF);
      }

@@ -263,7 +263,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whenc
    if ((long)target != target)
      {
        ptr->_errno = EOVERFLOW;
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return EOF;
      }

@@ -319,7 +319,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whenc
  	FREEUB (ptr, fp);
        fp->_flags&= ~__SEOF;
        memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return 0;
      }

@@ -349,7 +349,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whenc
        fp->_r -= n;
      }
    memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
-  _funlockfile (fp);
+  _newlib_flockfile_exit (fp);
    return 0;

    /*
@@ -361,7 +361,7 @@ dumb:
    if (_fflush_r (ptr, fp)
        || seekfn (ptr, fp->_cookie, offset, whence) == POS_ERR)
      {
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return EOF;
      }
    /* success: clear EOF indicator and discard ungetc() data */
@@ -379,7 +379,7 @@ dumb:
       is performed.  */
    fp->_flags&= ~__SNPT;
    memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return 0;
  }

Index: libc/stdio/ftell.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/ftell.c,v
retrieving revision 1.13
diff -u -p -r1.13 ftell.c
--- libc/stdio/ftell.c	4 Mar 2008 02:22:36 -0000	1.13
+++ libc/stdio/ftell.c	29 May 2012 17:18:38 -0000
@@ -109,12 +109,12 @@ _DEFUN(_ftell_r, (ptr, fp),

CHECK_INIT (ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    if (fp->_seek == NULL)
      {
        ptr->_errno = ESPIPE;
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return -1L;
      }

@@ -131,7 +131,7 @@ _DEFUN(_ftell_r, (ptr, fp),
        pos = fp->_seek (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
        if (pos == -1L)
          {
-          _funlockfile (fp);
+          _newlib_flockfile_exit (fp);
            return pos;
          }
      }
@@ -156,7 +156,7 @@ _DEFUN(_ftell_r, (ptr, fp),
        pos += fp->_p - fp->_bf._base;
      }

-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    if ((long)pos != pos)
      {
        pos = -1;
Index: libc/stdio/funopen.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/funopen.c,v
retrieving revision 1.1
diff -u -p -r1.1 funopen.c
--- libc/stdio/funopen.c	4 Jun 2007 18:10:17 -0000	1.1
+++ libc/stdio/funopen.c	29 May 2012 17:18:38 -0000
@@ -214,16 +214,16 @@ _DEFUN(_funopen_r, (ptr, cookie, readfn,
      return NULL;
    if ((c = (funcookie *) _malloc_r (ptr, sizeof *c)) == NULL)
      {
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* release */
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        return NULL;
      }

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    fp->_file = -1;
    c->cookie = (void *) cookie; /* cast away const */
    fp->_cookie = c;
@@ -260,7 +260,7 @@ _DEFUN(_funopen_r, (ptr, cookie, readfn,
  #endif
    c->closefn = closefn;
    fp->_close = funcloser;
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return fp;
  }

Index: libc/stdio/fwide.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fwide.c,v
retrieving revision 1.3
diff -u -p -r1.3 fwide.c
--- libc/stdio/fwide.c	5 Jan 2009 19:08:03 -0000	1.3
+++ libc/stdio/fwide.c	29 May 2012 17:18:38 -0000
@@ -68,7 +68,7 @@ _DEFUN(_fwide_r, (ptr, fp, mode),

CHECK_INIT(ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    if (mode != 0) {
      ORIENT (fp, mode);
    }
@@ -76,7 +76,7 @@ _DEFUN(_fwide_r, (ptr, fp, mode),
      ret = 0;
    else
      ret = (fp->_flags2&  __SWID) ? 1 : -1;
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return ret;
  }

Index: libc/stdio/fwrite.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fwrite.c,v
retrieving revision 1.8
diff -u -p -r1.8 fwrite.c
--- libc/stdio/fwrite.c	10 Dec 2008 23:43:12 -0000	1.8
+++ libc/stdio/fwrite.c	29 May 2012 17:18:38 -0000
@@ -119,14 +119,14 @@ _DEFUN(_fwrite_r, (ptr, buf, size, count

CHECK_INIT(ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ORIENT (fp, -1);
    if (__sfvwrite_r (ptr, fp,&uio) == 0)
      {
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return count;
      }
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return (n - uio.uio_resid) / size;
  }

Index: libc/stdio/getc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/getc.c,v
retrieving revision 1.8
diff -u -p -r1.8 getc.c
--- libc/stdio/getc.c	28 Jan 2011 10:49:11 -0000	1.8
+++ libc/stdio/getc.c	29 May 2012 17:18:38 -0000
@@ -92,9 +92,9 @@ _DEFUN(_getc_r, (ptr, fp),
  {
    int result;
    CHECK_INIT (ptr, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    result = __sgetc_r (ptr, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  }

@@ -106,9 +106,9 @@ _DEFUN(getc, (fp),
  {
    int result;
    CHECK_INIT (_REENT, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    result = __sgetc_r (_REENT, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  }

Index: libc/stdio/getdelim.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/getdelim.c,v
retrieving revision 1.8
diff -u -p -r1.8 getdelim.c
--- libc/stdio/getdelim.c	28 Jan 2011 10:49:11 -0000	1.8
+++ libc/stdio/getdelim.c	29 May 2012 17:18:38 -0000
@@ -81,7 +81,7 @@ _DEFUN(__getdelim, (bufptr, n, delim, fp

CHECK_INIT (_REENT, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    numbytes = *n;
    ptr = buf;
@@ -129,7 +129,7 @@ _DEFUN(__getdelim, (bufptr, n, delim, fp
          }
      }

-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);

    /* if no input data, return failure */
    if (ptr == buf)
Index: libc/stdio/gets.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/gets.c,v
retrieving revision 1.5
diff -u -p -r1.5 gets.c
--- libc/stdio/gets.c	28 Jan 2011 10:49:11 -0000	1.5
+++ libc/stdio/gets.c	29 May 2012 17:18:38 -0000
@@ -70,6 +70,7 @@ Supporting OS subroutines required:<<cl
  #include<_ansi.h>
  #include<reent.h>
  #include<stdio.h>
+#include "local.h"

  char *
  _DEFUN(_gets_r, (ptr, buf),
@@ -79,12 +80,12 @@ _DEFUN(_gets_r, (ptr, buf),
    register int c;
    register char *s = buf;

-  _flockfile (stdin);
+  _newlib_flockfile_start (stdin);
    while ((c = __sgetc_r (ptr, stdin)) != '\n')
      if (c == EOF)
        if (s == buf)
  	{
-	  _funlockfile (stdin);
+	  _newlib_flockfile_exit (stdin);
  	  return NULL;
  	}
        else
@@ -92,7 +93,7 @@ _DEFUN(_gets_r, (ptr, buf),
      else
        *s++ = c;
    *s = 0;
-  _funlockfile (stdin);
+  _newlib_flockfile_end (stdin);
    return buf;
  }

Index: libc/stdio/open_memstream.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/open_memstream.c,v
retrieving revision 1.6
diff -u -p -r1.6 open_memstream.c
--- libc/stdio/open_memstream.c	5 Jan 2011 17:38:42 -0000	1.6
+++ libc/stdio/open_memstream.c	29 May 2012 17:18:38 -0000
@@ -313,12 +313,12 @@ _DEFUN(internal_open_memstream_r, (ptr,
      return NULL;
    if ((c = (memstream *) _malloc_r (ptr, sizeof *c)) == NULL)
      {
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* release */
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        return NULL;
      }
    /* Use *size as a hint for initial sizing, but bound the initial
@@ -338,12 +338,12 @@ _DEFUN(internal_open_memstream_r, (ptr,
    *buf = _malloc_r (ptr, c->max);
    if (!*buf)
      {
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* release */
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        _free_r (ptr, c);
        return NULL;
      }
@@ -359,7 +359,7 @@ _DEFUN(internal_open_memstream_r, (ptr,
    c->saved.w = L'\0';
    c->wide = (int8_t) wide;

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    fp->_file = -1;
    fp->_flags = __SWR;
    fp->_cookie = c;
@@ -372,7 +372,7 @@ _DEFUN(internal_open_memstream_r, (ptr,
  #endif
    fp->_close = memcloser;
    ORIENT (fp, wide);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return fp;
  }

Index: libc/stdio/putc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/putc.c,v
retrieving revision 1.6
diff -u -p -r1.6 putc.c
--- libc/stdio/putc.c	26 Sep 2006 21:22:19 -0000	1.6
+++ libc/stdio/putc.c	29 May 2012 17:18:38 -0000
@@ -97,9 +97,9 @@ _DEFUN(_putc_r, (ptr, c, fp),
  {
    int result;
    CHECK_INIT (ptr, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    result = __sputc_r (ptr, c, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  }

@@ -112,9 +112,9 @@ _DEFUN(putc, (c, fp),
  #if !defined(PREFER_SIZE_OVER_SPEED)&&  !defined(__OPTIMIZE_SIZE__)
    int result;
    CHECK_INIT (_REENT, fp);
-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    result = __sputc_r (_REENT, c, fp);
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return result;
  #else
    return _putc_r (_REENT, c, fp);
Index: libc/stdio/setvbuf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/setvbuf.c,v
retrieving revision 1.7
diff -u -p -r1.7 setvbuf.c
--- libc/stdio/setvbuf.c	13 Jul 2007 20:37:53 -0000	1.7
+++ libc/stdio/setvbuf.c	29 May 2012 17:18:38 -0000
@@ -106,7 +106,7 @@ _DEFUN(setvbuf, (fp, buf, mode, size),

CHECK_INIT (_REENT, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

    /*
     * Verify arguments.  The `int' limit on `size' is due to this
@@ -115,7 +115,7 @@ _DEFUN(setvbuf, (fp, buf, mode, size),

    if ((mode != _IOFBF&&  mode != _IOLBF&&  mode != _IONBF) || (int)(_POINTER_INT) size<  0)
      {
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return (EOF);
      }

@@ -158,7 +158,7 @@ nbf:
            fp->_w = 0;
            fp->_bf._base = fp->_p = fp->_nbuf;
            fp->_bf._size = 1;
-          _funlockfile (fp);
+          _newlib_flockfile_exit (fp);
            return (ret);
          }
        fp->_flags |= __SMBF;
@@ -193,6 +193,6 @@ nbf:
    if (fp->_flags&  __SWR)
      fp->_w = fp->_flags&  (__SLBF | __SNBF) ? 0 : size;

-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return 0;
  }
Index: libc/stdio/ungetc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/ungetc.c,v
retrieving revision 1.10
diff -u -p -r1.10 ungetc.c
--- libc/stdio/ungetc.c	10 Dec 2008 23:43:12 -0000	1.10
+++ libc/stdio/ungetc.c	29 May 2012 17:18:38 -0000
@@ -125,7 +125,7 @@ _DEFUN(_ungetc_r, (rptr, c, fp),

CHECK_INIT (rptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

ORIENT (fp, -1);

@@ -140,14 +140,14 @@ _DEFUN(_ungetc_r, (rptr, c, fp),
         */
        if ((fp->_flags&  __SRW) == 0)
          {
-          _funlockfile (fp);
+          _newlib_flockfile_exit (fp);
            return EOF;
          }
        if (fp->_flags&  __SWR)
  	{
  	  if (_fflush_r (rptr, fp))
              {
-              _funlockfile (fp);
+              _newlib_flockfile_exit (fp);
                return EOF;
              }
  	  fp->_flags&= ~__SWR;
@@ -167,12 +167,12 @@ _DEFUN(_ungetc_r, (rptr, c, fp),
      {
        if (fp->_r>= fp->_ub._size&&  __submore (rptr, fp))
          {
-          _funlockfile (fp);
+          _newlib_flockfile_exit (fp);
            return EOF;
          }
        *--fp->_p = c;
        fp->_r++;
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return c;
      }

@@ -186,7 +186,7 @@ _DEFUN(_ungetc_r, (rptr, c, fp),
      {
        fp->_p--;
        fp->_r++;
-      _funlockfile (fp);
+      _newlib_flockfile_exit (fp);
        return c;
      }

@@ -202,7 +202,7 @@ _DEFUN(_ungetc_r, (rptr, c, fp),
    fp->_ubuf[sizeof (fp->_ubuf) - 1] = c;
    fp->_p =&fp->_ubuf[sizeof (fp->_ubuf) - 1];
    fp->_r = 1;
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return c;
  }

Index: libc/stdio/ungetwc.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/ungetwc.c,v
retrieving revision 1.1
diff -u -p -r1.1 ungetwc.c
--- libc/stdio/ungetwc.c	10 Dec 2008 23:43:12 -0000	1.1
+++ libc/stdio/ungetwc.c	29 May 2012 17:18:38 -0000
@@ -82,7 +82,7 @@ _DEFUN(_ungetwc_r, (ptr, wc, fp),
    char buf[MB_LEN_MAX];
    size_t len;

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);
    ORIENT (fp, 1);
    if (wc == WEOF)
      wc = WEOF;
@@ -98,7 +98,7 @@ _DEFUN(_ungetwc_r, (ptr, wc, fp),
  	  wc = WEOF;
  	  break;
  	}
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return wc;
  }

Index: libc/stdio/vfprintf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/vfprintf.c,v
retrieving revision 1.80
diff -u -p -r1.80 vfprintf.c
--- libc/stdio/vfprintf.c	13 Jan 2012 09:13:57 -0000	1.80
+++ libc/stdio/vfprintf.c	29 May 2012 17:18:39 -0000
@@ -708,20 +708,20 @@ _DEFUN(_VFPRINTF_R, (data, fp, fmt0, ap)
  #ifndef STRING_ONLY
  	/* Initialize std streams if not dealing with sprintf family.  */
  	CHECK_INIT (data, fp);
-	_flockfile (fp);
+	_newlib_flockfile_start (fp);

ORIENT(fp, -1);

  	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
  	if (cantwrite (data, fp)) {
-		_funlockfile (fp);
+		_newlib_flockfile_exit (fp);
  		return (EOF);
  	}

  	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
  	if ((fp->_flags&  (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR)&&
  	fp->_file>= 0) {
-		_funlockfile (fp);
+		_newlib_flockfile_exit (fp);
  		return (__sbprintf (data, fp, fmt0, ap));
  	}
  #else /* STRING_ONLY */
@@ -1633,7 +1633,7 @@ error:
  	if (malloc_buf != NULL)
  		_free_r (data, malloc_buf);
  #ifndef STRING_ONLY
-	_funlockfile (fp);
+	_newlib_flockfile_end (fp);
  #endif
  	return (__sferror (fp) ? EOF : ret);
  	/* NOTREACHED */
Index: libc/stdio/vfscanf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/vfscanf.c,v
retrieving revision 1.51
diff -u -p -r1.51 vfscanf.c
--- libc/stdio/vfscanf.c	23 Aug 2011 11:59:55 -0000	1.51
+++ libc/stdio/vfscanf.c	29 May 2012 17:18:39 -0000
@@ -148,10 +148,12 @@ Supporting OS subroutines required:
  #endif

  #ifdef STRING_ONLY
-#undef _flockfile
-#undef _funlockfile
-#define _flockfile(x) {}
-#define _funlockfile(x) {}
+#undef _newlib_flockfile_start
+#undef _newlib_flockfile_exit
+#undef _newlib_flockfile_end
+#define _newlib_flockfile_start(x) {}
+#define _newlib_flockfile_exit(x) {}
+#define _newlib_flockfile_end(x) {}
  #define _ungetc_r _sungetc_r
  #define __srefill_r __ssrefill_r
  #define _fread_r _sfread_r
@@ -496,7 +498,7 @@ _DEFUN(__SVFSCANF_R, (rptr, fp, fmt0, ap
  # define GET_ARG(n, ap, type) (va_arg (ap, type))
  #endif

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

ORIENT (fp, -1);

@@ -795,7 +797,7 @@ _DEFUN(__SVFSCANF_R, (rptr, fp, fmt0, ap
  	   * Disgusting backwards compatibility hacks.	XXX
  	   */
  	case '\0':		/* compat */
-	  _funlockfile (fp);
+	  _newlib_flockfile_exit (fp);
  	  return EOF;

  	default:		/* compat */
@@ -1595,12 +1597,12 @@ input_failure:
       should have been set prior to here.  On EOF failure (including
       invalid format string), return EOF if no matches yet, else number
       of matches made prior to failure.  */
-  _funlockfile (fp);
+  _newlib_flockfile_exit (fp);
    return nassigned&&  !(fp->_flags&  __SERR) ? nassigned : EOF;
  match_failure:
  all_done:
    /* Return number of matches, which can be 0 on match failure.  */
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return nassigned;
  }

Index: libc/stdio/vfwprintf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/vfwprintf.c,v
retrieving revision 1.7
diff -u -p -r1.7 vfwprintf.c
--- libc/stdio/vfwprintf.c	13 Jan 2012 09:13:57 -0000	1.7
+++ libc/stdio/vfwprintf.c	29 May 2012 17:18:39 -0000
@@ -553,20 +553,20 @@ _DEFUN(_VFWPRINTF_R, (data, fp, fmt0, ap
  #ifndef STRING_ONLY
  	/* Initialize std streams if not dealing with sprintf family.  */
  	CHECK_INIT (data, fp);
-	_flockfile (fp);
+	_newlib_flockfile_start (fp);

ORIENT(fp, 1);

  	/* sorry, fwprintf(read_only_file, "") returns EOF, not 0 */
  	if (cantwrite (data, fp)) {
-		_funlockfile (fp);
+		_newlib_flockfile_exit (fp);
  		return (EOF);
  	}

  	/* optimise fwprintf(stderr) (and other unbuffered Unix files) */
  	if ((fp->_flags&  (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR)&&
  	fp->_file>= 0) {
-		_funlockfile (fp);
+		_newlib_flockfile_exit (fp);
  		return (__sbwprintf (data, fp, fmt0, ap));
  	}
  #else /* STRING_ONLY */
@@ -1465,7 +1465,7 @@ error:
  	if (malloc_buf != NULL)
  		_free_r (data, malloc_buf);
  #ifndef STRING_ONLY
-	_funlockfile (fp);
+	_newlib_flockfile_end (fp);
  #endif
  	return (__sferror (fp) ? EOF : ret);
  	/* NOTREACHED */
Index: libc/stdio/vfwscanf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/vfwscanf.c,v
retrieving revision 1.2
diff -u -p -r1.2 vfwscanf.c
--- libc/stdio/vfwscanf.c	28 Jan 2011 10:49:11 -0000	1.2
+++ libc/stdio/vfwscanf.c	29 May 2012 17:18:39 -0000
@@ -145,10 +145,12 @@ C99, POSIX-1.2008
  #endif

  #ifdef STRING_ONLY
-#undef _flockfile
-#undef _funlockfile
-#define _flockfile(x) {}
-#define _funlockfile(x) {}
+#undef _newlib_flockfile_start
+#undef _newlib_flockfile_exit
+#undef _newlib_flockfile_end
+#define _newlib_flockfile_start(x) {}
+#define _newlib_flockfile_exit(x) {}
+#define _newlib_flockfile_end(x) {}
  #define _ungetwc_r _sungetwc_r
  #define __srefill_r __ssrefill_r
  #define _fgetwc_r _sfgetwc_r
@@ -434,7 +436,7 @@ _DEFUN(__SVFWSCANF_R, (rptr, fp, fmt0, a
  # define GET_ARG(n, ap, type) (va_arg (ap, type))
  #endif

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

ORIENT (fp, 1);

@@ -712,7 +714,7 @@ _DEFUN(__SVFWSCANF_R, (rptr, fp, fmt0, a
  	   * Disgusting backwards compatibility hacks.	XXX
  	   */
  	case L'\0':		/* compat */
-	  _funlockfile (fp);
+	  _newlib_flockfile_exit (fp);
  	  return EOF;

  	default:		/* compat */
@@ -1440,12 +1442,12 @@ input_failure:
       should have been set prior to here.  On EOF failure (including
       invalid format string), return EOF if no matches yet, else number
       of matches made prior to failure.  */
-  _funlockfile (fp);
+  _newlib_flockfile_exit (fp);
    return nassigned&&  !(fp->_flags&  __SERR) ? nassigned : EOF;
  match_failure:
  all_done:
    /* Return number of matches, which can be 0 on match failure.  */
-  _funlockfile (fp);
+  _newlib_flockfile_end (fp);
    return nassigned;
  }

Index: libc/stdio64/fdopen64.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio64/fdopen64.c,v
retrieving revision 1.5
diff -u -p -r1.5 fdopen64.c
--- libc/stdio64/fdopen64.c	31 Jul 2007 20:49:40 -0000	1.5
+++ libc/stdio64/fdopen64.c	29 May 2012 17:18:39 -0000
@@ -64,7 +64,7 @@ _DEFUN (_fdopen64_r, (ptr, fd, mode),
    if ((fp = __sfp (ptr)) == 0)
      return 0;

-  _flockfile(fp);
+  _newlib_flockfile_start(fp);

    fp->_flags = flags;
    /* POSIX recommends setting the O_APPEND bit on fd to match append
@@ -101,7 +101,7 @@ _DEFUN (_fdopen64_r, (ptr, fd, mode),

fp->_flags |= __SL64;

-  _funlockfile(fp);
+  _newlib_flockfile_end(fp);
    return fp;
  }

Index: libc/stdio64/fopen64.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio64/fopen64.c,v
retrieving revision 1.7
diff -u -p -r1.7 fopen64.c
--- libc/stdio64/fopen64.c	1 May 2007 23:03:36 -0000	1.7
+++ libc/stdio64/fopen64.c	29 May 2012 17:18:39 -0000
@@ -91,12 +91,12 @@ _DEFUN (_fopen64_r, (ptr, file, mode),

    if ((f = _open64_r (ptr, file, oflags, 0666))<  0)
      {
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* release */
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        return NULL;
      }

Index: libc/stdio64/freopen64.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio64/freopen64.c,v
retrieving revision 1.18
diff -u -p -r1.18 freopen64.c
--- libc/stdio64/freopen64.c	28 Jan 2011 10:49:11 -0000	1.18
+++ libc/stdio64/freopen64.c	29 May 2012 17:18:39 -0000
@@ -100,11 +100,11 @@ _DEFUN (_freopen64_r, (ptr, file, mode,

CHECK_INIT (ptr, fp);

-  _flockfile(fp);
+  _newlib_flockfile_start(fp);

    if ((flags = __sflags (ptr, mode,&oflags)) == 0)
      {
-      _funlockfile(fp);
+      _newlib_flockfile_exit(fp);
        _fclose_r (ptr, fp);
        return NULL;
      }
@@ -202,14 +202,14 @@ _DEFUN (_freopen64_r, (ptr, file, mode,

    if (f<  0)
      {				/* did not get it after all */
-      __sfp_lock_acquire ();
+      _newlib_sfp_lock_start ();
        fp->_flags = 0;		/* set it free */
        ptr->_errno = e;		/* restore in case _close clobbered */
-      _funlockfile(fp);
+      _newlib_flockfile_exit(fp);
  #ifndef __SINGLE_THREAD__
        __lock_close_recursive (fp->_lock);
  #endif
-      __sfp_lock_release ();
+      _newlib_sfp_lock_end ();
        return NULL;
      }

@@ -229,7 +229,7 @@ _DEFUN (_freopen64_r, (ptr, file, mode,

fp->_flags |= __SL64;

-  _funlockfile(fp);
+  _newlib_flockfile_end(fp);
    return fp;
  }

Index: libc/stdio64/fseeko64.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio64/fseeko64.c,v
retrieving revision 1.14
diff -u -p -r1.14 fseeko64.c
--- libc/stdio64/fseeko64.c	17 Dec 2009 19:43:43 -0000	1.14
+++ libc/stdio64/fseeko64.c	29 May 2012 17:18:39 -0000
@@ -126,7 +126,7 @@ _DEFUN (_fseeko64_r, (ptr, fp, offset, w

CHECK_INIT (ptr, fp);

-  _flockfile (fp);
+  _newlib_flockfile_start (fp);

curoff = fp->_offset;

@@ -144,7 +144,7 @@ _DEFUN (_fseeko64_r, (ptr, fp, offset, w
    if ((seekfn = fp->_seek64) == NULL)
      {
        ptr->_errno = ESPIPE;	/* ??? */
-      _funlockfile(fp);
+      _newlib_flockfile_exit(fp);
        return EOF;
      }

@@ -169,7 +169,7 @@ _DEFUN (_fseeko64_r, (ptr, fp, offset, w
  	  curoff = seekfn (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_CUR);
  	  if (curoff == -1L)
  	    {
-	      _funlockfile(fp);
+	      _newlib_flockfile_exit(fp);
  	      return EOF;
  	    }
  	}
@@ -194,7 +194,7 @@ _DEFUN (_fseeko64_r, (ptr, fp, offset, w

      default:
        ptr->_errno = EINVAL;
-      _funlockfile(fp);
+      _newlib_flockfile_exit(fp);
        return (EOF);
      }

@@ -294,7 +294,7 @@ _DEFUN (_fseeko64_r, (ptr, fp, offset, w
        if (HASUB (fp))
  	FREEUB (ptr, fp);
        fp->_flags&= ~__SEOF;
-      _funlockfile(fp);
+      _newlib_flockfile_exit(fp);
        return 0;
      }

@@ -323,7 +323,7 @@ _DEFUN (_fseeko64_r, (ptr, fp, offset, w
        fp->_p += n;
        fp->_r -= n;
      }
-  _funlockfile(fp);
+  _newlib_flockfile_end(fp);
    return 0;

    /*
Index: libc/stdio64/ftello64.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio64/ftello64.c,v
retrieving revision 1.9
diff -u -p -r1.9 ftello64.c
--- libc/stdio64/ftello64.c	4 Mar 2008 02:22:36 -0000	1.9
+++ libc/stdio64/ftello64.c	29 May 2012 17:18:39 -0000
@@ -99,12 +99,12 @@ _DEFUN (_ftello64_r, (ptr, fp),

CHECK_INIT (ptr, fp);

-  _flockfile(fp);
+  _newlib_flockfile_start(fp);

    if (fp->_seek64 == NULL)
      {
        ptr->_errno = ESPIPE;
-      _funlockfile(fp);
+      _newlib_flockfile_exit(fp);
        return -1L;
      }

@@ -121,7 +121,7 @@ _DEFUN (_ftello64_r, (ptr, fp),
        pos = fp->_seek64 (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_CUR);
        if (pos == -1L)
          {
-          _funlockfile(fp);
+          _newlib_flockfile_exit(fp);
            return pos;
          }
      }
@@ -146,7 +146,7 @@ _DEFUN (_ftello64_r, (ptr, fp),
        pos += fp->_p - fp->_bf._base;
      }

-  _funlockfile(fp);
+  _newlib_flockfile_end(fp);
    return pos;
  }





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