diff options
-rw-r--r-- | Bmakefile | 9 | ||||
-rw-r--r-- | ChangeLog | 35 | ||||
-rw-r--r-- | GNUmakefile | 8 | ||||
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | pthread.h | 4 | ||||
-rw-r--r-- | pthread_cond_signal.c | 125 | ||||
-rw-r--r-- | pthread_mutex_timedlock.c | 461 | ||||
-rw-r--r-- | pthread_once.c | 166 | ||||
-rw-r--r-- | ptw32_increase_semaphore.c | 146 | ||||
-rw-r--r-- | ptw32_relmillisecs.c | 120 | ||||
-rw-r--r-- | tests/benchtest1.c | 497 | ||||
-rw-r--r-- | tests/benchtest5.c | 334 | ||||
-rw-r--r-- | tests/semaphore1.c | 316 |
13 files changed, 1116 insertions, 1113 deletions
@@ -6,9 +6,9 @@ # Currently only the recommended pthreadBC.dll is built by this makefile.
#
- +
DLL_VER = 2
- +
DEVROOT = .
DLLDEST = $(DEVROOT)\DLL
@@ -139,6 +139,7 @@ PRIVATE_SRCS = \ ptw32_tkAssocDestroy.c \
ptw32_callUserDestroyRoutines.c \
ptw32_timespec.c \
+ ptw32_relmillisecs.c \
ptw32_throw.c \
ptw32_InterlockedCompareExchange.c \
ptw32_getprocessors.c
@@ -186,9 +187,7 @@ SEMAPHORE_SRCS = \ sem_getvalue.c \
sem_open.c \
sem_close.c \
- sem_unlink.c \
- ptw32_increase_semaphore.c \
- ptw32_decrease_semaphore.c
+ sem_unlink.c
SPIN_SRCS = \
ptw32_spinlock_check_need_init.c \
@@ -1,3 +1,38 @@ +2005-04-25 Ross Johnson <ross at callisto.canberra.edu.au> + + * ptw32_relmillisecs.c: New module; converts future abstime to + milliseconds relative to 'now'. + * pthread_mutex_timedlock.c: Use new ptw32_relmillisecs routine in + place of internal code; remove the NEED_SEM code - this routine is now + implemented for builds that define NEED_SEM (WinCE etc) + * sem_timedwait.c: Likewise; after timeout or cancellation, + re-attempt to acquire the semaphore in case one has been posted since + the timeout/cancel occurred. Thanks to + * Makefile: Add ptw32_relmillisecs.c module; remove + ptw32_{in,de}crease_semaphore.c modules. + * GNUmakefile: Likewise. + * Bmakefile: Likewise. + + * sem_init.c: Re-write the NEED_SEM code to be consistent with the + non-NEED_SEM code, but retaining use of an event in place of the w32 sema + for w32 systems that don't include semaphores (WinCE); + the NEED_SEM versions of semaphores has been broken for a long time but is + now fixed and supports all of the same routines as the non-NEED_SEM case. + * sem_destroy.c: Likewise. + * sem_wait.c: Likewise. + * sem_post.c: Likewise. + * sem_post_multple.c: Likewise. + * implement.h: Likewise. + * sem_timedwait.c: Likewise; this routine is now + implemented for builds that define NEED_SEM (WinCE etc). + * sem_trywait.c: Likewise. + * sem_getvalue.c: Likewise. + + * pthread_once.c: Yet more changes, reverting closer to Gottlob Frege's + first design, but retaining cancellation, priority boosting, and adding + preservation of W32 error codes to make pthread_once transparent to + GetLastError. + 2005-04-11 Ross Johnson <ross at callisto.canberra.edu.au> * pthread_once.c (pthread_once): Added priority boosting to diff --git a/GNUmakefile b/GNUmakefile index 94f49f1..2d034c0 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -219,6 +219,7 @@ SMALL_STATIC_OBJS = \ ptw32_new.o \
ptw32_reuse.o \
ptw32_semwait.o \
+ ptw32_relmillisecs.o \
ptw32_rwlock_check_need_init.o \
sched_get_priority_max.o \
sched_get_priority_min.o \
@@ -236,8 +237,6 @@ SMALL_STATIC_OBJS = \ sem_open.o \
sem_close.o \
sem_unlink.o \
- ptw32_increase_semaphore.o \
- ptw32_decrease_semaphore.o \
signal.o \
pthread_kill.o \
ptw32_spinlock_check_need_init.o \
@@ -349,6 +348,7 @@ PRIVATE_SRCS = \ ptw32_tkAssocDestroy.c \
ptw32_callUserDestroyRoutines.c \
ptw32_semwait.c \
+ ptw32_relmillisecs.c \
ptw32_timespec.c \
ptw32_throw.c \
ptw32_InterlockedCompareExchange.c \
@@ -397,9 +397,7 @@ SEMAPHORE_SRCS = \ sem_getvalue.c \
sem_open.c \
sem_close.c \
- sem_unlink.c \
- ptw32_increase_semaphore.c \
- ptw32_decrease_semaphore.c
+ sem_unlink.c
SPIN_SRCS = \
ptw32_spinlock_check_need_init.c \
@@ -174,6 +174,7 @@ SMALL_STATIC_OBJS = \ ptw32_cond_check_need_init.obj \
ptw32_mutex_check_need_init.obj \
ptw32_semwait.obj \
+ ptw32_relmillisecs.obj \
sched_get_priority_max.obj \
sched_get_priority_min.obj \
sched_setscheduler.obj \
@@ -190,8 +191,6 @@ SMALL_STATIC_OBJS = \ sem_open.obj \
sem_close.obj \
sem_unlink.obj \
- ptw32_increase_semaphore.obj \
- ptw32_decrease_semaphore.obj \
signal.obj \
pthread_kill.obj \
ptw32_spinlock_check_need_init.obj \
@@ -262,6 +261,7 @@ MISC_SRCS = \ ptw32_calloc.c \
ptw32_new.c \
ptw32_reuse.c \
+ ptw32_relmillisecs.c \
w32_CancelableWait.c
MUTEX_SRCS = \
@@ -346,9 +346,7 @@ SEMAPHORE_SRCS = \ sem_getvalue.c \
sem_open.c \
sem_close.c \
- sem_unlink.c \
- ptw32_increase_semaphore.c \
- ptw32_decrease_semaphore.c
+ sem_unlink.c
SPIN_SRCS = \
ptw32_spinlock_check_need_init.c \
@@ -37,8 +37,8 @@ * See the README file for an explanation of the pthreads-win32 version
* numbering scheme and how the DLL is named etc.
*/
-#define PTW32_VERSION 2,3,0,0
-#define PTW32_VERSION_STRING "2, 3, 0, 0\0"
+#define PTW32_VERSION 2,4,0,0
+#define PTW32_VERSION_STRING "2, 4, 0, 0\0"
/* There are three implementations of cancel cleanup.
* Note that pthread.h is included in both application
diff --git a/pthread_cond_signal.c b/pthread_cond_signal.c index 220a5dd..2b4f6d4 100644 --- a/pthread_cond_signal.c +++ b/pthread_cond_signal.c @@ -36,130 +36,7 @@ * * ------------------------------------------------------------- * Algorithm: - * The algorithm used in this implementation is that developed by - * Alexander Terekhov in colaboration with Louis Thomas. The bulk - * of the discussion is recorded in the file README.CV, which contains - * several generations of both colaborators original algorithms. The final - * algorithm used here is the one referred to as - * - * Algorithm 8a / IMPL_SEM,UNBLOCK_STRATEGY == UNBLOCK_ALL - * - * presented below in pseudo-code as it appeared: - * - * - * given: - * semBlockLock - bin.semaphore - * semBlockQueue - semaphore - * mtxExternal - mutex or CS - * mtxUnblockLock - mutex or CS - * nWaitersGone - int - * nWaitersBlocked - int - * nWaitersToUnblock - int - * - * wait( timeout ) { - * - * [auto: register int result ] // error checking omitted - * [auto: register int nSignalsWasLeft ] - * [auto: register int nWaitersWasGone ] - * - * sem_wait( semBlockLock ); - * nWaitersBlocked++; - * sem_post( semBlockLock ); - * - * unlock( mtxExternal ); - * bTimedOut = sem_wait( semBlockQueue,timeout ); - * - * lock( mtxUnblockLock ); - * if ( 0 != (nSignalsWasLeft = nWaitersToUnblock) ) { - * if ( bTimeout ) { // timeout (or canceled) - * if ( 0 != nWaitersBlocked ) { - * nWaitersBlocked--; - * } - * else { - * nWaitersGone++; // count spurious wakeups. - * } - * } - * if ( 0 == --nWaitersToUnblock ) { - * if ( 0 != nWaitersBlocked ) { - * sem_post( semBlockLock ); // open the gate. - * nSignalsWasLeft = 0; // do not open the gate - * // below again. - * } - * else if ( 0 != (nWaitersWasGone = nWaitersGone) ) { - * nWaitersGone = 0; - * } - * } - * } - * else if ( INT_MAX/2 == ++nWaitersGone ) { // timeout/canceled or - * // spurious semaphore :-) - * sem_wait( semBlockLock ); - * nWaitersBlocked -= nWaitersGone; // something is going on here - * // - test of timeouts? :-) - * sem_post( semBlockLock ); - * nWaitersGone = 0; - * } - * unlock( mtxUnblockLock ); - * - * if ( 1 == nSignalsWasLeft ) { - * if ( 0 != nWaitersWasGone ) { - * // sem_adjust( semBlockQueue,-nWaitersWasGone ); - * while ( nWaitersWasGone-- ) { - * sem_wait( semBlockQueue ); // better now than spurious later - * } - * } sem_post( semBlockLock ); // open the gate - * } - * - * lock( mtxExternal ); - * - * return ( bTimedOut ) ? ETIMEOUT : 0; - * } - * - * signal(bAll) { - * - * [auto: register int result ] - * [auto: register int nSignalsToIssue] - * - * lock( mtxUnblockLock ); - * - * if ( 0 != nWaitersToUnblock ) { // the gate is closed!!! - * if ( 0 == nWaitersBlocked ) { // NO-OP - * return unlock( mtxUnblockLock ); - * } - * if (bAll) { - * nWaitersToUnblock += nSignalsToIssue=nWaitersBlocked; - * nWaitersBlocked = 0; - * } - * else { - * nSignalsToIssue = 1; - * nWaitersToUnblock++; - * nWaitersBlocked--; - * } - * } - * else if ( nWaitersBlocked > nWaitersGone ) { // HARMLESS RACE CONDITION! - * sem_wait( semBlockLock ); // close the gate - * if ( 0 != nWaitersGone ) { - * nWaitersBlocked -= nWaitersGone; - * nWaitersGone = 0; - * } - * if (bAll) { - * nSignalsToIssue = nWaitersToUnblock = nWaitersBlocked; - * nWaitersBlocked = 0; - * } - * else { - * nSignalsToIssue = nWaitersToUnblock = 1; - * nWaitersBlocked--; - * } - * } - * else { // NO-OP - * return unlock( mtxUnblockLock ); - * } - * - * unlock( mtxUnblockLock ); - * sem_post( semBlockQueue,nSignalsToIssue ); - * return result; - * } - * ------------------------------------------------------------- - * + * See the comments at the top of pthread_cond_wait.c. */ #include "pthread.h" diff --git a/pthread_mutex_timedlock.c b/pthread_mutex_timedlock.c index db45f45..ad373ba 100644 --- a/pthread_mutex_timedlock.c +++ b/pthread_mutex_timedlock.c @@ -1,265 +1,196 @@ -/* - * pthread_mutex_timedlock.c - * - * Description: - * This translation unit implements mutual exclusion (mutex) primitives. - * - * -------------------------------------------------------------------------- - * - * Pthreads-win32 - POSIX Threads Library for Win32 - * Copyright(C) 1998 John E. Bossom - * Copyright(C) 1999,2005 Pthreads-win32 contributors - * - * Contact Email: rpj@callisto.canberra.edu.au - * - * The current list of contributors is contained - * in the file CONTRIBUTORS included with the source - * code distribution. The list can also be seen at the - * following World Wide Web location: - * http://sources.redhat.com/pthreads-win32/contributors.html - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library in the file COPYING.LIB; - * if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef _UWIN -//# include <process.h> -#endif -#ifndef NEED_FTIME -#include <sys/timeb.h> -#endif -#include "pthread.h" -#include "implement.h" - - -static INLINE int -ptw32_timed_eventwait (HANDLE event, const struct timespec *abstime) - /* - * ------------------------------------------------------ - * DESCRIPTION - * This function waits on an event until signaled or until - * abstime passes. - * If abstime has passed when this routine is called then - * it returns a result to indicate this. - * - * If 'abstime' is a NULL pointer then this function will - * block until it can successfully decrease the value or - * until interrupted by a signal. - * - * This routine is not a cancelation point. - * - * RESULTS - * 0 successfully signaled, - * ETIMEDOUT abstime passed - * EINVAL 'event' is not a valid event, - * - * ------------------------------------------------------ - */ -{ - -#ifdef NEED_FTIME - - struct timespec currSysTime; - -#else /* NEED_FTIME */ - - struct _timeb currSysTime; - -#endif /* NEED_FTIME */ - - const DWORD NANOSEC_PER_MILLISEC = 1000000; - const DWORD MILLISEC_PER_SEC = 1000; - DWORD milliseconds; - DWORD status; - - if (event == NULL) - { - return EINVAL; - } - else - { - if (abstime == NULL) - { - milliseconds = INFINITE; - } - else - { - /* - * Calculate timeout as milliseconds from current system time. - */ - - /* get current system time */ - -#ifdef NEED_FTIME - - { - FILETIME ft; - SYSTEMTIME st; - - GetSystemTime (&st); - SystemTimeToFileTime (&st, &ft); - /* - * GetSystemTimeAsFileTime(&ft); would be faster, - * but it does not exist on WinCE - */ - - ptw32_filetime_to_timespec (&ft, &currSysTime); - } - - /* - * subtract current system time from abstime - */ - milliseconds = - (abstime->tv_sec - currSysTime.tv_sec) * MILLISEC_PER_SEC; - milliseconds += - ((abstime->tv_nsec - currSysTime.tv_nsec) + - (NANOSEC_PER_MILLISEC / 2)) / NANOSEC_PER_MILLISEC; - -#else /* NEED_FTIME */ - _ftime (&currSysTime); - - /* - * subtract current system time from abstime - */ - milliseconds = - (abstime->tv_sec - currSysTime.time) * MILLISEC_PER_SEC; - milliseconds += - ((abstime->tv_nsec + - (NANOSEC_PER_MILLISEC / 2)) / NANOSEC_PER_MILLISEC) - - currSysTime.millitm; - -#endif /* NEED_FTIME */ - - if (((int) milliseconds) < 0) - { - return ETIMEDOUT; - } - } - - status = WaitForSingleObject (event, milliseconds); - - if (status == WAIT_OBJECT_0) - { - return 0; - } - else if (status == WAIT_TIMEOUT) - { - return ETIMEDOUT; - } - else - { - return EINVAL; - } - } - - return 0; - -} /* ptw32_timed_semwait */ - - -int -pthread_mutex_timedlock (pthread_mutex_t * mutex, - const struct timespec *abstime) -{ - int result; - pthread_mutex_t mx; - -#ifdef NEED_SEM - errno = ENOTSUP; - return -1; -#endif - - /* - * Let the system deal with invalid pointers. - */ - - /* - * We do a quick check to see if we need to do more work - * to initialise a static mutex. We check - * again inside the guarded section of ptw32_mutex_check_need_init() - * to avoid race conditions. - */ - if (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER) - { - if ((result = ptw32_mutex_check_need_init (mutex)) != 0) - { - return (result); - } - } - - mx = *mutex; - - if (mx->kind == PTHREAD_MUTEX_NORMAL) - { - if ((LONG) PTW32_INTERLOCKED_EXCHANGE( - (LPLONG) &mx->lock_idx, - (LONG) 1) != 0) - { - while ((LONG) PTW32_INTERLOCKED_EXCHANGE( - (LPLONG) &mx->lock_idx, - (LONG) -1) != 0) - { - if (0 != (result = ptw32_timed_eventwait (mx->event, abstime))) - { - return result; - } - } - } - } - else - { - pthread_t self = pthread_self(); - - if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_COMPARE_EXCHANGE( - (PTW32_INTERLOCKED_LPLONG) &mx->lock_idx, - (PTW32_INTERLOCKED_LONG) 1, - (PTW32_INTERLOCKED_LONG) 0) == 0) - { - mx->recursive_count = 1; - mx->ownerThread = self; - } - else - { - if (pthread_equal (mx->ownerThread, self)) - { - if (mx->kind == PTHREAD_MUTEX_RECURSIVE) - { - mx->recursive_count++; - } - else - { - return EDEADLK; - } - } - else - { - while ((LONG) PTW32_INTERLOCKED_EXCHANGE( - (LPLONG) &mx->lock_idx, - (LONG) -1) != 0) - { - if (0 != (result = ptw32_timed_eventwait (mx->event, abstime))) - { - return result; - } - } - - mx->recursive_count = 1; - mx->ownerThread = self; - } - } - } - - return 0; -} +/*
+ * pthread_mutex_timedlock.c
+ *
+ * Description:
+ * This translation unit implements mutual exclusion (mutex) primitives.
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Pthreads-win32 - POSIX Threads Library for Win32
+ * Copyright(C) 1998 John E. Bossom
+ * Copyright(C) 1999,2005 Pthreads-win32 contributors
+ *
+ * Contact Email: rpj@callisto.canberra.edu.au
+ *
+ * The current list of contributors is contained
+ * in the file CONTRIBUTORS included with the source
+ * code distribution. The list can also be seen at the
+ * following World Wide Web location:
+ * http://sources.redhat.com/pthreads-win32/contributors.html
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include "pthread.h"
+#include "implement.h"
+
+
+static INLINE int
+ptw32_timed_eventwait (HANDLE event, const struct timespec *abstime)
+ /*
+ * ------------------------------------------------------
+ * DESCRIPTION
+ * This function waits on an event until signaled or until
+ * abstime passes.
+ * If abstime has passed when this routine is called then
+ * it returns a result to indicate this.
+ *
+ * If 'abstime' is a NULL pointer then this function will
+ * block until it can successfully decrease the value or
+ * until interrupted by a signal.
+ *
+ * This routine is not a cancelation point.
+ *
+ * RESULTS
+ * 0 successfully signaled,
+ * ETIMEDOUT abstime passed
+ * EINVAL 'event' is not a valid event,
+ *
+ * ------------------------------------------------------
+ */
+{
+
+ DWORD milliseconds;
+ DWORD status;
+
+ if (event == NULL)
+ {
+ return EINVAL;
+ }
+ else
+ {
+ if (abstime == NULL)
+ {
+ milliseconds = INFINITE;
+ }
+ else
+ {
+ /*
+ * Calculate timeout as milliseconds from current system time.
+ */
+ milliseconds = ptw32_relmillisecs (abstime);
+ }
+
+ status = WaitForSingleObject (event, milliseconds);
+
+ if (status == WAIT_OBJECT_0)
+ {
+ return 0;
+ }
+ else if (status == WAIT_TIMEOUT)
+ {
+ return ETIMEDOUT;
+ }
+ else
+ {
+ return EINVAL;
+ }
+ }
+
+ return 0;
+
+} /* ptw32_timed_semwait */
+
+
+int
+pthread_mutex_timedlock (pthread_mutex_t * mutex,
+ const struct timespec *abstime)
+{
+ int result;
+ pthread_mutex_t mx;
+
+ /*
+ * Let the system deal with invalid pointers.
+ */
+
+ /*
+ * We do a quick check to see if we need to do more work
+ * to initialise a static mutex. We check
+ * again inside the guarded section of ptw32_mutex_check_need_init()
+ * to avoid race conditions.
+ */
+ if (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
+ {
+ if ((result = ptw32_mutex_check_need_init (mutex)) != 0)
+ {
+ return (result);
+ }
+ }
+
+ mx = *mutex;
+
+ if (mx->kind == PTHREAD_MUTEX_NORMAL)
+ {
+ if ((LONG) PTW32_INTERLOCKED_EXCHANGE(
+ (LPLONG) &mx->lock_idx,
+ (LONG) 1) != 0)
+ {
+ while ((LONG) PTW32_INTERLOCKED_EXCHANGE(
+ (LPLONG) &mx->lock_idx,
+ (LONG) -1) != 0)
+ {
+ if (0 != (result = ptw32_timed_eventwait (mx->event, abstime)))
+ {
+ return result;
+ }
+ }
+ }
+ }
+ else
+ {
+ pthread_t self = pthread_self();
+
+ if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_COMPARE_EXCHANGE(
+ (PTW32_INTERLOCKED_LPLONG) &mx->lock_idx,
+ (PTW32_INTERLOCKED_LONG) 1,
+ (PTW32_INTERLOCKED_LONG) 0) == 0)
+ {
+ mx->recursive_count = 1;
+ mx->ownerThread = self;
+ }
+ else
+ {
+ if (pthread_equal (mx->ownerThread, self))
+ {
+ if (mx->kind == PTHREAD_MUTEX_RECURSIVE)
+ {
+ mx->recursive_count++;
+ }
+ else
+ {
+ return EDEADLK;
+ }
+ }
+ else
+ {
+ while ((LONG) PTW32_INTERLOCKED_EXCHANGE(
+ (LPLONG) &mx->lock_idx,
+ (LONG) -1) != 0)
+ {
+ if (0 != (result = ptw32_timed_eventwait (mx->event, abstime)))
+ {
+ return result;
+ }
+ }
+
+ mx->recursive_count = 1;
+ mx->ownerThread = self;
+ }
+ }
+ }
+
+ return 0;
+}
diff --git a/pthread_once.c b/pthread_once.c index 28d4fee..fad9dcc 100644 --- a/pthread_once.c +++ b/pthread_once.c @@ -106,7 +106,7 @@ #include "implement.h"
-static void
+static void PTW32_CDECL
ptw32_once_init_routine_cleanup(void * arg)
{
pthread_once_t * once_control = (pthread_once_t *) arg;
@@ -114,16 +114,22 @@ ptw32_once_init_routine_cleanup(void * arg) (void) PTW32_INTERLOCKED_EXCHANGE((LPLONG)&once_control->state, (LONG)PTW32_ONCE_CANCELLED);
(void) PTW32_INTERLOCKED_EXCHANGE((LPLONG)&once_control->started, (LONG)PTW32_FALSE);
- EnterCriticalSection(&ptw32_once_event_lock);
- if (once_control->event)
+// EnterCriticalSection(&ptw32_once_event_lock);
+ if (InterlockedExchangeAdd((LPLONG)&once_control->event, 0L)) /* MBR fence */
{
+ int lasterror = GetLastError ();
+ int lastWSAerror = WSAGetLastError ();
+
/*
- * There are waiters, wake some up
- * We're deliberately not using PulseEvent. It's deprecated.
+ * There are waiters, wake some up.
*/
- SetEvent(once_control->event);
+ if (!SetEvent(once_control->event))
+ {
+ SetLastError (lasterror);
+ WSASetLastError (lastWSAerror);
+ }
}
- LeaveCriticalSection(&ptw32_once_event_lock);
+// LeaveCriticalSection(&ptw32_once_event_lock);
}
@@ -167,6 +173,9 @@ pthread_once (pthread_once_t * once_control, void (*init_routine) (void)) */
{
int result;
+ int lasterror;
+ int lastWSAerror;
+ int restoreLastError;
LONG state;
pthread_t self;
HANDLE w32Thread = 0;
@@ -181,6 +190,13 @@ pthread_once (pthread_once_t * once_control, void (*init_routine) (void)) result = 0;
}
+ /*
+ * We want to be invisible to GetLastError() outside of this routine.
+ */
+ lasterror = GetLastError ();
+ lastWSAerror = WSAGetLastError ();
+ restoreLastError = PTW32_FALSE;
+
while (!((state = InterlockedExchangeAdd((LPLONG)&once_control->state, 0L)) /* Atomic Read */
& (LONG)PTW32_ONCE_DONE))
{
@@ -212,12 +228,15 @@ pthread_once (pthread_once_t * once_control, void (*init_routine) (void)) * so we will not be starved by any other threads that may now be looping
* around.
*/
- EnterCriticalSection(&ptw32_once_event_lock);
- if (once_control->event)
+// EnterCriticalSection(&ptw32_once_event_lock);
+ if (InterlockedExchangeAdd((LPLONG)&once_control->event, 0L)) /* MBR fence */
{
- ResetEvent(once_control->event);
+ if (!ResetEvent(once_control->event))
+ {
+ restoreLastError = PTW32_TRUE;
+ }
}
- LeaveCriticalSection(&ptw32_once_event_lock);
+// LeaveCriticalSection(&ptw32_once_event_lock);
/*
* Any threads entering the wait section and getting out again before
@@ -251,15 +270,23 @@ pthread_once (pthread_once_t * once_control, void (*init_routine) (void)) /*
* we didn't create the event.
- * it is only there if there is someone waiting
+ * it is only there if there is someone waiting.
+ * Avoid using the global event_lock but still prevent SetEvent
+ * from overwriting any 'lasterror' if the event is closed before we
+ * are done with it.
*/
- if (once_control->event)
+ if (InterlockedExchangeAdd((LPLONG)&once_control->event, 0L)) /* MBR fence */
{
- SetEvent(once_control->event);
+ if (!SetEvent(once_control->event))
+ {
+ restoreLastError = PTW32_TRUE;
+ }
}
}
else
{
+ HANDLE tmpEvent;
+
if (cancelled)
{
/*
@@ -275,64 +302,95 @@ pthread_once (pthread_once_t * once_control, void (*init_routine) (void)) * while waiting, create an event to wait on
*/
- EnterCriticalSection(&ptw32_once_event_lock);
- once_control->eventUsers++;
-
- /*
- * RE CANCELLATION:
- * If we are the first thread after the initter thread, and the init_routine is cancelled
- * while we're suspended at this point in the code:-
- * - state will not get set to PTW32_ONCE_DONE;
- * - cleanup will not see an event and cannot set it;
- * - therefore, we will eventually resume, create an event and wait on it;
- * cleanup will set state == CANCELLED before checking for an event, so that
- * we will see it and avoid waiting (as for state == DONE). We will go around again and
- * we may then become the initter.
- * If we are still the only other thread when we get to the end of this block, we will
- * have closed the event (good). If another thread beats us to be initter, then we will
- * re-enter here (good). In case the old event is reused, the event is always reset by
- * the new initter before clearing the CANCELLED state, causing any threads that are
- * cycling around the loop to wait again.
- * The initter thread is guaranteed to be at equal or higher priority than any waiters
- * so no waiters will starve the initter, which might otherwise cause us to loop
- * forever.
- */
-
- if (!once_control->event)
+// EnterCriticalSection(&ptw32_once_event_lock);
+ if (1 == InterlockedIncrement((LPLONG)&once_control->eventUsers))
{
- once_control->event = CreateEvent(NULL, PTW32_TRUE, PTW32_FALSE, NULL);
+ /*
+ * RE CANCELLATION:
+ * If we are the first thread after the initter thread, and the init_routine is cancelled
+ * while we're suspended at this point in the code:-
+ * - state will not get set to PTW32_ONCE_DONE;
+ * - cleanup will not see an event and cannot set it;
+ * - therefore, we will eventually resume, create an event and wait on it;
+ * cleanup will set state == CANCELLED before checking for an event, so that
+ * we will see it and avoid waiting (as for state == DONE). We will go around again and
+ * we may then become the initter.
+ * If we are still the only other thread when we get to the end of this block, we will
+ * have closed the event (good). If another thread beats us to be initter, then we will
+ * re-enter here (good). In case the old event is reused, the event is always reset by
+ * the new initter before clearing the CANCELLED state, causing any threads that are
+ * cycling around the loop to wait again.
+ * The initter thread is guaranteed to be at equal or higher priority than any waiters
+ * so no waiters will starve the initter, which might otherwise cause us to loop
+ * forever.
+ */
+ tmpEvent = CreateEvent(NULL, PTW32_TRUE, PTW32_FALSE, NULL);
+ if (PTW32_INTERLOCKED_COMPARE_EXCHANGE((PTW32_INTERLOCKED_LPLONG)&once_control->event,
+ (PTW32_INTERLOCKED_LONG)tmpEvent,
+ (PTW32_INTERLOCKED_LONG)0))
+ {
+ CloseHandle(tmpEvent);
+ }
}
- LeaveCriticalSection(&ptw32_once_event_lock);
+// LeaveCriticalSection(&ptw32_once_event_lock);
/*
* Check 'state' again in case the initting thread has finished or cancelled
* and left before seeing that there was an event to trigger.
- * (Now that the event IS created, if init gets finished AFTER this,
- * then the event handle is guaranteed to be seen and triggered).
*/
- if (!InterlockedExchangeAdd((LPLONG)&once_control->state, 0L))
+ switch (InterlockedExchangeAdd((LPLONG)&once_control->state, 0L))
{
- /* Neither DONE nor CANCELLED */
- (void) WaitForSingleObject(once_control->event, INFINITE);
+ case PTW32_ONCE_CLEAR:
+ {
+ /* Neither DONE nor CANCELLED */
+ if (WAIT_FAILED == WaitForSingleObject(once_control->event, INFINITE))
+ {
+ restoreLastError = PTW32_TRUE;
+ /*
+ * If the wait failed it's probably because the event is invalid.
+ * That's possible after a cancellation (but rare) if we got through the
+ * event create block above while a woken thread was suspended between
+ * the decrement and exchange below and then resumed before we could wait.
+ * So we'll yield.
+ */
+ Sleep(0);
+ }
+ break;
+ }
+ case PTW32_ONCE_CANCELLED:
+ {
+ if (once_control->started)
+ {
+ /* The new initter hasn't cleared the cancellation yet, so give the
+ * processor to a more productive thread. */
+ Sleep(0);
+ }
+ break;
+ }
}
/* last one out shut off the lights */
- EnterCriticalSection(&ptw32_once_event_lock);
- if (0 == --once_control->eventUsers)
+// EnterCriticalSection(&ptw32_once_event_lock);
+ if (0 == InterlockedDecrement((LPLONG)&once_control->eventUsers))
{
/* we were last */
- CloseHandle(once_control->event);
- once_control->event = 0;
+ if ((tmpEvent = (HANDLE)
+ PTW32_INTERLOCKED_EXCHANGE((LPLONG)&once_control->event,
+ (LONG)0)))
+ {
+ CloseHandle(tmpEvent);
+ }
}
- LeaveCriticalSection(&ptw32_once_event_lock);
+// LeaveCriticalSection(&ptw32_once_event_lock);
}
}
-
- /*
- * Fall through Intentionally
- */
+ if (restoreLastError)
+ {
+ SetLastError (lasterror);
+ WSASetLastError (lastWSAerror);
+ }
/*
* ------------
diff --git a/ptw32_increase_semaphore.c b/ptw32_increase_semaphore.c index 2b25eee..c1ede3f 100644 --- a/ptw32_increase_semaphore.c +++ b/ptw32_increase_semaphore.c @@ -1,73 +1,73 @@ -/* - * ------------------------------------------------------------- - * - * Module: ptw32_increase_semaphore.c - * - * Purpose: - * Semaphores aren't actually part of the PThreads standard. - * They are defined by the POSIX Standard: - * - * POSIX 1003.1b-1993 (POSIX.1b) - * - * ------------------------------------------------------------- - * - * -------------------------------------------------------------------------- - * - * Pthreads-win32 - POSIX Threads Library for Win32 - * Copyright(C) 1998 John E. Bossom - * Copyright(C) 1999,2005 Pthreads-win32 contributors - * - * Contact Email: rpj@callisto.canberra.edu.au - * - * The current list of contributors is contained - * in the file CONTRIBUTORS included with the source - * code distribution. The list can also be seen at the - * following World Wide Web location: - * http://sources.redhat.com/pthreads-win32/contributors.html - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library in the file COPYING.LIB; - * if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include "pthread.h" -#include "semaphore.h" -#include "implement.h" - -#ifdef NEED_SEM - -INLINE BOOL -ptw32_increase_semaphore (sem_t * sem, unsigned int n) -{ - BOOL result; - register sem_t s = *sem; - - EnterCriticalSection (&s->sem_lock_cs); - - if (s->value + n > s->value) - { - s->value += n; - SetEvent (s->event); - result = PTW32_TRUE; - } - else - { - result = PTW32_FALSE; - } - - LeaveCriticalSection (&s->sem_lock_cs); - return result; -} - -#endif /* NEED_SEM */ +/*
+ * -------------------------------------------------------------
+ *
+ * Module: ptw32_increase_semaphore.c
+ *
+ * Purpose:
+ * Semaphores aren't actually part of the PThreads standard.
+ * They are defined by the POSIX Standard:
+ *
+ * POSIX 1003.1b-1993 (POSIX.1b)
+ *
+ * -------------------------------------------------------------
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Pthreads-win32 - POSIX Threads Library for Win32
+ * Copyright(C) 1998 John E. Bossom
+ * Copyright(C) 1999,2005 Pthreads-win32 contributors
+ *
+ * Contact Email: rpj@callisto.canberra.edu.au
+ *
+ * The current list of contributors is contained
+ * in the file CONTRIBUTORS included with the source
+ * code distribution. The list can also be seen at the
+ * following World Wide Web location:
+ * http://sources.redhat.com/pthreads-win32/contributors.html
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include "pthread.h"
+#include "semaphore.h"
+#include "implement.h"
+
+#ifdef NEED_SEM
+
+INLINE BOOL
+ptw32_increase_semaphore (sem_t * sem, int n)
+{
+ BOOL result;
+ register sem_t s = *sem;
+
+ EnterCriticalSection (&s->sem_lock_cs);
+
+ if (s->value + n > s->value)
+ {
+ s->value += n;
+ SetEvent (s->event);
+ result = PTW32_TRUE;
+ }
+ else
+ {
+ result = PTW32_FALSE;
+ }
+
+ LeaveCriticalSection (&s->sem_lock_cs);
+ return result;
+}
+
+#endif /* NEED_SEM */
diff --git a/ptw32_relmillisecs.c b/ptw32_relmillisecs.c new file mode 100644 index 0000000..2e6bc4d --- /dev/null +++ b/ptw32_relmillisecs.c @@ -0,0 +1,120 @@ +/*
+ * ptw32_relmillisecs.c
+ *
+ * Description:
+ * This translation unit implements miscellaneous thread functions.
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Pthreads-win32 - POSIX Threads Library for Win32
+ * Copyright(C) 1998 John E. Bossom
+ * Copyright(C) 1999,2005 Pthreads-win32 contributors
+ *
+ * Contact Email: rpj@callisto.canberra.edu.au
+ *
+ * The current list of contributors is contained
+ * in the file CONTRIBUTORS included with the source
+ * code distribution. The list can also be seen at the
+ * following World Wide Web location:
+ * http://sources.redhat.com/pthreads-win32/contributors.html
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#ifndef _UWIN
+//#include <process.h>
+#endif
+#include "pthread.h"
+#include "implement.h"
+#ifndef NEED_FTIME
+#include <sys/timeb.h>
+#endif
+
+
+INLINE DWORD
+ptw32_relmillisecs (const struct timespec * abstime)
+{
+ const int64_t NANOSEC_PER_MILLISEC = 1000000;
+ const int64_t MILLISEC_PER_SEC = 1000;
+ DWORD milliseconds;
+ int64_t tmpAbsMilliseconds;
+ int64_t tmpCurrMilliseconds;
+#ifdef NEED_FTIME
+ struct timespec currSysTime;
+ FILETIME ft;
+ SYSTEMTIME st;
+#else /* ! NEED_FTIME */
+ struct _timeb currSysTime;
+#endif /* NEED_FTIME */
+
+
+ /*
+ * Calculate timeout as milliseconds from current system time.
+ */
+
+ /*
+ * subtract current system time from abstime in a way that checks
+ * that abstime is never in the past, or is never equivalent to the
+ * defined INFINITE value (0xFFFFFFFF).
+ *
+ * Assume all integers are unsigned, i.e. cannot test if less than 0.
+ */
+ tmpAbsMilliseconds = (int64_t)abstime->tv_sec * MILLISEC_PER_SEC;
+ tmpAbsMilliseconds += ((int64_t)abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC;
+
+ /* get current system time */
+
+#ifdef NEED_FTIME
+
+ GetSystemTime(&st);
+ SystemTimeToFileTime(&st, &ft);
+ /*
+ * GetSystemTimeAsFileTime(&ft); would be faster,
+ * but it does not exist on WinCE
+ */
+
+ ptw32_filetime_to_timespec(&ft, &currSysTime);
+
+ tmpCurrMilliseconds = (int64_t)currSysTime.tv_sec * MILLISEC_PER_SEC;
+ tmpCurrMilliseconds += ((int64_t)currSysTime.tv_nsec + (NANOSEC_PER_MILLISEC/2))
+ / NANOSEC_PER_MILLISEC;
+
+#else /* ! NEED_FTIME */
+
+ _ftime(&currSysTime);
+
+ tmpCurrMilliseconds = (int64_t) currSysTime.time * MILLISEC_PER_SEC;
+ tmpCurrMilliseconds += (int64_t) currSysTime.millitm;
+
+#endif /* NEED_FTIME */
+
+ if (tmpAbsMilliseconds > tmpCurrMilliseconds)
+ {
+ milliseconds = (DWORD) (tmpAbsMilliseconds - tmpCurrMilliseconds);
+ if (milliseconds == INFINITE)
+ {
+ /* Timeouts must be finite */
+ milliseconds--;
+ }
+ }
+ else
+ {
+ /* The abstime given is in the past */
+ milliseconds = 0;
+ }
+
+ return milliseconds;
+}
diff --git a/tests/benchtest1.c b/tests/benchtest1.c index c8f7f41..da0380e 100644 --- a/tests/benchtest1.c +++ b/tests/benchtest1.c @@ -1,248 +1,249 @@ -/* - * benchtest1.c - * - * - * -------------------------------------------------------------------------- - * - * Pthreads-win32 - POSIX Threads Library for Win32 - * Copyright(C) 1998 John E. Bossom - * Copyright(C) 1999,2005 Pthreads-win32 contributors - * - * Contact Email: rpj@callisto.canberra.edu.au - * - * The current list of contributors is contained - * in the file CONTRIBUTORS included with the source - * code distribution. The list can also be seen at the - * following World Wide Web location: - * http://sources.redhat.com/pthreads-win32/contributors.html - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library in the file COPYING.LIB; - * if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * -------------------------------------------------------------------------- - * - * Measure time taken to complete an elementary operation. - * - * - Mutex - * Single thread iteration over lock/unlock for each mutex type. - */ - -#include "test.h" -#include <sys/timeb.h> - -#ifdef __GNUC__ -#include <stdlib.h> -#endif - -#include "benchtest.h" - -#define PTW32_MUTEX_TYPES -#define ITERATIONS 10000000L - -pthread_mutex_t mx; -pthread_mutexattr_t ma; -struct _timeb currSysTimeStart; -struct _timeb currSysTimeStop; -long durationMilliSecs; -long overHeadMilliSecs = 0; -int one = 1; -int zero = 0; - -#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \ - - (_TStart.time*1000+_TStart.millitm)) - -/* - * Dummy use of j, otherwise the loop may be removed by the optimiser - * when doing the overhead timing with an empty loop. - */ -#define TESTSTART \ - { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++; - -#define TESTSTOP \ - }; _ftime(&currSysTimeStop); if (j + k == i) j++; } - - -void -runTest (char * testNameString, int mType) -{ -#ifdef PTW32_MUTEX_TYPES - assert(pthread_mutexattr_settype(&ma, mType) == 0); -#endif - assert(pthread_mutex_init(&mx, &ma) == 0); - - TESTSTART - assert(pthread_mutex_lock(&mx) == zero); - assert(pthread_mutex_unlock(&mx) == zero); - TESTSTOP - - assert(pthread_mutex_destroy(&mx) == 0); - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - testNameString, - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); -} - - -int -main (int argc, char *argv[]) -{ - int i = 0; - CRITICAL_SECTION cs; - old_mutex_t ox; - pthread_mutexattr_init(&ma); - - printf( "=============================================================================\n"); - printf( "\nLock plus unlock on an unlocked mutex.\n%ld iterations\n\n", - ITERATIONS); - printf( "%-45s %15s %15s\n", - "Test", - "Total(msec)", - "average(usec)"); - printf( "-----------------------------------------------------------------------------\n"); - - /* - * Time the loop overhead so we can subtract it from the actual test times. - */ - - TESTSTART - assert(1 == one); - assert(1 == one); - TESTSTOP - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - overHeadMilliSecs = durationMilliSecs; - - - TESTSTART - assert((dummy_call(&i), 1) == one); - assert((dummy_call(&i), 1) == one); - TESTSTOP - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - "Dummy call x 2", - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); - - - TESTSTART - assert((interlocked_inc_with_conditionals(&i), 1) == one); - assert((interlocked_dec_with_conditionals(&i), 1) == one); - TESTSTOP - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - "Dummy call -> Interlocked with cond x 2", - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); - - - TESTSTART - assert((InterlockedIncrement(&i), 1) == one); - assert((InterlockedDecrement(&i), 1) == one); - TESTSTOP - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - "InterlockedOp x 2", - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); - - - InitializeCriticalSection(&cs); - - TESTSTART - assert((EnterCriticalSection(&cs), 1) == one); - assert((LeaveCriticalSection(&cs), 1) == one); - TESTSTOP - - DeleteCriticalSection(&cs); - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - "Simple Critical Section", - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); - - - old_mutex_use = OLD_WIN32CS; - assert(old_mutex_init(&ox, NULL) == 0); - - TESTSTART - assert(old_mutex_lock(&ox) == zero); - assert(old_mutex_unlock(&ox) == zero); - TESTSTOP - - assert(old_mutex_destroy(&ox) == 0); - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - "Old PT Mutex using a Critical Section (WNT)", - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); - - - old_mutex_use = OLD_WIN32MUTEX; - assert(old_mutex_init(&ox, NULL) == 0); - - TESTSTART - assert(old_mutex_lock(&ox) == zero); - assert(old_mutex_unlock(&ox) == zero); - TESTSTOP - - assert(old_mutex_destroy(&ox) == 0); - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - "Old PT Mutex using a Win32 Mutex (W9x)", - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); - - printf( ".............................................................................\n"); - - /* - * Now we can start the actual tests - */ -#ifdef PTW32_MUTEX_TYPES - runTest("PTHREAD_MUTEX_DEFAULT (W9x,WNT)", PTHREAD_MUTEX_DEFAULT); - - runTest("PTHREAD_MUTEX_NORMAL (W9x,WNT)", PTHREAD_MUTEX_NORMAL); - - runTest("PTHREAD_MUTEX_ERRORCHECK (W9x,WNT)", PTHREAD_MUTEX_ERRORCHECK); - - runTest("PTHREAD_MUTEX_RECURSIVE (W9x,WNT)", PTHREAD_MUTEX_RECURSIVE); -#else - runTest("Non-blocking lock", 0); -#endif - - printf( "=============================================================================\n"); - - /* - * End of tests. - */ - - pthread_mutexattr_destroy(&ma); - - return 0; -} +/*
+ * benchtest1.c
+ *
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Pthreads-win32 - POSIX Threads Library for Win32
+ * Copyright(C) 1998 John E. Bossom
+ * Copyright(C) 1999,2005 Pthreads-win32 contributors
+ *
+ * Contact Email: rpj@callisto.canberra.edu.au
+ *
+ * The current list of contributors is contained
+ * in the file CONTRIBUTORS included with the source
+ * code distribution. The list can also be seen at the
+ * following World Wide Web location:
+ * http://sources.redhat.com/pthreads-win32/contributors.html
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Measure time taken to complete an elementary operation.
+ *
+ * - Mutex
+ * Single thread iteration over lock/unlock for each mutex type.
+ */
+
+#include "test.h"
+#include <sys/timeb.h>
+
+#ifdef __GNUC__
+#include <stdlib.h>
+#endif
+
+#include "benchtest.h"
+
+#define PTW32_MUTEX_TYPES
+#define ITERATIONS 10000000L
+
+pthread_mutex_t mx;
+pthread_mutexattr_t ma;
+struct _timeb currSysTimeStart;
+struct _timeb currSysTimeStop;
+long durationMilliSecs;
+long overHeadMilliSecs = 0;
+int one = 1;
+int zero = 0;
+
+#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \
+ - (_TStart.time*1000+_TStart.millitm))
+
+/*
+ * Dummy use of j, otherwise the loop may be removed by the optimiser
+ * when doing the overhead timing with an empty loop.
+ */
+#define TESTSTART \
+ { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
+
+#define TESTSTOP \
+ }; _ftime(&currSysTimeStop); if (j + k == i) j++; }
+
+
+void
+runTest (char * testNameString, int mType)
+{
+#ifdef PTW32_MUTEX_TYPES
+ assert(pthread_mutexattr_settype(&ma, mType) == 0);
+#endif
+ assert(pthread_mutex_init(&mx, &ma) == 0);
+
+ TESTSTART
+ assert(pthread_mutex_lock(&mx) == zero);
+ assert(pthread_mutex_unlock(&mx) == zero);
+ TESTSTOP
+
+ assert(pthread_mutex_destroy(&mx) == 0);
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+
+ printf( "%-45s %15ld %15.3f\n",
+ testNameString,
+ durationMilliSecs,
+ (float) durationMilliSecs * 1E3 / ITERATIONS);
+}
+
+
+int
+main (int argc, char *argv[])
+{
+ int i = 0;
+ CRITICAL_SECTION cs;
+ old_mutex_t ox;
+ pthread_mutexattr_init(&ma);
+
+ printf( "=============================================================================\n");
+ printf( "\nLock plus unlock on an unlocked mutex.\n%ld iterations\n\n",
+ ITERATIONS);
+ printf( "%-45s %15s %15s\n",
+ "Test",
+ "Total(msec)",
+ "average(usec)");
+ printf( "-----------------------------------------------------------------------------\n");
+
+ /*
+ * Time the loop overhead so we can subtract it from the actual test times.
+ */
+
+ TESTSTART
+ assert(1 == one);
+ assert(1 == one);
+ TESTSTOP
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+ overHeadMilliSecs = durationMilliSecs;
+
+
+ TESTSTART
+ assert((dummy_call(&i), 1) == one);
+ assert((dummy_call(&i), 1) == one);
+ TESTSTOP
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+
+ printf( "%-45s %15ld %15.3f\n",
+ "Dummy call x 2",
+ durationMilliSecs,
+ (float) durationMilliSecs * 1E3 / ITERATIONS);
+
+
+ TESTSTART
+ assert((interlocked_inc_with_conditionals(&i), 1) == one);
+ assert((interlocked_dec_with_conditionals(&i), 1) == one);
+ TESTSTOP
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+
+ printf( "%-45s %15ld %15.3f\n",
+ "Dummy call -> Interlocked with cond x 2",
+ durationMilliSecs,
+ (float) durationMilliSecs * 1E3 / ITERATIONS);
+
+
+ TESTSTART
+ assert((InterlockedIncrement((LPLONG)&i), 1) == (LONG)one);
+ assert((InterlockedDecrement((LPLONG)&i), 1) == (LONG)one);
+ TESTSTOP
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+
+ printf( "%-45s %15ld %15.3f\n",
+ "InterlockedOp x 2",
+ durationMilliSecs,
+ (float) durationMilliSecs * 1E3 / ITERATIONS);
+
+
+ InitializeCriticalSection(&cs);
+
+ TESTSTART
+ assert((EnterCriticalSection(&cs), 1) == one);
+ assert((LeaveCriticalSection(&cs), 1) == one);
+ TESTSTOP
+
+ DeleteCriticalSection(&cs);
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+
+ printf( "%-45s %15ld %15.3f\n",
+ "Simple Critical Section",
+ durationMilliSecs,
+ (float) durationMilliSecs * 1E3 / ITERATIONS);
+
+
+ old_mutex_use = OLD_WIN32CS;
+ assert(old_mutex_init(&ox, NULL) == 0);
+
+ TESTSTART
+ assert(old_mutex_lock(&ox) == zero);
+ assert(old_mutex_unlock(&ox) == zero);
+ TESTSTOP
+
+ assert(old_mutex_destroy(&ox) == 0);
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+
+ printf( "%-45s %15ld %15.3f\n",
+ "Old PT Mutex using a Critical Section (WNT)",
+ durationMilliSecs,
+ (float) durationMilliSecs * 1E3 / ITERATIONS);
+
+
+ old_mutex_use = OLD_WIN32MUTEX;
+ assert(old_mutex_init(&ox, NULL) == 0);
+
+ TESTSTART
+ assert(old_mutex_lock(&ox) == zero);
+ assert(old_mutex_unlock(&ox) == zero);
+ TESTSTOP
+
+ assert(old_mutex_destroy(&ox) == 0);
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+
+ printf( "%-45s %15ld %15.3f\n",
+ "Old PT Mutex using a Win32 Mutex (W9x)",
+ durationMilliSecs,
+ (float) durationMilliSecs * 1E3 / ITERATIONS);
+
+ printf( ".............................................................................\n");
+
+ /*
+ * Now we can start the actual tests
+ */
+#ifdef PTW32_MUTEX_TYPES
+ runTest("PTHREAD_MUTEX_DEFAULT (W9x,WNT)", PTHREAD_MUTEX_DEFAULT);
+
+ runTest("PTHREAD_MUTEX_NORMAL (W9x,WNT)", PTHREAD_MUTEX_NORMAL);
+
+ runTest("PTHREAD_MUTEX_ERRORCHECK (W9x,WNT)", PTHREAD_MUTEX_ERRORCHECK);
+
+ runTest("PTHREAD_MUTEX_RECURSIVE (W9x,WNT)", PTHREAD_MUTEX_RECURSIVE);
+#else
+ runTest("Non-blocking lock", 0);
+#endif
+
+ printf( "=============================================================================\n");
+
+ /*
+ * End of tests.
+ */
+
+ pthread_mutexattr_destroy(&ma);
+
+ one = i; /* Dummy assignment to avoid 'variable unused' warning */
+ return 0;
+}
diff --git a/tests/benchtest5.c b/tests/benchtest5.c index 0a8a0b2..2fba9e3 100644 --- a/tests/benchtest5.c +++ b/tests/benchtest5.c @@ -1,175 +1,159 @@ -/* - * benchtest5.c - * - * - * -------------------------------------------------------------------------- - * - * Pthreads-win32 - POSIX Threads Library for Win32 - * Copyright(C) 1998 John E. Bossom - * Copyright(C) 1999,2005 Pthreads-win32 contributors - * - * Contact Email: rpj@callisto.canberra.edu.au - * - * The current list of contributors is contained - * in the file CONTRIBUTORS included with the source - * code distribution. The list can also be seen at the - * following World Wide Web location: - * http://sources.redhat.com/pthreads-win32/contributors.html - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library in the file COPYING.LIB; - * if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * -------------------------------------------------------------------------- - * - * Measure time taken to complete an elementary operation. - * - * - Semaphore - * Single thread iteration over post/wait for a semaphore. - */ - -#include "test.h" -#include <sys/timeb.h> - -#ifdef __GNUC__ -#include <stdlib.h> -#endif - -#include "benchtest.h" - -#define ITERATIONS 10000000L - -sem_t sema; - -struct _timeb currSysTimeStart; -struct _timeb currSysTimeStop; -long durationMilliSecs; -long overHeadMilliSecs = 0; -int one = 1; -int zero = 0; - -#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \ - - (_TStart.time*1000+_TStart.millitm)) - -/* - * Dummy use of j, otherwise the loop may be removed by the optimiser - * when doing the overhead timing with an empty loop. - */ -#define TESTSTART \ - { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++; - -#define TESTSTOP \ - }; _ftime(&currSysTimeStop); if (j + k == i) j++; } - - -void -reportTest (char * testNameString) -{ - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - testNameString, - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); -} - - -int -main (int argc, char *argv[]) -{ - printf( "=============================================================================\n"); - printf( "\nOperations on a semaphore.\n%ld iterations\n\n", - ITERATIONS); - printf( "%-45s %15s %15s\n", - "Test", - "Total(msec)", - "average(usec)"); - printf( "-----------------------------------------------------------------------------\n"); - - /* - * Time the loop overhead so we can subtract it from the actual test times. - */ - - TESTSTART - assert(1 == one); - TESTSTOP - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - overHeadMilliSecs = durationMilliSecs; - - - /* - * Now we can start the actual tests - */ - assert(sem_init(&sema, 0, 0) == 0); - TESTSTART - assert(sem_post(&sema) == zero); - TESTSTOP - assert(sem_destroy(&sema) == 0); - - reportTest("Post"); - - - assert(sem_init(&sema, 0, ITERATIONS) == 0); - TESTSTART - assert(sem_wait(&sema) == zero); - TESTSTOP - assert(sem_destroy(&sema) == 0); - - reportTest("Wait without blocking"); - - - /* - * Time the loop overhead so we can subtract it from the actual test times. - */ - - TESTSTART - assert(1 == one); - assert(1 == one); - TESTSTOP - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - overHeadMilliSecs = durationMilliSecs; - - - /* - * Now we can start the actual tests - */ - assert(sem_init(&sema, 0, 0) == 0); - TESTSTART - assert(sem_post(&sema) == zero); - assert(sem_wait(&sema) == zero); - TESTSTOP - assert(sem_destroy(&sema) == 0); - - reportTest("Post then Wait without blocking"); - - - assert(sem_init(&sema, 0, 1) == 0); - TESTSTART - assert(sem_wait(&sema) == zero); - assert(sem_post(&sema) == zero); - TESTSTOP - assert(sem_destroy(&sema) == 0); - - reportTest("Wait then Post without blocking"); - - printf( "=============================================================================\n"); - - /* - * End of tests. - */ - - return 0; -} +/*
+ * benchtest5.c
+ *
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Pthreads-win32 - POSIX Threads Library for Win32
+ * Copyright(C) 1998 John E. Bossom
+ * Copyright(C) 1999,2005 Pthreads-win32 contributors
+ *
+ * Contact Email: rpj@callisto.canberra.edu.au
+ *
+ * The current list of contributors is contained
+ * in the file CONTRIBUTORS included with the source
+ * code distribution. The list can also be seen at the
+ * following World Wide Web location:
+ * http://sources.redhat.com/pthreads-win32/contributors.html
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Measure time taken to complete an elementary operation.
+ *
+ * - Semaphore
+ * Single thread iteration over post/wait for a semaphore.
+ */
+
+#include "test.h"
+#include <sys/timeb.h>
+
+#ifdef __GNUC__
+#include <stdlib.h>
+#endif
+
+#include "benchtest.h"
+
+#define ITERATIONS 1000000L
+
+sem_t sema;
+HANDLE w32sema;
+
+struct _timeb currSysTimeStart;
+struct _timeb currSysTimeStop;
+long durationMilliSecs;
+long overHeadMilliSecs = 0;
+int one = 1;
+int zero = 0;
+
+#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \
+ - (_TStart.time*1000+_TStart.millitm))
+
+/*
+ * Dummy use of j, otherwise the loop may be removed by the optimiser
+ * when doing the overhead timing with an empty loop.
+ */
+#define TESTSTART \
+ { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
+
+#define TESTSTOP \
+ }; _ftime(&currSysTimeStop); if (j + k == i) j++; }
+
+
+void
+reportTest (char * testNameString)
+{
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+
+ printf( "%-45s %15ld %15.3f\n",
+ testNameString,
+ durationMilliSecs,
+ (float) durationMilliSecs * 1E3 / ITERATIONS);
+}
+
+
+int
+main (int argc, char *argv[])
+{
+ printf( "=============================================================================\n");
+ printf( "\nOperations on a semaphore.\n%ld iterations\n\n",
+ ITERATIONS);
+ printf( "%-45s %15s %15s\n",
+ "Test",
+ "Total(msec)",
+ "average(usec)");
+ printf( "-----------------------------------------------------------------------------\n");
+
+ /*
+ * Time the loop overhead so we can subtract it from the actual test times.
+ */
+
+ TESTSTART
+ assert(1 == one);
+ TESTSTOP
+
+ durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
+ overHeadMilliSecs = durationMilliSecs;
+
+
+ /*
+ * Now we can start the actual tests
+ */
+ assert((w32sema = CreateSemaphore(NULL, (long) 0, (long) ITERATIONS, NULL)) != 0);
+ TESTSTART
+ assert(ReleaseSemaphore(w32sema, 1, NULL) != zero);
+ TESTSTOP
+ assert(CloseHandle(w32sema) != 0);
+
+ reportTest("W32 Post with no waiters");
+
+
+ assert((w32sema = CreateSemaphore(NULL, (long) ITERATIONS, (long) ITERATIONS, NULL)) != 0);
+ TESTSTART
+ assert(WaitForSingleObject(w32sema, INFINITE) == WAIT_OBJECT_0);
+ TESTSTOP
+ assert(CloseHandle(w32sema) != 0);
+
+ reportTest("W32 Wait without blocking");
+
+
+ assert(sem_init(&sema, 0, 0) == 0);
+ TESTSTART
+ assert(sem_post(&sema) == zero);
+ TESTSTOP
+ assert(sem_destroy(&sema) == 0);
+
+ reportTest("POSIX Post with no waiters");
+
+
+ assert(sem_init(&sema, 0, ITERATIONS) == 0);
+ TESTSTART
+ assert(sem_wait(&sema) == zero);
+ TESTSTOP
+ assert(sem_destroy(&sema) == 0);
+
+ reportTest("POSIX Wait without blocking");
+
+
+ printf( "=============================================================================\n");
+
+ /*
+ * End of tests.
+ */
+
+ return 0;
+}
diff --git a/tests/semaphore1.c b/tests/semaphore1.c index b11de0d..8ebf365 100644 --- a/tests/semaphore1.c +++ b/tests/semaphore1.c @@ -1,157 +1,159 @@ -/* - * File: semaphore1.c - * - * - * -------------------------------------------------------------------------- - * - * Pthreads-win32 - POSIX Threads Library for Win32 - * Copyright(C) 1998 John E. Bossom - * Copyright(C) 1999,2005 Pthreads-win32 contributors - * - * Contact Email: rpj@callisto.canberra.edu.au - * - * The current list of contributors is contained - * in the file CONTRIBUTORS included with the source - * code distribution. The list can also be seen at the - * following World Wide Web location: - * http://sources.redhat.com/pthreads-win32/contributors.html - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library in the file COPYING.LIB; - * if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * -------------------------------------------------------------------------- - * - * Test Synopsis: Verify trywait() returns -1 and sets EAGAIN. - * - - * - * Test Method (Validation or Falsification): - * - Validation - * - * Requirements Tested: - * - - * - * Features Tested: - * - - * - * Cases Tested: - * - - * - * Description: - * - - * - * Environment: - * - - * - * Input: - * - None. - * - * Output: - * - File name, Line number, and failed expression on failure. - * - No output on success. - * - * Assumptions: - * - - * - * Pass Criteria: - * - Process returns zero exit status. - * - * Fail Criteria: - * - Process returns non-zero exit status. - */ - -#include "test.h" - -void * -thr(void * arg) -{ - sem_t s; - int result; - - assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0); - - assert((result = sem_trywait(&s)) == -1); - - if ( result == -1 ) - { - perror("thread: sem_trywait 1: expected error"); // No error - assert(errno == EAGAIN); - } - else - { - printf("thread: ok 1\n"); - } - - assert((result = sem_post(&s)) == 0); - - assert((result = sem_trywait(&s)) == 0); - - if ( result == -1 ) - { - perror("thread: sem_trywait 2"); - } - else - { - printf("thread: ok 2\n"); - } - - assert(sem_post(&s) == 0); - - return 0; -} - - -int -main() -{ - pthread_t t; - sem_t s; - int result; - - assert(pthread_create(&t, NULL, thr, NULL) == 0); - assert(pthread_join(t, (void **)&result) == 0); - assert(result == 0); - - assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0); - - assert((result = sem_trywait(&s)) == -1); - - if ( result == -1 ) - { - perror("main: sem_trywait 1: expected error"); // No error - assert(errno == EAGAIN); - } - else - { - printf("main: ok 1\n"); - } - - assert((result = sem_post(&s)) == 0); - - assert((result = sem_trywait(&s)) == 0); - - if ( result == -1 ) - { - perror("main: sem_trywait 2"); - } - else - { - printf("main: ok 2\n"); - } - - assert(sem_post(&s) == 0); - - return 0; -} - +/*
+ * File: semaphore1.c
+ *
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Pthreads-win32 - POSIX Threads Library for Win32
+ * Copyright(C) 1998 John E. Bossom
+ * Copyright(C) 1999,2005 Pthreads-win32 contributors
+ *
+ * Contact Email: rpj@callisto.canberra.edu.au
+ *
+ * The current list of contributors is contained
+ * in the file CONTRIBUTORS included with the source
+ * code distribution. The list can also be seen at the
+ * following World Wide Web location:
+ * http://sources.redhat.com/pthreads-win32/contributors.html
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * --------------------------------------------------------------------------
+ *
+ * Test Synopsis: Verify trywait() returns -1 and sets EAGAIN.
+ * -
+ *
+ * Test Method (Validation or Falsification):
+ * - Validation
+ *
+ * Requirements Tested:
+ * -
+ *
+ * Features Tested:
+ * -
+ *
+ * Cases Tested:
+ * -
+ *
+ * Description:
+ * -
+ *
+ * Environment:
+ * -
+ *
+ * Input:
+ * - None.
+ *
+ * Output:
+ * - File name, Line number, and failed expression on failure.
+ * - No output on success.
+ *
+ * Assumptions:
+ * -
+ *
+ * Pass Criteria:
+ * - Process returns zero exit status.
+ *
+ * Fail Criteria:
+ * - Process returns non-zero exit status.
+ */
+
+#include "test.h"
+
+void *
+thr(void * arg)
+{
+ sem_t s;
+ int result;
+
+ assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0);
+
+ assert((result = sem_trywait(&s)) == -1);
+
+ if ( result == -1 )
+ {
+ int err = errno;
+ perror("thread: sem_trywait 1: expected error"); // No error
+ assert(err == EAGAIN);
+ }
+ else
+ {
+ printf("thread: ok 1\n");
+ }
+
+ assert((result = sem_post(&s)) == 0);
+
+ assert((result = sem_trywait(&s)) == 0);
+
+ if ( result == -1 )
+ {
+ perror("thread: sem_trywait 2");
+ }
+ else
+ {
+ printf("thread: ok 2\n");
+ }
+
+ assert(sem_post(&s) == 0);
+
+ return 0;
+}
+
+
+int
+main()
+{
+ pthread_t t;
+ sem_t s;
+ int result;
+
+ assert(pthread_create(&t, NULL, thr, NULL) == 0);
+ assert(pthread_join(t, (void **)&result) == 0);
+ assert(result == 0);
+
+ assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0);
+
+ assert((result = sem_trywait(&s)) == -1);
+
+ if ( result == -1 )
+ {
+ int err = errno;
+ perror("main: sem_trywait 1: expected error"); // No error
+ assert(err == EAGAIN);
+ }
+ else
+ {
+ printf("main: ok 1\n");
+ }
+
+ assert((result = sem_post(&s)) == 0);
+
+ assert((result = sem_trywait(&s)) == 0);
+
+ if ( result == -1 )
+ {
+ perror("main: sem_trywait 2");
+ }
+ else
+ {
+ printf("main: ok 2\n");
+ }
+
+ assert(sem_post(&s) == 0);
+
+ return 0;
+}
+
|