]> sourceware.org Git - newlib-cygwin.git/blobdiff - winsup/cygwin/thread.cc
* cygwin.din (pthread_attr_getstack): Export.
[newlib-cygwin.git] / winsup / cygwin / thread.cc
index 095682022643772f051e0d27fe3044634a5ba532..0085320ade95da9363c4a246c011e4b94b00ebdf 100644 (file)
@@ -1,9 +1,7 @@
 /* thread.cc: Locking and threading module functions
 
-   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc.
-
-   Originally written by Marco Fuykschot <marco@ddi.nl>
-   Substantialy enhanced by Robert Collins <rbtcollins@hotmail.com>
+   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
+   2006, 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
 
 This file is part of Cygwin.
 
@@ -18,7 +16,7 @@ details. */
    the constraints we either pretend to be conformant, or return an error
    code.
 
-   Some caveats: PROCESS_SHARED objects while they pretend to be process
+   Some caveats: PROCESS_SHARED objects, while they pretend to be process
    shared, may not actually work.  Some test cases are needed to determine
    win32's behaviour.  My suspicion is that the win32 handle needs to be
    opened with different flags for proper operation.
@@ -26,36 +24,58 @@ details. */
    R.Collins, April 2001.  */
 
 #ifdef HAVE_CONFIG_H
-# include "config.h"
 #endif
 
 #include "winsup.h"
-#include <limits.h>
-#include "cygerrno.h"
-#include <assert.h>
+#include "miscfuncs.h"
+#include "path.h"
 #include <stdlib.h>
 #include "pinfo.h"
 #include "sigproc.h"
 #include "perprocess.h"
-#include "security.h"
 #include "cygtls.h"
-#include <semaphore.h>
-#include <stdio.h>
-#include <sys/timeb.h>
-#include <exceptions.h>
-#include <sys/fcntl.h>
-#include <sys/lock.h>
+#include "fhandler.h"
+#include "dtable.h"
+#include "cygheap.h"
+#include "ntdll.h"
 
 extern "C" void __fp_lock_all ();
 extern "C" void __fp_unlock_all ();
+extern "C" int valid_sched_parameters(const struct sched_param *);
+extern "C" int sched_set_thread_priority(HANDLE thread, int priority);
 static inline verifyable_object_state
-  verifyable_object_isvalid (void const * objectptr, long magic,
+  verifyable_object_isvalid (void const * objectptr, thread_magic_t magic,
                             void *static_ptr1 = NULL,
                             void *static_ptr2 = NULL,
                             void *static_ptr3 = NULL);
 
 extern int threadsafe;
 
+const pthread_t pthread_mutex::_new_mutex = (pthread_t) 1;
+const pthread_t pthread_mutex::_unlocked_mutex = (pthread_t) 2;
+const pthread_t pthread_mutex::_destroyed_mutex = (pthread_t) 3;
+
+inline bool
+pthread_mutex::no_owner()
+{
+    int res;
+    if (!owner)
+      {
+       debug_printf ("NULL owner value");
+       res = 1;
+      }
+    else if (owner == _destroyed_mutex)
+      {
+       paranoid_printf ("attempt to use destroyed mutex");
+       res = 1;
+      }
+    else if (owner == _new_mutex || owner == _unlocked_mutex)
+      res = 1;
+    else
+      res = 0;
+    return res;
+}
+
 #undef __getreent
 extern "C" struct _reent *
 __getreent ()
@@ -84,13 +104,8 @@ __cygwin_lock_fini (_LOCK_T *lock)
 extern "C" void
 __cygwin_lock_lock (_LOCK_T *lock)
 {
-  if (MT_INTERFACE->threadcount <= 1)
-    paranoid_printf ("threadcount %d.  not locking", MT_INTERFACE->threadcount);
-  else
-    {
-      paranoid_printf ("threadcount %d.  locking", MT_INTERFACE->threadcount);
-      pthread_mutex_lock ((pthread_mutex_t*) lock);
-    }
+  paranoid_printf ("threadcount %d.  locking", MT_INTERFACE->threadcount);
+  pthread_mutex_lock ((pthread_mutex_t*) lock);
 }
 
 extern "C" int
@@ -103,25 +118,20 @@ __cygwin_lock_trylock (_LOCK_T *lock)
 extern "C" void
 __cygwin_lock_unlock (_LOCK_T *lock)
 {
-  if (MT_INTERFACE->threadcount <= 1)
-    paranoid_printf ("threadcount %d.  not unlocking", MT_INTERFACE->threadcount);
-  else
-    {
-      pthread_mutex_unlock ((pthread_mutex_t*) lock);
-      paranoid_printf ("threadcount %d.  unlocked", MT_INTERFACE->threadcount);
-    }
+  pthread_mutex_unlock ((pthread_mutex_t*) lock);
+  paranoid_printf ("threadcount %d.  unlocked", MT_INTERFACE->threadcount);
 }
 
 static inline verifyable_object_state
-verifyable_object_isvalid (void const * objectptr, long magic, void *static_ptr1,
+verifyable_object_isvalid (void const *objectptr, thread_magic_t magic, void *static_ptr1,
                           void *static_ptr2, void *static_ptr3)
 {
-  verifyable_object **object = (verifyable_object **) objectptr;
-
   myfault efault;
-  if (efault.faulted ())
+  if (efault.faulted (objectptr))
     return INVALID_OBJECT;
 
+  verifyable_object **object = (verifyable_object **) objectptr;
+
   if ((static_ptr1 && *object == static_ptr1) ||
       (static_ptr2 && *object == static_ptr2) ||
       (static_ptr3 && *object == static_ptr3))
@@ -165,57 +175,54 @@ pthread_key::is_good_object (pthread_key_t const *key)
 }
 
 inline bool
-pthread_mutex::is_good_object (pthread_mutex_t const *mutex)
+pthread_spinlock::is_good_object (pthread_spinlock_t const *mutex)
 {
-  if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC) != VALID_OBJECT)
+  if (verifyable_object_isvalid (mutex, PTHREAD_SPINLOCK_MAGIC) != VALID_OBJECT)
     return false;
   return true;
 }
 
 inline bool
-pthread_mutex::is_good_initializer (pthread_mutex_t const *mutex)
+pthread_mutex::is_good_object (pthread_mutex_t const *mutex)
 {
-  if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
-                                PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
-                                PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
-                                PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) != VALID_STATIC_OBJECT)
+  if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC) != VALID_OBJECT)
     return false;
   return true;
 }
 
 inline bool
-pthread_mutex::is_good_initializer_or_object (pthread_mutex_t const *mutex)
+pthread_mutex::is_initializer (pthread_mutex_t const *mutex)
 {
   if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
                                 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
                                 PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
-                                PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) == INVALID_OBJECT)
+                                PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) != VALID_STATIC_OBJECT)
     return false;
   return true;
 }
 
 inline bool
-pthread_mutex::is_good_initializer_or_bad_object (pthread_mutex_t const *mutex)
+pthread_mutex::is_initializer_or_object (pthread_mutex_t const *mutex)
 {
   if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
                                 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
                                 PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
-                                PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) == VALID_OBJECT)
+                                PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) == INVALID_OBJECT)
     return false;
   return true;
 }
 
+/* FIXME: Accommodate PTHREAD_MUTEX_ERRORCHECK */
 inline bool
-pthread_mutex::can_be_unlocked (pthread_mutex_t const *mutex)
+pthread_mutex::can_be_unlocked ()
 {
   pthread_t self = pthread::self ();
-
-  if (!is_good_object (mutex))
-    return false;
-  /*
-   * Check if the mutex is owned by the current thread and can be unlocked
-   */
-  return ((*mutex)->recursion_counter == 1 && pthread::equal ((*mutex)->owner, self));
+  /* Check if the mutex is owned by the current thread and can be unlocked.
+   * Also check for the ANONYMOUS owner to cover NORMAL mutexes as well. */
+  bool res = type == PTHREAD_MUTEX_NORMAL || no_owner ()
+            || (recursion_counter == 1 && pthread::equal (owner, self));
+  pthread_printf ("recursion_counter %d res %d", recursion_counter, res);
+  return res;
 }
 
 inline bool
@@ -244,7 +251,7 @@ pthread_cond::is_good_object (pthread_cond_t const *cond)
 }
 
 inline bool
-pthread_cond::is_good_initializer (pthread_cond_t const *cond)
+pthread_cond::is_initializer (pthread_cond_t const *cond)
 {
   if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) != VALID_STATIC_OBJECT)
     return false;
@@ -252,21 +259,13 @@ pthread_cond::is_good_initializer (pthread_cond_t const *cond)
 }
 
 inline bool
-pthread_cond::is_good_initializer_or_object (pthread_cond_t const *cond)
+pthread_cond::is_initializer_or_object (pthread_cond_t const *cond)
 {
   if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) == INVALID_OBJECT)
     return false;
   return true;
 }
 
-inline bool
-pthread_cond::is_good_initializer_or_bad_object (pthread_cond_t const *cond)
-{
-  if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) == VALID_OBJECT)
-    return false;
-  return true;
-}
-
 /* RW locks */
 inline bool
 pthread_rwlock::is_good_object (pthread_rwlock_t const *rwlock)
@@ -277,7 +276,7 @@ pthread_rwlock::is_good_object (pthread_rwlock_t const *rwlock)
 }
 
 inline bool
-pthread_rwlock::is_good_initializer (pthread_rwlock_t const *rwlock)
+pthread_rwlock::is_initializer (pthread_rwlock_t const *rwlock)
 {
   if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) != VALID_STATIC_OBJECT)
     return false;
@@ -285,21 +284,13 @@ pthread_rwlock::is_good_initializer (pthread_rwlock_t const *rwlock)
 }
 
 inline bool
-pthread_rwlock::is_good_initializer_or_object (pthread_rwlock_t const *rwlock)
+pthread_rwlock::is_initializer_or_object (pthread_rwlock_t const *rwlock)
 {
   if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) == INVALID_OBJECT)
     return false;
   return true;
 }
 
-inline bool
-pthread_rwlock::is_good_initializer_or_bad_object (pthread_rwlock_t const *rwlock)
-{
-  if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) == VALID_OBJECT)
-    return false;
-  return true;
-}
-
 inline bool
 semaphore::is_good_object (sem_t const * sem)
 {
@@ -308,43 +299,6 @@ semaphore::is_good_object (sem_t const * sem)
   return true;
 }
 
-LPCRITICAL_SECTION
-ResourceLocks::Lock (int _resid)
-{
-  return &lock;
-}
-
-void
-SetResourceLock (int _res_id, int _mode, const char *_function)
-{
-  EnterCriticalSection (user_data->resourcelocks->Lock (_res_id));
-}
-
-void
-ReleaseResourceLock (int _res_id, int _mode, const char *_function)
-{
-  LeaveCriticalSection (user_data->resourcelocks->Lock (_res_id));
-}
-
-void
-ResourceLocks::Init ()
-{
-  InitializeCriticalSection (&lock);
-  inited = true;
-  thread_printf ("lock %p inited by %p , %d", &lock, user_data, myself->pid);
-}
-
-void
-ResourceLocks::Delete ()
-{
-  if (inited)
-    {
-      thread_printf ("Close Resource Locks %p ", &lock);
-      DeleteCriticalSection (&lock);
-      inited = false;
-    }
-}
-
 void
 MTinterface::Init ()
 {
@@ -381,7 +335,7 @@ MTinterface::fixup_after_fork ()
 void
 pthread::init_mainthread ()
 {
-  pthread *thread = get_tls_self_pointer ();
+  pthread *thread = _my_tls.tid;
   if (!thread)
     {
       thread = new pthread ();
@@ -391,8 +345,9 @@ pthread::init_mainthread ()
 
   set_tls_self_pointer (thread);
   thread->thread_id = GetCurrentThreadId ();
-  if (!DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
-                       &thread->win32_obj_id, 0, FALSE, DUPLICATE_SAME_ACCESS))
+  if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
+                       GetCurrentProcess (), &thread->win32_obj_id,
+                       0, FALSE, DUPLICATE_SAME_ACCESS))
     api_fatal ("failed to create mainthread handle");
   if (!thread->create_cancel_event ())
     api_fatal ("couldn't create cancel event for main thread");
@@ -403,7 +358,7 @@ pthread::init_mainthread ()
 pthread *
 pthread::self ()
 {
-  pthread *thread = get_tls_self_pointer ();
+  pthread *thread = _my_tls.tid;
   if (!thread)
     {
       thread = pthread_null::get_null_pthread ();
@@ -412,12 +367,6 @@ pthread::self ()
   return thread;
 }
 
-pthread *
-pthread::get_tls_self_pointer ()
-{
-  return _my_tls.tid;
-}
-
 void
 pthread::set_tls_self_pointer (pthread *thread)
 {
@@ -429,12 +378,13 @@ List<pthread> pthread::threads;
 
 /* member methods */
 pthread::pthread ():verifyable_object (PTHREAD_MAGIC), win32_obj_id (0),
-                   valid (false), suspended (false),
+                   valid (false), suspended (false), canceled (false),
                    cancelstate (0), canceltype (0), cancel_event (0),
                    joiner (NULL), next (NULL), cleanup_stack (NULL)
 {
   if (this != pthread_null::get_null_pthread ())
     threads.insert (this);
+  parent_tls = &_my_tls;
 }
 
 pthread::~pthread ()
@@ -451,7 +401,7 @@ pthread::~pthread ()
 bool
 pthread::create_cancel_event ()
 {
-  cancel_event = ::CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
+  cancel_event = ::CreateEvent (&sec_none_nih, true, false, NULL);
   if (!cancel_event)
     {
       system_printf ("couldn't create cancel event, %E");
@@ -486,7 +436,7 @@ pthread::precreate (pthread_attr *newattr)
       return;
     }
   /* Change the mutex type to NORMAL to speed up mutex operations */
-  mutex.type = PTHREAD_MUTEX_NORMAL;
+  mutex.set_type (PTHREAD_MUTEX_NORMAL);
   if (!create_cancel_event ())
     magic = 0;
 }
@@ -517,7 +467,7 @@ pthread::create (void *(*func) (void *), pthread_attr *newattr,
     {
       postcreate ();
       while (!cygtls)
-       low_priority_sleep (0);
+       yield ();
     }
   retval = magic;
   mutex.unlock ();
@@ -563,14 +513,10 @@ pthread::exit (void *value_ptr)
     _my_tls.local_clib.__sdidinit = 0;
   (_reclaim_reent) (_REENT);
 
-
   if (InterlockedDecrement (&MT_INTERFACE->threadcount) == 0)
     ::exit (0);
   else
-    {
-      _my_tls.remove (INFINITE);
-      ExitThread (0);
-    }
+    ExitThread (0);
 }
 
 int
@@ -592,6 +538,7 @@ pthread::cancel ()
     {
       // cancel deferred
       mutex.unlock ();
+      canceled = true;
       SetEvent (cancel_event);
       return 0;
     }
@@ -616,189 +563,339 @@ pthread::cancel ()
   ResumeThread (win32_obj_id);
 
   return 0;
-/*
-  TODO: insert  pthread_testcancel into the required functions
-  the required function list is: *indicates done, X indicates not present in cygwin.
-aio_suspend ()
-*close ()
-*creat ()
-fcntl ()
-fsync ()
-getmsg ()
-getpmsg ()
-lockf ()
-mq_receive ()
-mq_send ()
-msgrcv ()
-msgsnd ()
-msync ()
-nanosleep ()
-open ()
-*pause ()
-poll ()
-pread ()
-*pthread_cond_timedwait ()
-*pthread_cond_wait ()
-*pthread_join ()
-*pthread_testcancel ()
-putmsg ()
-putpmsg ()
-pwrite ()
-read ()
-readv ()
-select ()
-*sem_wait ()
-*sigpause ()
-*sigsuspend ()
-sigtimedwait ()
-sigwait ()
-sigwaitinfo ()
-*sleep ()
-*system ()
-tcdrain ()
-*usleep ()
-*wait ()
-*wait3()
-waitid ()
-*waitpid ()
-write ()
-writev ()
-
-the optional list is:
-catclose ()
-catgets ()
-catopen ()
-closedir ()
-closelog ()
-ctermid ()
-dbm_close ()
-dbm_delete ()
-dbm_fetch ()
-dbm_nextkey ()
-dbm_open ()
-dbm_store ()
-dlclose ()
-dlopen ()
-endgrent ()
-endpwent ()
-endutxent ()
-fclose ()
-fcntl ()
-fflush ()
-fgetc ()
-fgetpos ()
-fgets ()
-fgetwc ()
-fgetws ()
-fopen ()
-fprintf ()
-fputc ()
-fputs ()
-fputwc ()
-fputws ()
-fread ()
-freopen ()
-fscanf ()
-fseek ()
-fseeko ()
-fsetpos ()
-ftell ()
-ftello ()
-ftw ()
-fwprintf ()
-fwrite ()
-fwscanf ()
-getc ()
-getc_unlocked ()
-getchar ()
-getchar_unlocked ()
-getcwd ()
-getdate ()
-getgrent ()
-getgrgid ()
-getgrgid_r ()
-getgrnam ()
-getgrnam_r ()
-getlogin ()
-getlogin_r ()
-getpwent ()
-*getpwnam ()
-*getpwnam_r ()
-*getpwuid ()
-*getpwuid_r ()
-gets ()
-getutxent ()
-getutxid ()
-getutxline ()
-getw ()
-getwc ()
-getwchar ()
-getwd ()
-glob ()
-iconv_close ()
-iconv_open ()
-ioctl ()
-lseek ()
-mkstemp ()
-nftw ()
-opendir ()
-openlog ()
-pclose ()
-perror ()
-popen ()
-printf ()
-putc ()
-putc_unlocked ()
-putchar ()
-putchar_unlocked ()
-puts ()
-pututxline ()
-putw ()
-putwc ()
-putwchar ()
-readdir ()
-readdir_r ()
-remove ()
-rename ()
-rewind ()
-rewinddir ()
-scanf ()
-seekdir ()
-semop ()
-setgrent ()
-setpwent ()
-setutxent ()
-strerror ()
-syslog ()
-tmpfile ()
-tmpnam ()
-ttyname ()
-ttyname_r ()
-ungetc ()
-ungetwc ()
-unlink ()
-vfprintf ()
-vfwprintf ()
-vprintf ()
-vwprintf ()
-wprintf ()
-wscanf ()
-
-Note, that for fcntl (), for any value of the cmd argument.
-
-And we must not introduce cancellation points anywhere else that's part of the posix or
-opengroup specs.
- */
 }
 
+/* TODO: Insert pthread_testcancel into the required functions.
+
+   Here are the lists of required and optional functions per POSIX.1-2001
+   and POSIX.1-2008. A star (*) indicates that the Cygwin function already
+   is a cancellation point (aka "calls pthread_testcancel"), an o (o)
+   indicates that the function is not implemented in Cygwin.
+
+   Required cancellation points:
+
+    * accept ()
+    o aio_suspend ()
+    o clock_nanosleep ()
+    * close ()
+    * connect ()
+    * creat ()
+    * fcntl () F_SETLKW
+    * fdatasync ()
+    * fsync ()
+    o getmsg ()
+    o getpmsg ()
+    * lockf () F_LOCK
+    * mq_receive ()
+    * mq_send ()
+    * mq_timedreceive ()
+    * mq_timedsend ()
+      msgrcv ()
+      msgsnd ()
+    * msync ()
+    * nanosleep ()
+    * open ()
+    * openat ()
+    * pause ()
+      poll ()
+    * pread ()
+      pselect ()
+    * pthread_cond_timedwait ()
+    * pthread_cond_wait ()
+    * pthread_join ()
+    * pthread_testcancel ()
+    o putmsg ()
+    o putpmsg ()
+    * pwrite ()
+    * read ()
+    * readv ()
+    * recv ()
+    * recvfrom ()
+    * recvmsg ()
+      select ()
+    * sem_timedwait ()
+    * sem_wait ()
+    * send ()
+    * sendmsg ()
+    * sendto ()
+    * sigpause ()
+    * sigsuspend ()
+    o sigtimedwait ()
+    * sigwait ()
+    * sigwaitinfo ()
+    * sleep ()
+    * system ()
+    * tcdrain ()
+    * usleep ()
+    * wait ()
+    * wait3()
+    o waitid ()
+    * waitpid ()
+    * write ()
+    * writev ()
+
+   Optional cancellation points:
+
+      access ()
+      asctime ()
+      asctime_r ()
+      catclose ()      Implemented externally: libcatgets
+      catgets ()       Implemented externally: libcatgets
+      catopen ()       Implemented externally: libcatgets
+      chmod ()
+      chown ()
+      closedir ()
+      closelog ()
+      ctermid ()
+      ctime ()
+      ctime_r ()
+      dbm_close ()     Implemented externally: libgdbm
+      dbm_delete ()    Implemented externally: libgdbm
+      dbm_fetch ()     Implemented externally: libgdbm
+      dbm_nextkey ()   Implemented externally: libgdbm
+      dbm_open ()      Implemented externally: libgdbm
+      dbm_store ()     Implemented externally: libgdbm
+      dlclose ()
+      dlopen ()
+      dprintf ()
+      endgrent ()
+      endhostent ()
+    o endnetent ()
+      endprotoent ()
+      endpwent ()
+      endservent ()
+      endutxent ()
+      faccessat ()
+      fchmod ()
+      fchmodat ()
+      fchown ()
+      fchownat ()
+    * fclose ()
+    * fcntl () (any value)
+      fflush ()
+      fgetc ()
+      fgetpos ()
+      fgets ()
+      fgetwc ()
+      fgetws ()
+    o fmtmsg ()
+      fopen ()
+      fpathconf ()
+      fprintf ()
+      fputc ()
+      fputs ()
+      fputwc ()
+      fputws ()
+      fread ()
+      freopen ()
+      fscanf ()
+      fseek ()
+      fseeko ()
+      fsetpos ()
+      fstat ()
+      fstatat ()
+      ftell ()
+      ftello ()
+      ftw ()
+      futimens ()
+      fwprintf ()
+      fwrite ()
+      fwscanf ()
+      getaddrinfo ()
+      getc ()
+      getc_unlocked ()
+      getchar ()
+      getchar_unlocked ()
+      getcwd ()
+    o getdate ()
+      getdelim ()
+      getgrent ()
+      getgrgid ()
+      getgrgid_r ()
+      getgrnam ()
+      getgrnam_r ()
+      gethostbyaddr ()
+      gethostbyname ()
+      gethostent ()
+      gethostid ()
+      gethostname ()
+      getline ()
+      getlogin ()
+      getlogin_r ()
+      getnameinfo ()
+    o getnetbyaddr ()
+    o getnetbyname ()
+    o getnetent ()
+      getopt () (if opterr is nonzero)
+      getprotobyname ()
+      getprotobynumber ()
+      getprotoent ()
+      getpwent ()
+    * getpwnam ()
+    * getpwnam_r ()
+    * getpwuid ()
+    * getpwuid_r ()
+      gets ()
+      getservbyname ()
+      getservbyport ()
+      getservent ()
+      getutxent ()
+      getutxid ()
+      getutxline ()
+      getwc ()
+      getwchar ()
+      getwd ()
+      glob ()
+      iconv_close ()   Implemented externally: libiconv
+      iconv_open ()    Implemented externally: libiconv
+      ioctl ()
+      link ()
+      linkat ()
+    o lio_listio ()
+      localtime ()
+      localtime_r ()
+    * lockf ()
+      lseek ()
+      lstat ()
+      mkdir ()
+      mkdirat ()
+      mkdtemp ()
+      mkfifo ()
+      mkfifoat ()
+      mknod ()
+      mknodat ()
+      mkstemp ()
+      mktime ()
+      nftw ()
+      opendir ()
+      openlog ()
+      pathconf ()
+      pclose ()
+      perror ()
+      popen ()
+      posix_fadvise ()
+      posix_fallocate ()
+      posix_madvise ()
+      posix_openpt ()
+    o posix_spawn ()
+    o posix_spawnp ()
+    o posix_trace_clear ()
+    o posix_trace_close ()
+    o posix_trace_create ()
+    o posix_trace_create_withlog ()
+    o posix_trace_eventtypelist_getnext_id ()
+    o posix_trace_eventtypelist_rewind ()
+    o posix_trace_flush ()
+    o posix_trace_get_attr ()
+    o posix_trace_get_filter ()
+    o posix_trace_get_status ()
+    o posix_trace_getnext_event ()
+    o posix_trace_open ()
+    o posix_trace_rewind ()
+    o posix_trace_set_filter ()
+    o posix_trace_shutdown ()
+    o posix_trace_timedgetnext_event ()
+    o posix_typed_mem_open ()
+      printf ()
+    o psiginfo ()
+    o psignal ()
+      pthread_rwlock_rdlock ()
+      pthread_rwlock_timedrdlock ()
+      pthread_rwlock_timedwrlock ()
+      pthread_rwlock_wrlock ()
+      putc ()
+      putc_unlocked ()
+      putchar ()
+      putchar_unlocked ()
+      puts ()
+      pututxline ()
+      putwc ()
+      putwchar ()
+      readdir ()
+      readdir_r ()
+      readlink ()
+      readlinkat ()
+      remove ()
+      rename ()
+      renameat ()
+      rewind ()
+      rewinddir ()
+      scandir ()
+      scanf ()
+      seekdir ()
+      semop ()
+      setgrent ()
+      sethostent ()
+    o setnetent ()
+      setprotoent ()
+      setpwent ()
+      setservent ()
+      setutxent ()
+      sigpause ()
+      stat ()
+      strerror ()
+      strerror_r ()
+      strftime ()
+      symlink ()
+      symlinkat ()
+      sync ()
+      syslog ()
+      tmpfile ()
+      tmpnam ()
+      ttyname ()
+      ttyname_r ()
+      tzset ()
+      ungetc ()
+      ungetwc ()
+      unlink ()
+      unlinkat ()
+      utime ()
+      utimensat ()
+      utimes ()
+      vdprintf ()
+      vfprintf ()
+      vfwprintf ()
+      vprintf ()
+      vwprintf ()
+      wcsftime ()
+      wordexp ()
+      wprintf ()
+      wscanf ()
+
+   An implementation may also mark other functions not specified in the
+   standard as cancellation points.  In particular, an implementation is
+   likely to mark any nonstandard function that may block as a
+   cancellation point. */
+
 void
 pthread::testcancel ()
 {
   if (cancelstate == PTHREAD_CANCEL_DISABLE)
     return;
 
-  if (WaitForSingleObject (cancel_event, 0) == WAIT_OBJECT_0)
-    cancel_self ();
+  /* We check for the canceled flag first.  This allows to use the
+     pthread_testcancel function a lot without adding the overhead of
+     an OS call.  Only if the thread is marked as canceled, we wait for
+     cancel_event being really set, on the off-chance that pthread_cancel
+     gets interrupted before calling SetEvent. */
+  if (canceled)
+    {
+      WaitForSingleObject (cancel_event, INFINITE);
+      cancel_self ();
+    }
+}
+
+/* Return cancel event handle if it exists *and* cancel is not disabled.
+   This function is supposed to be used from other functions which are
+   cancelable and need the cancel event in a WFMO call. */
+HANDLE
+pthread::get_cancel_event ()
+{
+  pthread_t thread = pthread::self ();
+
+  return (thread && thread->cancel_event
+         && thread->cancelstate != PTHREAD_CANCEL_DISABLE)
+         ? thread->cancel_event : NULL;
 }
 
 void
@@ -808,7 +905,8 @@ pthread::static_cancel_self ()
 }
 
 DWORD
-cancelable_wait (HANDLE object, DWORD timeout, const cw_cancel_action cancel_action,
+cancelable_wait (HANDLE object, DWORD timeout,
+                const cw_cancel_action cancel_action,
                 const enum cw_sig_wait sig_wait)
 {
   DWORD res;
@@ -824,7 +922,7 @@ cancelable_wait (HANDLE object, DWORD timeout, const cw_cancel_action cancel_act
   DWORD cancel_n;
   if (cancel_action == cw_no_cancel || !pthread::is_good_object (&thread) ||
       thread->cancelstate == PTHREAD_CANCEL_DISABLE)
-    cancel_n = (DWORD) -1;
+    cancel_n = WAIT_TIMEOUT + 1;
   else
     {
       cancel_n = WAIT_OBJECT_0 + num++;
@@ -832,8 +930,8 @@ cancelable_wait (HANDLE object, DWORD timeout, const cw_cancel_action cancel_act
     }
 
   DWORD sig_n;
-  if (sig_wait == cw_sig_nosig || &_my_tls != _main_tls)
-    sig_n = (DWORD) -1;
+  if (sig_wait == cw_sig_nosig)
+    sig_n = WAIT_TIMEOUT + 1;
   else
     {
       sig_n = WAIT_OBJECT_0 + num++;
@@ -964,6 +1062,7 @@ pthread::_fixup_after_fork ()
       magic = 0;
       valid = false;
       win32_obj_id = NULL;
+      canceled = false;
       cancel_event = NULL;
     }
 }
@@ -986,7 +1085,7 @@ pthread::resume ()
 
 pthread_attr::pthread_attr ():verifyable_object (PTHREAD_ATTR_MAGIC),
 joinable (PTHREAD_CREATE_JOINABLE), contentionscope (PTHREAD_SCOPE_PROCESS),
-inheritsched (PTHREAD_INHERIT_SCHED), stacksize (0)
+inheritsched (PTHREAD_INHERIT_SCHED), stackaddr (NULL), stacksize (0)
 {
   schedparam.sched_priority = 0;
 }
@@ -1044,7 +1143,7 @@ pthread_cond::pthread_cond (pthread_condattr *attr) :
    * Change the mutex type to NORMAL.
    * This mutex MUST be of type normal
   */
-  mtx_in.type = PTHREAD_MUTEX_NORMAL;
+  mtx_in.set_type (PTHREAD_MUTEX_NORMAL);
 
   verifyable_mutex_obj = &mtx_out;
   if (!pthread_mutex::is_good_object (&verifyable_mutex_obj))
@@ -1054,12 +1153,12 @@ pthread_cond::pthread_cond (pthread_condattr *attr) :
       return;
     }
   /* Change the mutex type to NORMAL to speed up mutex operations */
-  mtx_out.type = PTHREAD_MUTEX_NORMAL;
+  mtx_out.set_type (PTHREAD_MUTEX_NORMAL);
 
   sem_wait = ::CreateSemaphore (&sec_none_nih, 0, LONG_MAX, NULL);
   if (!sem_wait)
     {
-      debug_printf ("CreateSemaphore failed. %E");
+      pthread_printf ("CreateSemaphore failed. %E");
       magic = 0;
       return;
     }
@@ -1258,7 +1357,7 @@ pthread_rwlock::pthread_rwlock (pthread_rwlockattr *attr) :
       return;
     }
   /* Change the mutex type to NORMAL to speed up mutex operations */
-  mtx.type = PTHREAD_MUTEX_NORMAL;
+  mtx.set_type (PTHREAD_MUTEX_NORMAL);
 
   verifyable_cond_obj = &cond_readers;
   if (!pthread_cond::is_good_object (&verifyable_cond_obj))
@@ -1294,9 +1393,13 @@ pthread_rwlock::rdlock ()
 
   mtx.lock ();
 
-  if (lookup_reader (self))
+  reader = lookup_reader (self);
+  if (reader)
     {
-      result = EDEADLK;
+      if (reader->n < ULONG_MAX)
+       ++reader->n;
+      else
+       errno = EAGAIN;
       goto DONE;
     }
 
@@ -1319,6 +1422,7 @@ pthread_rwlock::rdlock ()
     }
 
   reader->thread = self;
+  reader->n = 1;
   add_reader (reader);
 
  DONE:
@@ -1339,10 +1443,15 @@ pthread_rwlock::tryrdlock ()
     result = EBUSY;
   else
     {
-      struct RWLOCK_READER *reader = new struct RWLOCK_READER;
-      if (reader)
+      struct RWLOCK_READER *reader;
+
+      reader = lookup_reader (self);
+      if (reader && reader->n < ULONG_MAX)
+       ++reader->n;
+      else if ((reader = new struct RWLOCK_READER))
        {
          reader->thread = self;
+         reader->n = 1;
          add_reader (reader);
        }
       else
@@ -1432,6 +1541,8 @@ pthread_rwlock::unlock ()
          result = EPERM;
          goto DONE;
        }
+      if (--reader->n > 0)
+       goto DONE;
 
       remove_reader (reader);
       delete reader;
@@ -1578,28 +1689,7 @@ pthread_key::run_destructor ()
     }
 }
 
-/* pshared mutexs:
-
-   REMOVED FROM CURRENT. These can be reinstated with the daemon, when all the
-   gymnastics can be a lot easier.
-
-   the mutex_t (size 4) is not used as a verifyable object because we cannot
-   guarantee the same address space for all processes.
-   we use the following:
-   high bit set (never a valid address).
-   second byte is reserved for the priority.
-   third byte is reserved
-   fourth byte is the mutex id. (max 255 cygwin mutexs system wide).
-   creating mutex's does get slower and slower, but as creation is a one time
-   job, it should never become an issue
-
-   And if you're looking at this and thinking, why not an array in cygwin for all mutexs,
-   - you incur a penalty on _every_ mutex call and you have toserialise them all.
-   ... Bad karma.
-
-   option 2? put everything in userspace and update the ABI?
-   - bad karma as well - the HANDLE, while identical across process's,
-   Isn't duplicated, it's reopened. */
+/* pshared mutexs */
 
 /* static members */
 
@@ -1608,9 +1698,6 @@ List<pthread_mutex> pthread_mutex::mutexes;
 /* This is used for mutex creation protection within a single process only */
 fast_mutex NO_COPY pthread_mutex::mutex_initialization_lock;
 
-/* We can only be called once.
-   TODO: (no rush) use a non copied memory section to
-   hold an initialization flag.  */
 void
 pthread_mutex::init_mutex ()
 {
@@ -1619,50 +1706,54 @@ pthread_mutex::init_mutex ()
 }
 
 pthread_mutex::pthread_mutex (pthread_mutexattr *attr) :
-  verifyable_object (PTHREAD_MUTEX_MAGIC),
+  verifyable_object (0),       /* set magic to zero initially */
   lock_counter (0),
-  win32_obj_id (NULL), recursion_counter (0),
-  condwaits (0), owner (NULL), type (PTHREAD_MUTEX_ERRORCHECK),
+  win32_obj_id (NULL), owner (_new_mutex),
+#ifdef DEBUGGING
+  tid (0),
+#endif
+  recursion_counter (0), condwaits (0),
+  type (PTHREAD_MUTEX_ERRORCHECK),
   pshared (PTHREAD_PROCESS_PRIVATE)
 {
-  win32_obj_id = ::CreateSemaphore (&sec_none_nih, 0, LONG_MAX, NULL);
+  win32_obj_id = ::CreateEvent (&sec_none_nih, false, false, NULL);
   if (!win32_obj_id)
-    {
-      magic = 0;
-      return;
-    }
+    return;
   /*attr checked in the C call */
-  if (attr)
-    {
-      if (attr->pshared == PTHREAD_PROCESS_SHARED)
-       {
-         // fail
-         magic = 0;
-         return;
-       }
-
-      type = attr->mutextype;
-    }
+  if (!attr)
+    /* handled in the caller */;
+  else if (attr->pshared != PTHREAD_PROCESS_SHARED)
+    type = attr->mutextype;
+  else
+    return;            /* Not implemented */
 
+  magic = PTHREAD_MUTEX_MAGIC;
   mutexes.insert (this);
 }
 
 pthread_mutex::~pthread_mutex ()
 {
   if (win32_obj_id)
-    CloseHandle (win32_obj_id);
+    {
+      CloseHandle (win32_obj_id);
+      win32_obj_id = NULL;
+    }
 
   mutexes.remove (this);
+  owner = _destroyed_mutex;
+  magic = 0;
 }
 
 int
-pthread_mutex::_lock (pthread_t self)
+pthread_mutex::lock ()
 {
+  pthread_t self = ::pthread_self ();
   int result = 0;
 
-  if (InterlockedIncrement ((long *)&lock_counter) == 1)
+  if (InterlockedIncrement ((long *) &lock_counter) == 1)
     set_owner (self);
-  else if (type == PTHREAD_MUTEX_NORMAL || !pthread::equal (owner, self))
+  else if (type == PTHREAD_MUTEX_NORMAL /* potentially causes deadlock */
+          || !pthread::equal (owner, self))
     {
       cancelable_wait (win32_obj_id, INFINITE, cw_no_cancel, cw_sig_resume);
       set_owner (self);
@@ -1676,12 +1767,44 @@ pthread_mutex::_lock (pthread_t self)
        result = EDEADLK;
     }
 
+  pthread_printf ("mutex %p, self %p, owner %p, lock_counter %d, recursion_counter %d",
+                 this, self, owner, lock_counter, recursion_counter);
   return result;
 }
 
 int
-pthread_mutex::_trylock (pthread_t self)
+pthread_mutex::unlock ()
+{
+  int res = 0;
+  pthread_t self = ::pthread_self ();
+  if (type == PTHREAD_MUTEX_NORMAL)
+    /* no error checking */;
+  else if (no_owner ())
+    res = type == PTHREAD_MUTEX_ERRORCHECK ? EINVAL : 0;
+  else if (!pthread::equal (owner, self))
+    res = EPERM;
+  if (!res && recursion_counter > 0 && --recursion_counter == 0)
+    /* Don't try to unlock anything if recursion_counter == 0.
+       This means the mutex was never locked or that we've forked. */
+    {
+      owner = (pthread_t) _unlocked_mutex;
+#ifdef DEBUGGING
+      tid = 0;
+#endif
+      if (InterlockedDecrement ((long *) &lock_counter))
+       ::SetEvent (win32_obj_id); // Another thread is waiting
+      res = 0;
+    }
+
+  pthread_printf ("mutex %p, owner %p, self %p, lock_counter %d, recursion_counter %d, type %d, res %d",
+                 this, owner, self, lock_counter, recursion_counter, type, res);
+  return res;
+}
+
+int
+pthread_mutex::trylock ()
 {
+  pthread_t self = ::pthread_self ();
   int result = 0;
 
   if (InterlockedCompareExchange ((long *) &lock_counter, 1, 0) == 0)
@@ -1695,32 +1818,15 @@ pthread_mutex::_trylock (pthread_t self)
 }
 
 int
-pthread_mutex::_unlock (pthread_t self)
-{
-  if (!pthread::equal (owner, self))
-    return EPERM;
-
-  if (--recursion_counter == 0)
-    {
-      owner = NULL;
-      if (InterlockedDecrement ((long *)&lock_counter))
-       // Another thread is waiting
-       ::ReleaseSemaphore (win32_obj_id, 1, NULL);
-    }
-
-  return 0;
-}
-
-int
-pthread_mutex::_destroy (pthread_t self)
+pthread_mutex::destroy ()
 {
-  if (condwaits || _trylock (self))
+  if (condwaits || trylock ())
     // Do not destroy a condwaited or locked mutex
     return EBUSY;
-  else if (recursion_counter != 1)
+  else if (recursion_counter > 1)
     {
       // Do not destroy a recursive locked mutex
-      --recursion_counter;
+      recursion_counter--;
       return EBUSY;
     }
 
@@ -1731,22 +1837,20 @@ pthread_mutex::_destroy (pthread_t self)
 void
 pthread_mutex::_fixup_after_fork ()
 {
-  debug_printf ("mutex %x in _fixup_after_fork", this);
+  pthread_printf ("mutex %p", this);
   if (pshared != PTHREAD_PROCESS_PRIVATE)
-    api_fatal ("pthread_mutex::_fixup_after_fork () doesn'tunderstand PROCESS_SHARED mutex's");
-
-  if (owner == NULL)
-    /* mutex has no owner, reset to initial */
-    lock_counter = 0;
-  else if (lock_counter != 0)
-    /* All waiting threads are gone after a fork */
-    lock_counter = 1;
-
-  win32_obj_id = ::CreateSemaphore (&sec_none_nih, 0, LONG_MAX, NULL);
-  if (!win32_obj_id)
-    api_fatal ("pthread_mutex::_fixup_after_fork () failed to recreate win32 semaphore for mutex");
+    api_fatal ("pthread_mutex::_fixup_after_fork () doesn't understand PROCESS_SHARED mutex's");
 
+  /* All waiting threads are gone after a fork */
+  recursion_counter = 0;
+  lock_counter = 0;
   condwaits = 0;
+#ifdef DEBUGGING
+  tid = 0xffffffff;    /* Don't know the tid after a fork */
+#endif
+  win32_obj_id = ::CreateEvent (&sec_none_nih, false, false, NULL);
+  if (!win32_obj_id)
+    api_fatal ("pthread_mutex::_fixup_after_fork () failed to recreate win32 event for mutex");
 }
 
 pthread_mutexattr::pthread_mutexattr ():verifyable_object (PTHREAD_MUTEXATTR_MAGIC),
@@ -1758,281 +1862,140 @@ pthread_mutexattr::~pthread_mutexattr ()
 {
 }
 
-List<semaphore> semaphore::semaphores;
+/* pshared spinlocks
 
-semaphore::semaphore (int pshared, unsigned int value)
-: verifyable_object (SEM_MAGIC),
-  shared (pshared),
-  currentvalue (value),
-  name (NULL)
-{
-  SECURITY_ATTRIBUTES sa = (pshared != PTHREAD_PROCESS_PRIVATE)
-                          ? sec_all : sec_none_nih;
-  this->win32_obj_id = ::CreateSemaphore (&sa, value, LONG_MAX, NULL);
-  if (!this->win32_obj_id)
-    magic = 0;
+   The infrastructure is provided by the underlying pthread_mutex class.
+   The rest is a simplification implementing spin locking. */
 
-  semaphores.insert (this);
+pthread_spinlock::pthread_spinlock (int pshared) :
+  pthread_mutex (NULL)
+{
+  magic = PTHREAD_SPINLOCK_MAGIC;
+  set_type (PTHREAD_MUTEX_NORMAL);
+  set_shared (pshared);
 }
 
-semaphore::semaphore (const char *sem_name, int oflag, mode_t mode,
-                                 unsigned int value)
-: verifyable_object (SEM_MAGIC),
-  shared (PTHREAD_PROCESS_SHARED),
-  currentvalue (value),                /* Unused for named semaphores. */
-  name (NULL)
+int
+pthread_spinlock::lock ()
 {
-  if (oflag & O_CREAT)
+  pthread_t self = ::pthread_self ();
+  int result = -1;
+
+  do
     {
-      SECURITY_ATTRIBUTES sa = sec_all;
-      security_descriptor sd;
-      if (allow_ntsec)
-       set_security_attribute (mode, &sa, sd);
-      this->win32_obj_id = ::CreateSemaphore (&sa, value, LONG_MAX, sem_name);
-      if (!this->win32_obj_id)
-       magic = 0;
-      if (GetLastError () == ERROR_ALREADY_EXISTS && (oflag & O_EXCL))
+      if (InterlockedExchange ((long *) &lock_counter, 1) == 0)
        {
-         __seterrno ();
-         CloseHandle (this->win32_obj_id);
-         magic = 0;
+         set_owner (self);
+         result = 0;
        }
+      else if (pthread::equal (owner, self))
+       result = EDEADLK;
+      else /* Minimal timeout to minimize CPU usage while still spinning. */
+       cancelable_wait (win32_obj_id, 1L, cw_no_cancel, cw_sig_resume);
     }
+  while (result == -1);
+  pthread_printf ("spinlock %p, self %p, owner %p", this, self, owner);
+  return result;
+}
+
+int
+pthread_spinlock::unlock ()
+{
+  pthread_t self = ::pthread_self ();
+  int result = 0;
+
+  if (!pthread::equal (owner, self))
+    result = EPERM;
   else
     {
-      this->win32_obj_id = ::OpenSemaphore (SEMAPHORE_ALL_ACCESS, FALSE,
-                                           sem_name);
-      if (!this->win32_obj_id)
-       {
-         __seterrno ();
-         magic = 0;
-       }
-    }
-  if (magic)
-    {
-      name = new char [strlen (sem_name + 1)];
-      if (!name)
-       {
-         set_errno (ENOSPC);
-         CloseHandle (this->win32_obj_id);
-         magic = 0;
-       }
-      else
-       strcpy (name, sem_name);
+      owner = (pthread_t) _unlocked_mutex;
+#ifdef DEBUGGING
+      tid = 0;
+#endif
+      InterlockedExchange ((long *) &lock_counter, 0);
+      ::SetEvent (win32_obj_id);
+      result = 0;
     }
-
-  semaphores.insert (this);
+  pthread_printf ("spinlock %p, owner %p, self %p, res %d",
+                 this, owner, self, result);
+  return result;
 }
 
-semaphore::~semaphore ()
+DWORD WINAPI
+pthread::thread_init_wrapper (void *arg)
 {
-  if (win32_obj_id)
-    CloseHandle (win32_obj_id);
+  pthread *thread = (pthread *) arg;
+  set_tls_self_pointer (thread);
+
+  thread->mutex.lock ();
 
-  delete [] name;
+  // if thread is detached force cleanup on exit
+  if (thread->attr.joinable == PTHREAD_CREATE_DETACHED && thread->joiner == NULL)
+    thread->joiner = thread;
+  _my_tls.sigmask = thread->parent_tls->sigmask;
+  thread->mutex.unlock ();
 
-  semaphores.remove (this);
+  thread_printf ("started thread %p %p %p %p %p %p", arg, &_my_tls.local_clib,
+                _impure_ptr, thread, thread->function, thread->arg);
+
+  // call the user's thread
+  void *ret = thread->function (thread->arg);
+
+  thread->exit (ret);
+
+  return 0;    // just for show.  Never returns.
 }
 
-void
-semaphore::_post ()
+unsigned long
+pthread::getsequence_np ()
 {
-  if (ReleaseSemaphore (win32_obj_id, 1, &currentvalue))
-    currentvalue++;
+  return get_thread_id ();
 }
 
 int
-semaphore::_getvalue (int *sval)
+pthread::create (pthread_t *thread, const pthread_attr_t *attr,
+                 void *(*start_routine) (void *), void *arg)
 {
-  long val;
+  if (attr && !pthread_attr::is_good_object (attr))
+    return EINVAL;
 
-  switch (WaitForSingleObject (win32_obj_id, 0))
+  *thread = new pthread ();
+  if (!(*thread)->create (start_routine, attr ? *attr : NULL, arg))
     {
-      case WAIT_OBJECT_0:
-       ReleaseSemaphore (win32_obj_id, 1, &val);
-       *sval = val + 1;
-       break;
-      case WAIT_TIMEOUT:
-       *sval = 0;
-       break;
-      default:
-       set_errno (EAGAIN);
-       return -1;
+      delete (*thread);
+      *thread = NULL;
+      return EAGAIN;
     }
+
   return 0;
 }
 
 int
-semaphore::_trywait ()
+pthread::once (pthread_once_t *once_control, void (*init_routine) (void))
 {
-  /* FIXME: signals should be able to interrupt semaphores...
-    We probably need WaitForMultipleObjects here.  */
-  if (WaitForSingleObject (win32_obj_id, 0) == WAIT_TIMEOUT)
+  // already done ?
+  if (once_control->state)
+    return 0;
+
+  pthread_mutex_lock (&once_control->mutex);
+  /* Here we must set a cancellation handler to unlock the mutex if needed */
+  /* but a cancellation handler is not the right thing. We need this in the thread
+   *cleanup routine. Assumption: a thread can only be in one pthread_once routine
+   *at a time. Stote a mutex_t *in the pthread_structure. if that's non null unlock
+   *on pthread_exit ();
+   */
+  if (!once_control->state)
     {
-      set_errno (EAGAIN);
-      return -1;
+      init_routine ();
+      once_control->state = 1;
     }
-  currentvalue--;
+  /* Here we must remove our cancellation handler */
+  pthread_mutex_unlock (&once_control->mutex);
   return 0;
 }
 
 int
-semaphore::_timedwait (const struct timespec *abstime)
-{
-  struct timeval tv;
-  long waitlength;
-
-  myfault efault;
-  if (efault.faulted ())
-    {
-      /* According to SUSv3, abstime need not be checked for validity,
-        if the semaphore can be locked immediately. */
-      if (!_trywait ())
-       return 0;
-      set_errno (EINVAL);
-      return -1;
-    }
-
-  gettimeofday (&tv, NULL);
-  waitlength = abstime->tv_sec * 1000 + abstime->tv_nsec / (1000 * 1000);
-  waitlength -= tv.tv_sec * 1000 + tv.tv_usec / 1000;
-  if (waitlength < 0)
-    waitlength = 0;
-  switch (cancelable_wait (win32_obj_id, waitlength, cw_cancel_self, cw_sig_eintr))
-    {
-    case WAIT_OBJECT_0:
-      currentvalue--;
-      break;
-    case WAIT_SIGNALED:
-      set_errno (EINTR);
-      return -1;
-    case WAIT_TIMEOUT:
-      set_errno (ETIMEDOUT);
-      return -1;
-    default:
-      debug_printf ("cancelable_wait failed. %E");
-      __seterrno ();
-      return -1;
-    }
-  return 0;
-}
-
-int
-semaphore::_wait ()
-{
-  switch (cancelable_wait (win32_obj_id, INFINITE, cw_cancel_self, cw_sig_eintr))
-    {
-    case WAIT_OBJECT_0:
-      currentvalue--;
-      break;
-    case WAIT_SIGNALED:
-      set_errno (EINTR);
-      return -1;
-    default:
-      debug_printf ("cancelable_wait failed. %E");
-      break;
-    }
-  return 0;
-}
-
-void
-semaphore::_fixup_after_fork ()
-{
-  if (shared == PTHREAD_PROCESS_PRIVATE)
-    {
-      debug_printf ("sem %x in _fixup_after_fork", this);
-      /* FIXME: duplicate code here and in the constructor. */
-      this->win32_obj_id = ::CreateSemaphore (&sec_none_nih, currentvalue,
-                                             LONG_MAX, NULL);
-      if (!win32_obj_id)
-       api_fatal ("failed to create new win32 semaphore, error %d");
-    }
-}
-
-verifyable_object::verifyable_object (long verifyer):
-magic (verifyer)
-{
-}
-
-verifyable_object::~verifyable_object ()
-{
-  magic = 0;
-}
-
-DWORD WINAPI
-pthread::thread_init_wrapper (void *arg)
-{
-  pthread *thread = (pthread *) arg;
-  set_tls_self_pointer (thread);
-
-  thread->mutex.lock ();
-
-  // if thread is detached force cleanup on exit
-  if (thread->attr.joinable == PTHREAD_CREATE_DETACHED && thread->joiner == NULL)
-    thread->joiner = thread;
-  thread->mutex.unlock ();
-
-  thread_printf ("started thread %p %p %p %p %p %p", arg, &_my_tls.local_clib,
-                _impure_ptr, thread, thread->function, thread->arg);
-
-  // call the user's thread
-  void *ret = thread->function (thread->arg);
-
-  thread->exit (ret);
-
-  return 0;    // just for show.  Never returns.
-}
-
-unsigned long
-pthread::getsequence_np ()
-{
-  return get_thread_id ();
-}
-
-int
-pthread::create (pthread_t *thread, const pthread_attr_t *attr,
-                 void *(*start_routine) (void *), void *arg)
-{
-  if (attr && !pthread_attr::is_good_object (attr))
-    return EINVAL;
-
-  *thread = new pthread ();
-  if (!(*thread)->create (start_routine, attr ? *attr : NULL, arg))
-    {
-      delete (*thread);
-      *thread = NULL;
-      return EAGAIN;
-    }
-
-  return 0;
-}
-
-int
-pthread::once (pthread_once_t *once_control, void (*init_routine) (void))
-{
-  // already done ?
-  if (once_control->state)
-    return 0;
-
-  pthread_mutex_lock (&once_control->mutex);
-  /* Here we must set a cancellation handler to unlock the mutex if needed */
-  /* but a cancellation handler is not the right thing. We need this in the thread
-   *cleanup routine. Assumption: a thread can only be in one pthread_once routine
-   *at a time. Stote a mutex_t *in the pthread_structure. if that's non null unlock
-   *on pthread_exit ();
-   */
-  if (!once_control->state)
-    {
-      init_routine ();
-      once_control->state = 1;
-    }
-  /* Here we must remove our cancellation handler */
-  pthread_mutex_unlock (&once_control->mutex);
-  return 0;
-}
-
-int
-pthread::cancel (pthread_t thread)
+pthread::cancel (pthread_t thread)
 {
   if (!is_good_object (&thread))
     return ESRCH;
@@ -2274,11 +2237,35 @@ pthread_attr_setscope (pthread_attr_t *attr, int contentionscope)
   return 0;
 }
 
+extern "C" int
+pthread_attr_getstack (const pthread_attr_t *attr, void **addr, size_t *size)
+{
+  if (!pthread_attr::is_good_object (attr))
+    return EINVAL;
+  /* uses lowest address of stack on all platforms */
+  *addr = (void *)((int)(*attr)->stackaddr - (*attr)->stacksize);
+  *size = (*attr)->stacksize;
+  return 0;
+}
+
+extern "C" int
+pthread_attr_getstackaddr (const pthread_attr_t *attr, void **addr)
+{
+  if (!pthread_attr::is_good_object (attr))
+    return EINVAL;
+  /* uses stack address, which is the higher address on platforms
+     where the stack grows downwards, such as x86 */
+  *addr = (*attr)->stackaddr;
+  return 0;
+}
+
 extern "C" int
 pthread_attr_setstacksize (pthread_attr_t *attr, size_t size)
 {
   if (!pthread_attr::is_good_object (attr))
     return EINVAL;
+  if (size < PTHREAD_STACK_MIN)
+    return EINVAL;    
   (*attr)->stacksize = size;
   return 0;
 }
@@ -2418,6 +2405,51 @@ pthread::resume (pthread_t *thread)
   return 0;
 }
 
+extern "C" int
+pthread_getattr_np (pthread_t thread, pthread_attr_t *attr)
+{
+  const size_t sizeof_tbi = sizeof (THREAD_BASIC_INFORMATION);
+  PTHREAD_BASIC_INFORMATION tbi;
+  NTSTATUS ret;
+
+  if (!pthread::is_good_object (&thread))
+    return ESRCH;
+
+  /* attr may not be pre-initialized */
+  if (!pthread_attr::is_good_object (attr))
+  {
+    int rv = pthread_attr_init (attr);
+    if (rv != 0)
+      return rv;
+  }
+
+  (*attr)->joinable = thread->attr.joinable;
+  (*attr)->contentionscope = thread->attr.contentionscope;
+  (*attr)->inheritsched = thread->attr.inheritsched;
+  (*attr)->schedparam = thread->attr.schedparam;
+
+  tbi = (PTHREAD_BASIC_INFORMATION) malloc (sizeof_tbi);
+  ret = NtQueryInformationThread (thread->win32_obj_id, ThreadBasicInformation,
+                                  tbi, sizeof_tbi, NULL);
+
+  if (NT_SUCCESS (ret))
+    {
+      PNT_TIB tib = tbi->TebBaseAddress;
+      (*attr)->stackaddr = tib->StackBase;
+      /* stack grows downwards on x86 systems */
+      (*attr)->stacksize = (int)tib->StackBase - (int)tib->StackLimit;
+    }
+  else
+    {
+      debug_printf ("NtQueryInformationThread(ThreadBasicInformation), "
+                    "status %p", ret);
+      (*attr)->stackaddr = thread->attr.stackaddr;
+      (*attr)->stacksize = thread->attr.stacksize;
+    }
+
+  return 0;
+}
+
 /* provided for source level compatability.
    See http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_getconcurrency.html
 */
@@ -2445,11 +2477,6 @@ pthread_getschedparam (pthread_t thread, int *policy,
 extern "C" int
 pthread_key_create (pthread_key_t *key, void (*destructor) (void *))
 {
-  /* The opengroup docs don't define if we should check this or not,
-     but creation is relatively rare.  */
-  if (pthread_key::is_good_object (key))
-    return EBUSY;
-
   *key = new pthread_key (destructor);
 
   if (!pthread_key::is_good_object (key))
@@ -2501,6 +2528,17 @@ pthread_setschedparam (pthread_t thread, int policy,
   return rv;
 }
 
+extern "C" int
+pthread_setschedprio (pthread_t thread, int priority)
+{
+  if (!pthread::is_good_object (&thread))
+    return ESRCH;
+  int rv =
+    sched_set_thread_priority (thread->win32_obj_id, priority);
+  if (!rv)
+    thread->attr.schedparam.sched_priority = priority;
+  return rv;
+}
 
 extern "C" int
 pthread_setspecific (pthread_key_t key, const void *value)
@@ -2524,7 +2562,7 @@ pthread_getspecific (pthread_key_t key)
 extern "C" int
 pthread_cond_destroy (pthread_cond_t *cond)
 {
-  if (pthread_cond::is_good_initializer (cond))
+  if (pthread_cond::is_initializer (cond))
     return 0;
   if (!pthread_cond::is_good_object (cond))
     return EINVAL;
@@ -2542,7 +2580,6 @@ pthread_cond_destroy (pthread_cond_t *cond)
 int
 pthread_cond::init (pthread_cond_t *cond, const pthread_condattr_t *attr)
 {
-
   pthread_cond_t new_cond;
 
   if (attr && !pthread_condattr::is_good_object (attr))
@@ -2550,18 +2587,20 @@ pthread_cond::init (pthread_cond_t *cond, const pthread_condattr_t *attr)
 
   cond_initialization_lock.lock ();
 
-  if (!is_good_initializer_or_bad_object (cond))
+  new_cond = new pthread_cond (attr ? (*attr) : NULL);
+  if (!is_good_object (&new_cond))
     {
+      delete new_cond;
       cond_initialization_lock.unlock ();
-      return EBUSY;
+      return EAGAIN;
     }
 
-  new_cond = new pthread_cond (attr ? (*attr) : NULL);
-  if (!is_good_object (&new_cond))
+  myfault efault;
+  if (efault.faulted ())
     {
       delete new_cond;
       cond_initialization_lock.unlock ();
-      return EAGAIN;
+      return EINVAL;
     }
 
   *cond = new_cond;
@@ -2573,7 +2612,7 @@ pthread_cond::init (pthread_cond_t *cond, const pthread_condattr_t *attr)
 extern "C" int
 pthread_cond_broadcast (pthread_cond_t *cond)
 {
-  if (pthread_cond::is_good_initializer (cond))
+  if (pthread_cond::is_initializer (cond))
     return 0;
   if (!pthread_cond::is_good_object (cond))
     return EINVAL;
@@ -2586,7 +2625,7 @@ pthread_cond_broadcast (pthread_cond_t *cond)
 extern "C" int
 pthread_cond_signal (pthread_cond_t *cond)
 {
-  if (pthread_cond::is_good_initializer (cond))
+  if (pthread_cond::is_initializer (cond))
     return 0;
   if (!pthread_cond::is_good_object (cond))
     return EINVAL;
@@ -2602,10 +2641,10 @@ __pthread_cond_dowait (pthread_cond_t *cond, pthread_mutex_t *mutex,
 {
   if (!pthread_mutex::is_good_object (mutex))
     return EINVAL;
-  if (!pthread_mutex::can_be_unlocked (mutex))
+  if (!(*mutex)->can_be_unlocked ())
     return EPERM;
 
-  if (pthread_cond::is_good_initializer (cond))
+  if (pthread_cond::is_initializer (cond))
     pthread_cond::init (cond, NULL);
   if (!pthread_cond::is_good_object (cond))
     return EINVAL;
@@ -2638,7 +2677,7 @@ pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex,
      evaluate microseconds directly in DWORD. */
   if (tv.tv_sec > abstime->tv_sec
       || (tv.tv_sec == abstime->tv_sec
-          && tv.tv_usec > abstime->tv_nsec / 1000))
+         && tv.tv_usec > abstime->tv_nsec / 1000))
     return ETIMEDOUT;
 
   waitlength = (abstime->tv_sec - tv.tv_sec) * 1000;
@@ -2706,7 +2745,7 @@ pthread_condattr_destroy (pthread_condattr_t *condattr)
 extern "C" int
 pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
 {
-  if (pthread_rwlock::is_good_initializer (rwlock))
+  if (pthread_rwlock::is_initializer (rwlock))
     return 0;
   if (!pthread_rwlock::is_good_object (rwlock))
     return EINVAL;
@@ -2731,18 +2770,20 @@ pthread_rwlock::init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr
 
   rwlock_initialization_lock.lock ();
 
-  if (!is_good_initializer_or_bad_object (rwlock))
+  new_rwlock = new pthread_rwlock (attr ? (*attr) : NULL);
+  if (!is_good_object (&new_rwlock))
     {
+      delete new_rwlock;
       rwlock_initialization_lock.unlock ();
-      return EBUSY;
+      return EAGAIN;
     }
 
-  new_rwlock = new pthread_rwlock (attr ? (*attr) : NULL);
-  if (!is_good_object (&new_rwlock))
+  myfault efault;
+  if (efault.faulted ())
     {
       delete new_rwlock;
       rwlock_initialization_lock.unlock ();
-      return EAGAIN;
+      return EINVAL;
     }
 
   *rwlock = new_rwlock;
@@ -2756,7 +2797,7 @@ pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
 {
   pthread_testcancel ();
 
-  if (pthread_rwlock::is_good_initializer (rwlock))
+  if (pthread_rwlock::is_initializer (rwlock))
     pthread_rwlock::init (rwlock, NULL);
   if (!pthread_rwlock::is_good_object (rwlock))
     return EINVAL;
@@ -2767,7 +2808,7 @@ pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
 extern "C" int
 pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
 {
-  if (pthread_rwlock::is_good_initializer (rwlock))
+  if (pthread_rwlock::is_initializer (rwlock))
     pthread_rwlock::init (rwlock, NULL);
   if (!pthread_rwlock::is_good_object (rwlock))
     return EINVAL;
@@ -2780,7 +2821,7 @@ pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
 {
   pthread_testcancel ();
 
-  if (pthread_rwlock::is_good_initializer (rwlock))
+  if (pthread_rwlock::is_initializer (rwlock))
     pthread_rwlock::init (rwlock, NULL);
   if (!pthread_rwlock::is_good_object (rwlock))
     return EINVAL;
@@ -2791,7 +2832,7 @@ pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
 extern "C" int
 pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
 {
-  if (pthread_rwlock::is_good_initializer (rwlock))
+  if (pthread_rwlock::is_initializer (rwlock))
     pthread_rwlock::init (rwlock, NULL);
   if (!pthread_rwlock::is_good_object (rwlock))
     return EINVAL;
@@ -2802,7 +2843,7 @@ pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
 extern "C" int
 pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
 {
-  if (pthread_rwlock::is_good_initializer (rwlock))
+  if (pthread_rwlock::is_initializer (rwlock))
     return 0;
   if (!pthread_rwlock::is_good_object (rwlock))
     return EINVAL;
@@ -2869,12 +2910,29 @@ pthread_kill (pthread_t thread, int sig)
   if (!pthread::is_good_object (&thread))
     return EINVAL;
 
-  siginfo_t si;
+  siginfo_t si = {0};
   si.si_signo = sig;
   si.si_code = SI_USER;
-  si.si_pid = si.si_uid = si.si_errno = 0;
-  thread->cygtls->set_threadkill ();
-  int rval = sig ? sig_send (NULL, si, thread->cygtls) : 0;
+  si.si_pid = myself->pid;
+  si.si_uid = myself->uid;
+  int rval;
+  if (!thread->valid)
+    rval = ESRCH;
+  else if (sig)
+    {
+      thread->cygtls->set_threadkill ();
+      rval = sig_send (NULL, si, thread->cygtls);
+    }
+  else
+    switch (WaitForSingleObject (thread->win32_obj_id, 0))
+      {
+      case WAIT_TIMEOUT:
+       rval = 0;
+       break;
+      default:
+       rval = ESRCH;
+       break;
+      }
 
   // unlock myself
   return rval;
@@ -2901,39 +2959,42 @@ pthread_mutex::init (pthread_mutex_t *mutex,
                     const pthread_mutexattr_t *attr,
                     const pthread_mutex_t initializer)
 {
-  pthread_mutex_t new_mutex;
-
   if (attr && !pthread_mutexattr::is_good_object (attr))
     return EINVAL;
 
   mutex_initialization_lock.lock ();
-
-  if (!is_good_initializer_or_bad_object (mutex))
+  if (initializer == NULL || pthread_mutex::is_initializer (mutex))
     {
-      mutex_initialization_lock.unlock ();
-      return EBUSY;
-    }
+      pthread_mutex_t new_mutex = new pthread_mutex (attr ? (*attr) : NULL);
+      if (!is_good_object (&new_mutex))
+       {
+         delete new_mutex;
+         mutex_initialization_lock.unlock ();
+         return EAGAIN;
+       }
 
-  new_mutex = new pthread_mutex (attr ? (*attr) : NULL);
-  if (!is_good_object (&new_mutex))
-    {
-      delete new_mutex;
-      mutex_initialization_lock.unlock ();
-      return EAGAIN;
-    }
+      if (!attr && initializer)
+       {
+         if (initializer == PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
+           new_mutex->type = PTHREAD_MUTEX_RECURSIVE;
+         else if (initializer == PTHREAD_NORMAL_MUTEX_INITIALIZER_NP)
+           new_mutex->type = PTHREAD_MUTEX_NORMAL;
+         else if (initializer == PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP)
+           new_mutex->type = PTHREAD_MUTEX_ERRORCHECK;
+       }
 
-  if (!attr && initializer)
-    {
-      if (initializer == PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
-       new_mutex->type = PTHREAD_MUTEX_RECURSIVE;
-      else if (initializer == PTHREAD_NORMAL_MUTEX_INITIALIZER_NP)
-       new_mutex->type = PTHREAD_MUTEX_NORMAL;
-      else if (initializer == PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP)
-       new_mutex->type = PTHREAD_MUTEX_ERRORCHECK;
-    }
+      myfault efault;
+      if (efault.faulted ())
+       {
+         delete new_mutex;
+         mutex_initialization_lock.unlock ();
+         return EINVAL;
+       }
 
-  *mutex = new_mutex;
+      *mutex = new_mutex;
+    }
   mutex_initialization_lock.unlock ();
+  pthread_printf ("*mutex %p, attr %p, initializer %p", *mutex, attr, initializer);
 
   return 0;
 }
@@ -2955,7 +3016,7 @@ pthread_mutex_getprioceiling (const pthread_mutex_t *mutex,
 extern "C" int
 pthread_mutex_lock (pthread_mutex_t *mutex)
 {
-  if (pthread_mutex::is_good_initializer (mutex))
+  if (pthread_mutex::is_initializer (mutex))
     pthread_mutex::init (mutex, NULL, *mutex);
   if (!pthread_mutex::is_good_object (mutex))
     return EINVAL;
@@ -2965,7 +3026,7 @@ pthread_mutex_lock (pthread_mutex_t *mutex)
 extern "C" int
 pthread_mutex_trylock (pthread_mutex_t *mutex)
 {
-  if (pthread_mutex::is_good_initializer (mutex))
+  if (pthread_mutex::is_initializer (mutex))
     pthread_mutex::init (mutex, NULL, *mutex);
   if (!pthread_mutex::is_good_object (mutex))
     return EINVAL;
@@ -2975,7 +3036,7 @@ pthread_mutex_trylock (pthread_mutex_t *mutex)
 extern "C" int
 pthread_mutex_unlock (pthread_mutex_t *mutex)
 {
-  if (pthread_mutex::is_good_initializer (mutex))
+  if (pthread_mutex::is_initializer (mutex))
     return EPERM;
   if (!pthread_mutex::is_good_object (mutex))
     return EINVAL;
@@ -2987,7 +3048,7 @@ pthread_mutex_destroy (pthread_mutex_t *mutex)
 {
   int rv;
 
-  if (pthread_mutex::is_good_initializer (mutex))
+  if (pthread_mutex::is_initializer (mutex))
     return 0;
   if (!pthread_mutex::is_good_object (mutex))
     return EINVAL;
@@ -3007,6 +3068,63 @@ pthread_mutex_setprioceiling (pthread_mutex_t *mutex, int prioceiling,
   return ENOSYS;
 }
 
+/* Spinlocks  */
+
+int
+pthread_spinlock::init (pthread_spinlock_t *spinlock, int pshared)
+{
+  pthread_spinlock_t new_spinlock = new pthread_spinlock (pshared);
+  if (!is_good_object (&new_spinlock))
+    {
+      delete new_spinlock;
+      return EAGAIN;
+    }
+
+  myfault efault;
+  if (efault.faulted ())
+    {
+      delete new_spinlock;
+      return EINVAL;
+    }
+
+  *spinlock = new_spinlock;
+  pthread_printf ("*spinlock %p, pshared %d", *spinlock, pshared);
+
+  return 0;
+}
+
+extern "C" int
+pthread_spin_lock (pthread_spinlock_t *spinlock)
+{
+  if (!pthread_spinlock::is_good_object (spinlock))
+    return EINVAL;
+  return (*spinlock)->lock ();
+}
+
+extern "C" int
+pthread_spin_trylock (pthread_spinlock_t *spinlock)
+{
+  if (!pthread_spinlock::is_good_object (spinlock))
+    return EINVAL;
+  return (*spinlock)->trylock ();
+}
+
+extern "C" int
+pthread_spin_unlock (pthread_spinlock_t *spinlock)
+{
+  if (!pthread_spinlock::is_good_object (spinlock))
+    return EINVAL;
+  return (*spinlock)->unlock ();
+}
+
+extern "C" int
+pthread_spin_destroy (pthread_spinlock_t *spinlock)
+{
+  if (!pthread_spinlock::is_good_object (spinlock))
+    return EINVAL;
+  return (*spinlock)->destroy ();
+}
+
 /* Win32 doesn't support mutex priorities - see __pthread_mutex_getprioceiling
    for more detail */
 extern "C" int
@@ -3130,17 +3248,204 @@ pthread_mutexattr_settype (pthread_mutexattr_t *attr, int type)
 
 /* Semaphores */
 
+List<semaphore> semaphore::semaphores;
+
+semaphore::semaphore (int pshared, unsigned int value)
+: verifyable_object (SEM_MAGIC),
+  shared (pshared),
+  currentvalue (value),
+  fd (-1),
+  hash (0ULL),
+  sem (NULL)
+{
+  SECURITY_ATTRIBUTES sa = (pshared != PTHREAD_PROCESS_PRIVATE)
+                          ? sec_all : sec_none_nih;
+  this->win32_obj_id = ::CreateSemaphore (&sa, value, LONG_MAX, NULL);
+  if (!this->win32_obj_id)
+    magic = 0;
+
+  semaphores.insert (this);
+}
+
+semaphore::semaphore (unsigned long long shash, LUID sluid, int sfd,
+                     sem_t *ssem, int oflag, mode_t mode, unsigned int value)
+: verifyable_object (SEM_MAGIC),
+  shared (PTHREAD_PROCESS_SHARED),
+  currentvalue (value),                /* Unused for named semaphores. */
+  fd (sfd),
+  hash (shash),
+  luid (sluid),
+  sem (ssem)
+{
+  char name[MAX_PATH];
+
+  __small_sprintf (name, "semaphore/%016X%08x%08x",
+                  hash, luid.HighPart, luid.LowPart);
+  this->win32_obj_id = ::CreateSemaphore (&sec_all, value, LONG_MAX, name);
+  if (!this->win32_obj_id)
+    magic = 0;
+  if (GetLastError () == ERROR_ALREADY_EXISTS && (oflag & O_EXCL))
+    {
+      __seterrno ();
+      CloseHandle (this->win32_obj_id);
+      magic = 0;
+    }
+
+  semaphores.insert (this);
+}
+
+semaphore::~semaphore ()
+{
+  if (win32_obj_id)
+    CloseHandle (win32_obj_id);
+
+  semaphores.remove (this);
+}
+
+void
+semaphore::_post ()
+{
+  if (ReleaseSemaphore (win32_obj_id, 1, &currentvalue))
+    currentvalue++;
+}
+
+int
+semaphore::_getvalue (int *sval)
+{
+  long val;
+
+  switch (WaitForSingleObject (win32_obj_id, 0))
+    {
+      case WAIT_OBJECT_0:
+       ReleaseSemaphore (win32_obj_id, 1, &val);
+       *sval = val + 1;
+       break;
+      case WAIT_TIMEOUT:
+       *sval = 0;
+       break;
+      default:
+       set_errno (EAGAIN);
+       return -1;
+    }
+  return 0;
+}
+
+int
+semaphore::_trywait ()
+{
+  /* FIXME: signals should be able to interrupt semaphores...
+    We probably need WaitForMultipleObjects here.  */
+  if (WaitForSingleObject (win32_obj_id, 0) == WAIT_TIMEOUT)
+    {
+      set_errno (EAGAIN);
+      return -1;
+    }
+  currentvalue--;
+  return 0;
+}
+
+int
+semaphore::_timedwait (const struct timespec *abstime)
+{
+  struct timeval tv;
+  long waitlength;
+
+  myfault efault;
+  if (efault.faulted ())
+    {
+      /* According to SUSv3, abstime need not be checked for validity,
+        if the semaphore can be locked immediately. */
+      if (!_trywait ())
+       return 0;
+      set_errno (EINVAL);
+      return -1;
+    }
+
+  gettimeofday (&tv, NULL);
+  waitlength = abstime->tv_sec * 1000 + abstime->tv_nsec / (1000 * 1000);
+  waitlength -= tv.tv_sec * 1000 + tv.tv_usec / 1000;
+  if (waitlength < 0)
+    waitlength = 0;
+  switch (cancelable_wait (win32_obj_id, waitlength, cw_cancel_self, cw_sig_eintr))
+    {
+    case WAIT_OBJECT_0:
+      currentvalue--;
+      break;
+    case WAIT_SIGNALED:
+      set_errno (EINTR);
+      return -1;
+    case WAIT_TIMEOUT:
+      set_errno (ETIMEDOUT);
+      return -1;
+    default:
+      pthread_printf ("cancelable_wait failed. %E");
+      __seterrno ();
+      return -1;
+    }
+  return 0;
+}
+
+int
+semaphore::_wait ()
+{
+  switch (cancelable_wait (win32_obj_id, INFINITE, cw_cancel_self, cw_sig_eintr))
+    {
+    case WAIT_OBJECT_0:
+      currentvalue--;
+      break;
+    case WAIT_SIGNALED:
+      set_errno (EINTR);
+      return -1;
+    default:
+      pthread_printf ("cancelable_wait failed. %E");
+      break;
+    }
+  return 0;
+}
+
+void
+semaphore::_fixup_after_fork ()
+{
+  if (shared == PTHREAD_PROCESS_PRIVATE)
+    {
+      pthread_printf ("sem %x", this);
+      /* FIXME: duplicate code here and in the constructor. */
+      this->win32_obj_id = ::CreateSemaphore (&sec_none_nih, currentvalue,
+                                             LONG_MAX, NULL);
+      if (!win32_obj_id)
+       api_fatal ("failed to create new win32 semaphore, error %d");
+    }
+}
+
+void
+semaphore::_terminate ()
+{
+  int _sem_close (sem_t *, bool);
+
+  if (sem)
+    _sem_close (sem, false);
+}
+
 /* static members */
 
 int
 semaphore::init (sem_t *sem, int pshared, unsigned int value)
 {
-  /* opengroup calls this undefined */
+  /*
+     We can't tell the difference between reinitialising an
+     existing semaphore and initialising a semaphore who's
+     contents happen to be a valid pointer
+   */
   if (is_good_object (sem))
-    return EBUSY;
+    {
+      paranoid_printf ("potential attempt to reinitialise a semaphore");
+    }
 
   if (value > SEM_VALUE_MAX)
-    return EINVAL;
+    {
+      set_errno(EINVAL);
+      return -1;
+    }
 
   *sem = new semaphore (pshared, value);
 
@@ -3148,7 +3453,8 @@ semaphore::init (sem_t *sem, int pshared, unsigned int value)
     {
       delete (*sem);
       *sem = NULL;
-      return EAGAIN;
+      set_errno(EAGAIN);
+      return -1;
     }
   return 0;
 }
@@ -3157,7 +3463,17 @@ int
 semaphore::destroy (sem_t *sem)
 {
   if (!is_good_object (sem))
-    return EINVAL;
+    {
+      set_errno(EINVAL);
+      return -1;
+    }
+
+  /* It's invalid to destroy a semaphore not opened with sem_init. */
+  if ((*sem)->fd != -1)
+    {
+      set_errno(EINVAL);
+      return -1;
+    }
 
   /* FIXME - new feature - test for busy against threads... */
 
@@ -3166,8 +3482,30 @@ semaphore::destroy (sem_t *sem)
   return 0;
 }
 
+int
+semaphore::close (sem_t *sem)
+{
+  if (!is_good_object (sem))
+    {
+      set_errno(EINVAL);
+      return -1;
+    }
+
+  /* It's invalid to close a semaphore not opened with sem_open. */
+  if ((*sem)->fd == -1)
+    {
+      set_errno(EINVAL);
+      return -1;
+    }
+
+  delete (*sem);
+  delete sem;
+  return 0;
+}
+
 sem_t *
-semaphore::open (const char *name, int oflag, mode_t mode, unsigned int value)
+semaphore::open (unsigned long long hash, LUID luid, int fd, int oflag,
+                mode_t mode, unsigned int value, bool &wasopen)
 {
   if (value > SEM_VALUE_MAX)
     {
@@ -3175,6 +3513,22 @@ semaphore::open (const char *name, int oflag, mode_t mode, unsigned int value)
       return NULL;
     }
 
+  /* sem_open is supposed to return the same pointer, if the same named
+     semaphore is opened multiple times in the same process, as long as
+     the semaphore hasn't been closed or unlinked in the meantime. */
+  semaphores.mx.lock ();
+  for (semaphore *sema = semaphores.head; sema; sema = sema->next)
+    if (sema->fd >= 0 && sema->hash == hash
+       && sema->luid.HighPart == luid.HighPart
+       && sema->luid.LowPart == sema->luid.LowPart)
+      {
+       wasopen = true;
+       semaphores.mx.unlock ();
+       return sema->sem;
+      }
+  semaphores.mx.unlock ();
+
+  wasopen = false;
   sem_t *sem = new sem_t;
   if (!sem)
     {
@@ -3182,7 +3536,7 @@ semaphore::open (const char *name, int oflag, mode_t mode, unsigned int value)
       return NULL;
     }
 
-  *sem = new semaphore (name, oflag, mode, value);
+  *sem = new semaphore (hash, luid, fd, sem, oflag, mode, value);
 
   if (!is_good_object (sem))
     {
@@ -3257,6 +3611,28 @@ semaphore::getvalue (sem_t *sem, int *sval)
   return (*sem)->_getvalue (sval);
 }
 
+int
+semaphore::getinternal (sem_t *sem, int *sfd, unsigned long long *shash,
+                       LUID *sluid, unsigned int *sval)
+{
+  myfault efault;
+  if (efault.faulted () || !is_good_object (sem))
+    {
+      set_errno (EINVAL);
+      return -1;
+    }
+  if ((*sfd = (*sem)->fd) < 0)
+    {
+      set_errno (EINVAL);
+      return -1;
+    }
+  *shash = (*sem)->hash;
+  *sluid = (*sem)->luid;
+  /* POSIX defines the value in calls to sem_init/sem_open as unsigned, but
+     the sem_getvalue gets a pointer to int to return the value.  Go figure! */
+  return (*sem)->_getvalue ((int *)sval);
+}
+
 /* pthread_null */
 pthread *
 pthread_null::get_null_pthread ()
This page took 0.076735 seconds and 5 git commands to generate.