diff -urpN cygwin.org/dcrt0.cc cygwin/dcrt0.cc
--- cygwin.org/dcrt0.cc	2004-02-10 12:09:20.469726400 +0100
+++ cygwin/dcrt0.cc	2004-02-10 13:08:02.549700800 +0100
@@ -756,10 +756,13 @@ dll_crt0_1 (char *)
   ProtectHandle (hMainProc);
   ProtectHandle (hMainThread);
 
-  /* Initialize pthread mainthread when not forked and it is safe to call new,
-     otherwise it is reinitalized in fixup_after_fork */
+  /* Initialize pthread mainthread and stdio when not forked.
+     Note: Must be done after malloc is initialized */
   if (!user_data->forkee)
-    pthread::init_mainthread ();
+    {
+      pthread::init_mainthread ();
+      __sinit (_impure_ptr);
+    }
 
 #ifdef DEBUGGING
   strace.microseconds ();
@@ -946,8 +949,6 @@ _dll_crt0 ()
 
   if (child_proc_info && child_proc_info->type == _PROC_FORK)
     user_data->forkee = child_proc_info->cygpid;
-  else
-    __sinit (_impure_ptr);
 
   initialize_main_tls (padding);
   dll_crt0_1 (padding);
diff -urpN cygwin.org/include/cygwin/_types.h cygwin/include/cygwin/_types.h
--- cygwin.org/include/cygwin/_types.h	1970-01-01 01:00:00.000000000 +0100
+++ cygwin/include/cygwin/_types.h	2004-02-10 12:25:45.382086400 +0100
@@ -0,0 +1,16 @@
+/* cygwin/_types.h
+
+   Copyright 2004 Red Hat, Inc.
+
+This file is part of Cygwin.
+
+This software is a copyrighted work licensed under the terms of the
+Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
+details. */
+
+#ifndef _CYGWIN__TYPES_H
+#define _CYGWIN__TYPES_H
+
+typedef void *_flock_t;
+
+#endif	/* _CYGWIN__TYPES_H */
diff -urpN cygwin.org/include/sys/lock.h cygwin/include/sys/lock.h
--- cygwin.org/include/sys/lock.h	1970-01-01 01:00:00.000000000 +0100
+++ cygwin/include/sys/lock.h	2004-02-10 12:19:32.649998400 +0100
@@ -0,0 +1,36 @@
+/* sys/lock.h
+
+   Copyright 2004 Red Hat, Inc.
+
+This file is part of Cygwin.
+
+This software is a copyrighted work licensed under the terms of the
+Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
+details. */
+
+#ifndef _SYS_LOCK_H_
+#define _SYS_LOCK_H_
+
+typedef void *_LOCK_T;
+#define _LOCK_RECURSIVE_T _LOCK_T
+
+/*
+ * This must match cygwins PTHREAD_MUTEX_INITIALIZER which is
+ * defined in <pthread.h>
+ */
+#define _LOCK_T_INITIALIZER ((_LOCK_T)20)
+
+#define __LOCK_INIT(CLASS,NAME) \
+  CLASS _LOCK_T NAME = _LOCK_T_INITIALIZER; 
+
+#define __lock_init(__lock) __cygwin_lock_init(&__lock)
+#define __lock_close(__lock) __cygwin_lock_fini(&__lock)
+#define __lock_acquire(__lock) __cygwin_lock_lock(&__lock)
+#define __lock_release(__lock) __cygwin_lock_unlock(&__lock)
+
+#define __lock_init_recursive(__lock) __cygwin_lock_init_recursive(&__lock)
+#define __lock_close_recursive(__lock) __cygwin_lock_fini(&__lock)
+#define __lock_acquire_recursive(__lock) __cygwin_lock_lock(&__lock)
+#define __lock_release_recursive(__lock) __cygwin_lock_unlock(&__lock)
+
+#endif
diff -urpN cygwin.org/include/sys/stdio.h cygwin/include/sys/stdio.h
--- cygwin.org/include/sys/stdio.h	1970-01-01 01:00:00.000000000 +0100
+++ cygwin/include/sys/stdio.h	2004-02-10 12:19:32.740128000 +0100
@@ -0,0 +1,25 @@
+/* sys/stdio.h
+
+   Copyright 2004 Red Hat, Inc.
+
+This file is part of Cygwin.
+
+This software is a copyrighted work licensed under the terms of the
+Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
+details. */
+
+#ifndef _SYS_STDIO_H_
+#define _SYS_STDIO_H_
+
+#include <sys/lock.h>
+
+#if !defined(__SINGLE_THREAD__)
+#  if !defined(_flockfile)
+#    define _flockfile(fp) __cygwin_lock_lock ((_LOCK_T *)&(fp)->_lock)
+#  endif
+#  if !defined(_funlockfile)
+#    define _funlockfile(fp) __cygwin_lock_unlock ((_LOCK_T *)&(fp)->_lock)
+#  endif
+#endif
+ 
+#endif
diff -urpN cygwin.org/thread.cc cygwin/thread.cc
--- cygwin.org/thread.cc	2004-02-10 12:09:00.180552000 +0100
+++ cygwin/thread.cc	2004-02-10 12:19:32.900358400 +0100
@@ -44,6 +44,7 @@ details. */
 #include <sys/timeb.h>
 #include <exceptions.h>
 #include <sys/fcntl.h>
+#include <sys/lock.h>
 
 extern int threadsafe;
 
@@ -54,6 +55,39 @@ __getreent ()
   return &_my_tls.local_clib;
 }
 
+extern "C" void
+__cygwin_lock_init (_LOCK_T *lock)
+{
+  *lock = NULL;
+  pthread_mutex_init ((pthread_mutex_t*) lock, NULL);
+}
+
+extern "C" void
+__cygwin_lock_init_recursive (_LOCK_T *lock)
+{
+  *lock = NULL;
+  if (!pthread_mutex_init ((pthread_mutex_t*) lock, NULL))
+    (*(pthread_mutex_t*)lock)->type = PTHREAD_MUTEX_RECURSIVE;
+}
+
+extern "C" void
+__cygwin_lock_fini (_LOCK_T *lock)
+{
+  pthread_mutex_destroy ((pthread_mutex_t*) lock);
+}
+
+extern "C" void
+__cygwin_lock_lock (_LOCK_T *lock)
+{
+  pthread_mutex_lock ((pthread_mutex_t*) lock);
+}
+
+extern "C" void
+__cygwin_lock_unlock (_LOCK_T *lock)
+{
+  pthread_mutex_unlock ((pthread_mutex_t*) lock);
+}
+
 inline LPCRITICAL_SECTION
 ResourceLocks::Lock (int _resid)
 {
