[PATCH] a new pthread_cond implementation

Thomas Pfaff tpfaff@gmx.net
Thu Jan 23 12:21:00 GMT 2003

Attached is a new pthread_cond implementation.

The current implementation uses WIN32 events to block threads. To work
around the missing SignalObjectAndWait on 9x and to protect against the
"lost wakeup" problem the code is rather complex and uses a busy loop to
wait for released threads.
(see http://www.cs.wustl.edu/~schmidt/win32-cv-1.html for a strategies
about implementing pthread cond variables on WIN32 and a description of
the lost wakeup problem).

Actually the implementation suffers from following problems:

1. pthread_cond_broadcast does not protect against the lost wakeup
2. pthread_cond_{wait|timedwait} is not a cancellation point.
3. pthread_cond_timedwait ignores the nsec part of abstime.
4. The spec requires that the mutex which is used with the condition
shall be locked by the calling thread. The current code does not check
this and will additionally create the mutex if it initialized with
PTHREAD_MUTEX_INITIALIZER. The opengroup spec suggests EPERM under that

The attached code has passed all pthreads_win32 tests so far and a stress
test of one the pthreads_win32 contributors. And i was able to build a
threaded perl 5.8.0-1 that passed all pthread related tests. The
pthread_cond test in perl is really good and it failed with the current
code erratically.

(FYI: The perl test result was:
t/op/alarm...........................Unable to create sub named "" at
op/alarm.t line 20.
# Looks like you planned 4 tests but ran 0.
FAILED at test 1
t/op/magic...........................FAILED at test 24
lib/ExtUtils/t/Embed.................FAILED at test 2
Failed 3 test scripts out of 681, 99.56% okay.
The Embed test failed with an segmentation fault.)

Attached are also two additional test cases from pthreads_win32.

As a side effect thread.cc will shrink significantly.

2003-01-23  Thomas Pfaff  <tpfaff@gmx.net>

	* thread.h (pthread_mutex::canBeUnlocked): New static method.
	(pthread_cond::ExitingWait): Remove.
	(pthread_cond::mutex): Ditto.
	(pthread_cond::cond_access): Ditto.
	(pthread_cond::win32_obj_id): Ditto.
	(pthread_cond::TimedWait): Ditto.
	(pthread_cond::BroadCast): Ditto.
	(pthread_cond::Signal): Ditto.
	(pthread_cond::pending): New member.
	(pthread_cond::semWait): Ditto.
	(pthread_cond::semIn): Ditto.
	(pthread_cond::mtxOut): Ditto.
	(pthread_cond::mtxCond): Ditto.
	(pthread_cond::UnBlock): New method.
	(pthread_cond::Wait): Ditto.
	* thread.cc: Update list of cancellation points.
	(pthread_cond::pthread_cond): Rewrite.
	(pthread_cond::~pthread_cond): Ditto.
	(pthread_cond::TimedWait): Remove.
	(pthread_cond::BroadCast): Ditto.
	(pthread_cond::Signal): Ditto.
	(pthread_cond::UnBlock): Implement.
	(pthread_cond::Wait): Ditto.
	(pthread_cond::fixup_after_fork): Rewrite.
	(pthread_mutex::canBeUnlocked): Implement.
	(pthread_mutex::fixup_after_fork): Remove DETECT_BAD_APP
	(__pthread_cond_broadcast): Just return 0 if the condition is
	ot initialized. Call pthread_cond::UnBlock to release blocked
	(__pthread_cond_signal): Ditto.
	(__pthread_cond__dowait): Rewrite.
	(pthread_cond_timedwait): Fix waitlength calculation.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: condvar9.c
URL: <http://cygwin.com/pipermail/cygwin-patches/attachments/20030123/2d9cd902/attachment.c>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: condvar7.c
URL: <http://cygwin.com/pipermail/cygwin-patches/attachments/20030123/2d9cd902/attachment-0001.c>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pthread_cond.patch
URL: <http://cygwin.com/pipermail/cygwin-patches/attachments/20030123/2d9cd902/attachment.ksh>

More information about the Cygwin-patches mailing list