forked from GerHobbelt/pthread-win32
-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.CV
3036 lines (2408 loc) · 84.8 KB
/
README.CV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
README.CV -- Condition Variables
--------------------------------
The original implementation of condition variables in
pthreads-win32 was based on a discussion paper:
"Strategies for Implementing POSIX Condition Variables
on Win32": http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
The changes suggested below were made on Feb 6 2001. This
file is included in the package for the benefit of anyone
interested in understanding the pthreads-win32 implementation
of condition variables and the (sometimes subtle) issues that
it attempts to resolve.
Thanks go to the individuals whose names appear throughout
the following text.
Ross Johnson
--------------------------------------------------------------------
fyi.. (more detailed problem description/demos + possible fix/patch)
regards,
alexander.
Alexander Terekhov
31.01.2001 17:43
cc:
From: Alexander Terekhov/Germany/IBM@IBMDE
Subject: Implementation of POSIX CVs: spur.wakeups/lost
signals/deadlocks/unfairness
ACE VERSION:
5.1.12 (pthread-win32 snapshot 2000-12-29)
HOST MACHINE and OPERATING SYSTEM:
IBM IntelliStation Z Pro, 2 x XEON 1GHz, Win2K
TARGET MACHINE and OPERATING SYSTEM, if different from HOST:
COMPILER NAME AND VERSION (AND PATCHLEVEL):
Microsoft Visual C++ 6.0
AREA/CLASS/EXAMPLE AFFECTED:
Implementation of POSIX condition variables - OS.cpp/.h
DOES THE PROBLEM AFFECT:
EXECUTION? YES!
SYNOPSIS:
a) spurious wakeups (minor problem)
b) lost signals
c) broadcast deadlock
d) unfairness (minor problem)
DESCRIPTION:
Please see attached copy of discussion thread
from comp.programming.threads for more details on
some reported problems. (i've also posted a "fyi"
message to ace-users a week or two ago but
unfortunately did not get any response so far).
It seems that current implementation suffers from
two essential problems:
1) cond.waiters_count does not accurately reflect
number of waiters blocked on semaphore - w/o
proper synchronisation that could result (in the
time window when counter is not accurate)
in spurious wakeups organised by subsequent
_signals and _broadcasts.
2) Always having (with no e.g. copy_and_clear/..)
the same queue in use (semaphore+counter)
neither signal nor broadcast provide 'atomic'
behaviour with respect to other threads/subsequent
calls to signal/broadcast/wait.
Each problem and combination of both could produce
various nasty things:
a) spurious wakeups (minor problem)
it is possible that waiter(s) which was already
unblocked even so is still counted as blocked
waiter. signal and broadcast will release
semaphore which will produce a spurious wakeup
for a 'real' waiter coming later.
b) lost signals
signalling thread ends up consuming its own
signal. please see demo/discussion below.
c) broadcast deadlock
last_waiter processing code does not correctly
handle the case with multiple threads
waiting for the end of broadcast.
please see demo/discussion below.
d) unfairness (minor problem)
without SignalObjectAndWait some waiter(s)
may end up consuming broadcasted signals
multiple times (spurious wakeups) because waiter
thread(s) can be preempted before they call
semaphore wait (but after count++ and mtx.unlock).
REPEAT BY:
See below... run problem demos programs (tennis.cpp and
tennisb.cpp) number of times concurrently (on multiprocessor)
and in multiple sessions or just add a couple of "Sleep"s
as described in the attached copy of discussion thread
from comp.programming.threads
SAMPLE FIX/WORKAROUND:
See attached patch to pthread-win32.. well, I can not
claim that it is completely bug free but at least my
test and tests provided by pthreads-win32 seem to work.
Perhaps that will help.
regards,
alexander.
>> Forum: comp.programming.threads
>> Thread: pthread_cond_* implementation questions
.
.
.
David Schwartz <[email protected]> wrote:
> [email protected] wrote:
>
>> BTW, could you please also share your view on other perceived
>> "problems" such as nested broadcast deadlock, spurious wakeups
>> and (the latest one) lost signals??
>
>I'm not sure what you mean. The standard allows an implementation
>to do almost whatever it likes. In fact, you could implement
>pthread_cond_wait by releasing the mutex, sleeping a random
>amount of time, and then reacquiring the mutex. Of course,
>this would be a pretty poor implementation, but any code that
>didn't work under that implementation wouldn't be strictly
>compliant.
The implementation you suggested is indeed correct
one (yes, now I see it :). However it requires from
signal/broadcast nothing more than to "{ return 0; }"
That is not the case for pthread-win32 and ACE
implementations. I do think that these implementations
(basically the same implementation) have some serious
problems with wait/signal/broadcast calls. I am looking
for help to clarify whether these problems are real
or not. I think that I can demonstrate what I mean
using one or two small sample programs.
.
.
.
==========
tennis.cpp
==========
#include "ace/Synch.h"
#include "ace/Thread.h"
enum GAME_STATE {
START_GAME,
PLAYER_A, // Player A playes the ball
PLAYER_B, // Player B playes the ball
GAME_OVER,
ONE_PLAYER_GONE,
BOTH_PLAYERS_GONE
};
enum GAME_STATE eGameState;
ACE_Mutex* pmtxGameStateLock;
ACE_Condition< ACE_Mutex >* pcndGameStateChange;
void*
playerA(
void* pParm
)
{
// For access to game state variable
pmtxGameStateLock->acquire();
// Play loop
while ( eGameState < GAME_OVER ) {
// Play the ball
cout << endl << "PLAYER-A" << endl;
// Now its PLAYER-B's turn
eGameState = PLAYER_B;
// Signal to PLAYER-B that now it is his turn
pcndGameStateChange->signal();
// Wait until PLAYER-B finishes playing the ball
do {
pcndGameStateChange->wait();
if ( PLAYER_B == eGameState )
cout << endl << "----PLAYER-A: SPURIOUS WAKEUP!!!" << endl;
} while ( PLAYER_B == eGameState );
}
// PLAYER-A gone
eGameState = (GAME_STATE)(eGameState+1);
cout << endl << "PLAYER-A GONE" << endl;
// No more access to state variable needed
pmtxGameStateLock->release();
// Signal PLAYER-A gone event
pcndGameStateChange->broadcast();
return 0;
}
void*
playerB(
void* pParm
)
{
// For access to game state variable
pmtxGameStateLock->acquire();
// Play loop
while ( eGameState < GAME_OVER ) {
// Play the ball
cout << endl << "PLAYER-B" << endl;
// Now its PLAYER-A's turn
eGameState = PLAYER_A;
// Signal to PLAYER-A that now it is his turn
pcndGameStateChange->signal();
// Wait until PLAYER-A finishes playing the ball
do {
pcndGameStateChange->wait();
if ( PLAYER_A == eGameState )
cout << endl << "----PLAYER-B: SPURIOUS WAKEUP!!!" << endl;
} while ( PLAYER_A == eGameState );
}
// PLAYER-B gone
eGameState = (GAME_STATE)(eGameState+1);
cout << endl << "PLAYER-B GONE" << endl;
// No more access to state variable needed
pmtxGameStateLock->release();
// Signal PLAYER-B gone event
pcndGameStateChange->broadcast();
return 0;
}
int
main (int, ACE_TCHAR *[])
{
pmtxGameStateLock = new ACE_Mutex();
pcndGameStateChange = new ACE_Condition< ACE_Mutex >( *pmtxGameStateLock
);
// Set initial state
eGameState = START_GAME;
// Create players
ACE_Thread::spawn( playerA );
ACE_Thread::spawn( playerB );
// Give them 5 sec. to play
Sleep( 5000 );//sleep( 5 );
// Set game over state
pmtxGameStateLock->acquire();
eGameState = GAME_OVER;
// Let them know
pcndGameStateChange->broadcast();
// Wait for players to stop
do {
pcndGameStateChange->wait();
} while ( eGameState < BOTH_PLAYERS_GONE );
// Cleanup
cout << endl << "GAME OVER" << endl;
pmtxGameStateLock->release();
delete pcndGameStateChange;
delete pmtxGameStateLock;
return 0;
}
===========
tennisb.cpp
===========
#include "ace/Synch.h"
#include "ace/Thread.h"
enum GAME_STATE {
START_GAME,
PLAYER_A, // Player A playes the ball
PLAYER_B, // Player B playes the ball
GAME_OVER,
ONE_PLAYER_GONE,
BOTH_PLAYERS_GONE
};
enum GAME_STATE eGameState;
ACE_Mutex* pmtxGameStateLock;
ACE_Condition< ACE_Mutex >* pcndGameStateChange;
void*
playerA(
void* pParm
)
{
// For access to game state variable
pmtxGameStateLock->acquire();
// Play loop
while ( eGameState < GAME_OVER ) {
// Play the ball
cout << endl << "PLAYER-A" << endl;
// Now its PLAYER-B's turn
eGameState = PLAYER_B;
// Signal to PLAYER-B that now it is his turn
pcndGameStateChange->broadcast();
// Wait until PLAYER-B finishes playing the ball
do {
pcndGameStateChange->wait();
if ( PLAYER_B == eGameState )
cout << endl << "----PLAYER-A: SPURIOUS WAKEUP!!!" << endl;
} while ( PLAYER_B == eGameState );
}
// PLAYER-A gone
eGameState = (GAME_STATE)(eGameState+1);
cout << endl << "PLAYER-A GONE" << endl;
// No more access to state variable needed
pmtxGameStateLock->release();
// Signal PLAYER-A gone event
pcndGameStateChange->broadcast();
return 0;
}
void*
playerB(
void* pParm
)
{
// For access to game state variable
pmtxGameStateLock->acquire();
// Play loop
while ( eGameState < GAME_OVER ) {
// Play the ball
cout << endl << "PLAYER-B" << endl;
// Now its PLAYER-A's turn
eGameState = PLAYER_A;
// Signal to PLAYER-A that now it is his turn
pcndGameStateChange->broadcast();
// Wait until PLAYER-A finishes playing the ball
do {
pcndGameStateChange->wait();
if ( PLAYER_A == eGameState )
cout << endl << "----PLAYER-B: SPURIOUS WAKEUP!!!" << endl;
} while ( PLAYER_A == eGameState );
}
// PLAYER-B gone
eGameState = (GAME_STATE)(eGameState+1);
cout << endl << "PLAYER-B GONE" << endl;
// No more access to state variable needed
pmtxGameStateLock->release();
// Signal PLAYER-B gone event
pcndGameStateChange->broadcast();
return 0;
}
int
main (int, ACE_TCHAR *[])
{
pmtxGameStateLock = new ACE_Mutex();
pcndGameStateChange = new ACE_Condition< ACE_Mutex >( *pmtxGameStateLock
);
// Set initial state
eGameState = START_GAME;
// Create players
ACE_Thread::spawn( playerA );
ACE_Thread::spawn( playerB );
// Give them 5 sec. to play
Sleep( 5000 );//sleep( 5 );
// Make some noise
pmtxGameStateLock->acquire();
cout << endl << "---Noise ON..." << endl;
pmtxGameStateLock->release();
for ( int i = 0; i < 100000; i++ )
pcndGameStateChange->broadcast();
cout << endl << "---Noise OFF" << endl;
// Set game over state
pmtxGameStateLock->acquire();
eGameState = GAME_OVER;
cout << endl << "---Stopping the game..." << endl;
// Let them know
pcndGameStateChange->broadcast();
// Wait for players to stop
do {
pcndGameStateChange->wait();
} while ( eGameState < BOTH_PLAYERS_GONE );
// Cleanup
cout << endl << "GAME OVER" << endl;
pmtxGameStateLock->release();
delete pcndGameStateChange;
delete pmtxGameStateLock;
return 0;
}
.
.
.
David Schwartz <[email protected]> wrote:
>> > It's compliant
>>
>> That is really good.
>
>> Tomorrow (I have to go urgently now) I will try to
>> demonstrate the lost-signal "problem" of current
>> pthread-win32 and ACE-(variant w/o SingleObjectAndWait)
>> implementations: players start suddenly drop their balls :-)
>> (with no change in source code).
>
>Signals aren't lost, they're going to the main thread,
>which isn't coded correctly to handle them. Try this:
>
> // Wait for players to stop
> do {
>
> pthread_cond_wait( &cndGameStateChange,&mtxGameStateLock );
>printf("Main thread stole a signal\n");
>
> } while ( eGameState < BOTH_PLAYERS_GONE );
>
>I bet everytime you thing a signal is lost, you'll see that printf.
>The signal isn't lost, it was stolen by another thread.
well, you can probably loose your bet.. it was indeed stolen
by "another" thread but not the one you seem to think of.
I think that what actually happens is the following:
H:\SA\UXX\pt\PTHREADS\TESTS>tennis3.exe
PLAYER-A
PLAYER-B
----PLAYER-B: SPURIOUS WAKEUP!!!
PLAYER-A GONE
PLAYER-B GONE
GAME OVER
H:\SA\UXX\pt\PTHREADS\TESTS>
here you can see that PLAYER-B after playing his first
ball (which came via signal from PLAYER-A) just dropped
it down. What happened is that his signal to player A
was consumed as spurious wakeup by himself (player B).
The implementation has a problem:
================
waiting threads:
================
{ /** Critical Section
inc cond.waiters_count
}
/*
/* Atomic only if using Win32 SignalObjectAndWait
/*
cond.mtx.release
/*** ^^-- A THREAD WHICH DID SIGNAL MAY ACQUIRE THE MUTEX,
/*** GO INTO WAIT ON THE SAME CONDITION AND OVERTAKE
/*** ORIGINAL WAITER(S) CONSUMING ITS OWN SIGNAL!
cond.sem.wait
Player-A after playing game's initial ball went into
wait (called _wait) but was pre-empted before reaching
wait semaphore. He was counted as waiter but was not
actually waiting/blocked yet.
===============
signal threads:
===============
{ /** Critical Section
waiters_count = cond.waiters_count
}
if ( waiters_count != 0 )
sem.post 1
endif
Player-B after he received signal/ball from Player A
called _signal. The _signal did see that there was
one waiter blocked on the condition (Player-A) and
released the semaphore.. (but it did not unblock
Player-A because he was not actually blocked).
Player-B thread continued its execution, called _wait,
was counted as second waiter BUT was allowed to slip
through opened semaphore gate (which was opened for
Player-B) and received his own signal. Player B remained
blocked followed by Player A. Deadlock happened which
lasted until main thread came in and said game over.
It seems to me that the implementation fails to
correctly implement the following statement
from specification:
http://www.opengroup.org/
onlinepubs/007908799/xsh/pthread_cond_wait.html
"These functions atomically release mutex and cause
the calling thread to block on the condition variable
cond; atomically here means "atomically with respect
to access by another thread to the mutex and then the
condition variable". That is, if another thread is
able to acquire the mutex after the about-to-block
thread has released it, then a subsequent call to
pthread_cond_signal() or pthread_cond_broadcast()
in that thread behaves as if it were issued after
the about-to-block thread has blocked."
Question: Am I right?
(I produced the program output above by simply
adding ?Sleep( 1 )?:
================
waiting threads:
================
{ /** Critical Section
inc cond.waiters_count
}
/*
/* Atomic only if using Win32 SignalObjectAndWait
/*
cond.mtx.release
Sleep( 1 ); // Win32
/*** ^^-- A THREAD WHICH DID SIGNAL MAY ACQUIRE THE MUTEX,
/*** GO INTO WAIT ON THE SAME CONDITION AND OVERTAKE
/*** ORIGINAL WAITER(S) CONSUMING ITS OWN SIGNAL!
cond.sem.wait
to the source code of pthread-win32 implementation:
http://sources.redhat.com/cgi-bin/cvsweb.cgi/pthreads/
condvar.c?rev=1.36&content-type=text/
x-cvsweb-markup&cvsroot=pthreads-win32
/*
* We keep the lock held just long enough to increment the count of
* waiters by one (above).
* Note that we can't keep it held across the
* call to sem_wait since that will deadlock other calls
* to pthread_cond_signal
*/
cleanup_args.mutexPtr = mutex;
cleanup_args.cv = cv;
cleanup_args.resultPtr = &result;
pthread_cleanup_push (ptw32_cond_wait_cleanup, (void *)
&cleanup_args);
if ((result = pthread_mutex_unlock (mutex)) == 0)
{((result
Sleep( 1 ); // @AT
/*
* Wait to be awakened by
* pthread_cond_signal, or
* pthread_cond_broadcast, or
* a timeout
*
* Note:
* ptw32_sem_timedwait is a cancellation point,
* hence providing the
* mechanism for making pthread_cond_wait a cancellation
* point. We use the cleanup mechanism to ensure we
* re-lock the mutex and decrement the waiters count
* if we are canceled.
*/
if (ptw32_sem_timedwait (&(cv->sema), abstime) == -1) {
result = PTW32_GET_ERRNO();
}
}
pthread_cleanup_pop (1); /* Always cleanup */
BTW, on my system (2 CPUs) I can manage to get
signals lost even without any source code modification
if I run the tennis program many times in different
shell sessions.
.
.
.
David Schwartz <[email protected]> wrote:
>[email protected] wrote:
>
>> well, it might be that the program is in fact buggy.
>> but you did not show me any bug.
>
>You're right. I was close but not dead on. I was correct, however,
>that the code is buggy because it uses 'pthread_cond_signal' even
>though not any thread waiting on the condition variable can do the
>job. I was wrong in which thread could be waiting on the cv but
>unable to do the job.
Okay, lets change 'pthread_cond_signal' to 'pthread_cond_broadcast'
but also add some noise from main() right before declaring the game
to be over (I need it in order to demonstrate another problem of
pthread-win32/ACE implementations - broadcast deadlock)...
.
.
.
It is my understanding of POSIX conditions,
that on correct implementation added noise
in form of unnecessary broadcasts from main,
should not break the tennis program. The
only 'side effect' of added noise on correct
implementation would be 'spurious wakeups' of
players (in fact they are not spurious,
players just see them as spurious) unblocked,
not by another player but by main before
another player had a chance to acquire the
mutex and change the game state variable:
.
.
.
PLAYER-B
PLAYER-A
---Noise ON...
PLAYER-B
PLAYER-A
.
.
.
PLAYER-B
PLAYER-A
----PLAYER-A: SPURIOUS WAKEUP!!!
PLAYER-B
PLAYER-A
---Noise OFF
PLAYER-B
---Stopping the game...
PLAYER-A GONE
PLAYER-B GONE
GAME OVER
H:\SA\UXX\pt\PTHREADS\TESTS>
On pthread-win32/ACE implementations the
program could stall:
.
.
.
PLAYER-A
PLAYER-B
PLAYER-A
PLAYER-B
PLAYER-A
PLAYER-B
PLAYER-A
PLAYER-B
---Noise ON...
PLAYER-A
---Noise OFF
^C
H:\SA\UXX\pt\PTHREADS\TESTS>
The implementation has problems:
================
waiting threads:
================
{ /** Critical Section
inc cond.waiters_count
}
/*
/* Atomic only if using Win32 SignalObjectAndWait
/*
cond.mtx.release
cond.sem.wait
/*** ^^-- WAITER CAN BE PREEMPTED AFTER BEING UNBLOCKED...
{ /** Critical Section
dec cond.waiters_count
/*** ^^- ...AND BEFORE DECREMENTING THE COUNT (1)
last_waiter = ( cond.was_broadcast &&
cond.waiters_count == 0 )
if ( last_waiter )
cond.was_broadcast = FALSE
endif
}
if ( last_waiter )
/*
/* Atomic only if using Win32 SignalObjectAndWait
/*
cond.auto_reset_event_or_sem.post /* Event for Win32
cond.mtx.acquire
/*** ^^-- ...AND BEFORE CALL TO mtx.acquire (2)
/*** ^^-- NESTED BROADCASTS RESULT IN A DEADLOCK
else
cond.mtx.acquire
/*** ^^-- ...AND BEFORE CALL TO mtx.acquire (3)
endif
==================
broadcast threads:
==================
{ /** Critical Section
waiters_count = cond.waiters_count
if ( waiters_count != 0 )
cond.was_broadcast = TRUE
endif
}
if ( waiters_count != 0 )
cond.sem.post waiters_count
/*** ^^^^^--- SPURIOUS WAKEUPS DUE TO (1)
cond.auto_reset_event_or_sem.wait /* Event for Win32
/*** ^^^^^--- DEADLOCK FOR FURTHER BROADCASTS IF THEY
HAPPEN TO GO INTO WAIT WHILE PREVIOUS
BROADCAST IS STILL IN PROGRESS/WAITING
endif
a) cond.waiters_count does not accurately reflect
number of waiters blocked on semaphore - that could
result (in the time window when counter is not accurate)
in spurios wakeups organised by subsequent _signals
and _broadcasts. From standard compliance point of view
that is OK but that could be a real problem from
performance/efficiency point of view.
b) If subsequent broadcast happen to go into wait on
cond.auto_reset_event_or_sem before previous
broadcast was unblocked from cond.auto_reset_event_or_sem
by its last waiter, one of two blocked threads will
remain blocked because last_waiter processing code
fails to unblock both threads.
In the situation with tennisb.c the Player-B was put
in a deadlock by noise (broadcast) coming from main
thread. And since Player-B holds the game state
mutex when it calls broadcast, the whole program
stalled: Player-A was deadlocked on mutex and
main thread after finishing with producing the noise
was deadlocked on mutex too (needed to declare the
game over)
(I produced the program output above by simply
adding ?Sleep( 1 )?:
==================
broadcast threads:
==================
{ /** Critical Section
waiters_count = cond.waiters_count
if ( waiters_count != 0 )
cond.was_broadcast = TRUE
endif
}
if ( waiters_count != 0 )
Sleep( 1 ); //Win32
cond.sem.post waiters_count
/*** ^^^^^--- SPURIOUS WAKEUPS DUE TO (1)
cond.auto_reset_event_or_sem.wait /* Event for Win32
/*** ^^^^^--- DEADLOCK FOR FURTHER BROADCASTS IF THEY
HAPPEN TO GO INTO WAIT WHILE PREVIOUS
BROADCAST IS STILL IN PROGRESS/WAITING
endif
to the source code of pthread-win32 implementation:
http://sources.redhat.com/cgi-bin/cvsweb.cgi/pthreads/
condvar.c?rev=1.36&content-type=text/
x-cvsweb-markup&cvsroot=pthreads-win32
if (wereWaiters)
{(wereWaiters)sroot=pthreads-win32eb.cgi/pthreads/Yem...m
/*
* Wake up all waiters
*/
Sleep( 1 ); //@AT
#ifdef NEED_SEM
result = (ptw32_increase_semaphore( &cv->sema, cv->waiters )
? 0
: EINVAL);
#else /* NEED_SEM */
result = (ReleaseSemaphore( cv->sema, cv->waiters, NULL )
? 0
: EINVAL);
#endif /* NEED_SEM */
}
(void) pthread_mutex_unlock(&(cv->waitersLock));
if (wereWaiters && result == 0)
{(wereWaiters
/*
* Wait for all the awakened threads to acquire their part of
* the counting semaphore
*/