summaryrefslogtreecommitdiff
path: root/ecos/packages/kernel/current/src/sync/mutex.cxx
blob: 876d12ace89eff211493512c1d1f087cc0b2be2b (plain)
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
//==========================================================================
//
//      sync/mutex.cxx
//
//      Mutex and condition variable implementation
//
//==========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
// -------------------------------------------                              
// This file is part of eCos, the Embedded Configurable Operating System.   
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under    
// the terms of the GNU General Public License as published by the Free     
// Software Foundation; either version 2 or (at your option) any later      
// version.                                                                 
//
// eCos 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 General Public License    
// for more details.                                                        
//
// You should have received a copy of the GNU General Public License        
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
//
// As a special exception, if other files instantiate templates or use      
// macros or inline functions from this file, or you compile this file      
// and link it with other works to produce a work based on this file,       
// this file does not by itself cause the resulting work to be covered by   
// the GNU General Public License. However the source code for this file    
// must still be made available in accordance with section (3) of the GNU   
// General Public License v2.                                               
//
// This exception does not invalidate any other reasons why a work based    
// on this file might be covered by the GNU General Public License.         
// -------------------------------------------                              
// ####ECOSGPLCOPYRIGHTEND####                                              
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    nickg
// Contributors: nickg, jlarmour
// Date:         1999-02-17
// Purpose:      Mutex implementation
// Description:  This file contains the implementations of the mutex
//               and condition variable classes.
//
//####DESCRIPTIONEND####
//
//==========================================================================

#include <pkgconf/kernel.h>

#include <cyg/kernel/ktypes.h>         // base kernel types
#include <cyg/infra/cyg_trac.h>        // tracing macros
#include <cyg/infra/cyg_ass.h>         // assertion macros
#include <cyg/kernel/instrmnt.h>       // instrumentation

#include <cyg/kernel/mutex.hxx>        // our header

#include <cyg/kernel/thread.inl>       // thread inlines
#include <cyg/kernel/sched.inl>        // scheduler inlines
#include <cyg/kernel/clock.inl>        // clock inlines

// -------------------------------------------------------------------------
// Mutex protocol test macros.
// If the dynamic protocol option is enabled, then these generate appropriate
// tests on the protocol field. If there is no dynamic choice then they simply
// result in empty statements.

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC

#define IF_PROTOCOL_INHERIT if( protocol == INHERIT )
#define IF_PROTOCOL_CEILING if( protocol == CEILING )
#define IF_PROTOCOL_ACTIVE  if( protocol != NONE )

#else

#define IF_PROTOCOL_INHERIT
#define IF_PROTOCOL_CEILING
#define IF_PROTOCOL_ACTIVE

#endif

// -------------------------------------------------------------------------
// Constructor

Cyg_Mutex::Cyg_Mutex()
{
    CYG_REPORT_FUNCTION();
        
    locked      = false;
    owner       = NULL;

#if defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT) && \
    defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC)

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_INHERIT
    protocol    = INHERIT;
#endif    
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_CEILING
    protocol    = CEILING;
    ceiling     = CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY;
#endif    
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_NONE
    protocol    = NONE;
#endif

#else // not (DYNAMIC and DEFAULT defined)

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING    
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY

    // if there is a default priority ceiling defined, use that to initialize
    // the ceiling.
    ceiling = CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY;    

#else

    // Otherwise set it to zero.
    ceiling = 0;
    
#endif    
#endif

#endif // DYNAMIC and DEFAULT defined
    
    CYG_REPORT_RETURN();
}

// -------------------------------------------------------------------------
// Construct with defined protocol

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC

Cyg_Mutex::Cyg_Mutex( cyg_protcol protocol_arg )
{
    CYG_REPORT_FUNCTION();
        
    locked      = false;
    owner       = NULL;

    protocol    = protocol_arg;

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING    
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY

    // if there is a default priority ceiling defined, use that to initialize
    // the ceiling.
    ceiling = CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY;    

#else

    // Otherwise set it to zero.
    ceiling = 0;
    
#endif    
#endif
    
    CYG_REPORT_RETURN();
}

#endif

// -------------------------------------------------------------------------
// Destructor

Cyg_Mutex::~Cyg_Mutex()
{
    CYG_REPORT_FUNCTION();
        
    CYG_ASSERT( owner == NULL, "Deleting mutex with owner");
    CYG_ASSERT( queue.empty(), "Deleting mutex with waiting threads");
    CYG_REPORT_RETURN();
}

// -------------------------------------------------------------------------

#ifdef CYGDBG_USE_ASSERTS

cyg_bool
Cyg_Mutex::check_this( cyg_assert_class_zeal zeal) const
{
//    CYG_REPORT_FUNCTION();
        
    // check that we have a non-NULL pointer first
    if( this == NULL ) return false;
    
    switch( zeal )
    {
    case cyg_system_test:
    case cyg_extreme:
    case cyg_thorough:
    case cyg_quick:
    case cyg_trivial:
        if(  locked && owner == NULL ) return false;
        if( !locked && owner != NULL ) return false;        
    case cyg_none:
    default:
        break;
    };

    return true;
}

#endif

// -------------------------------------------------------------------------
// Lock and/or wait

cyg_bool
Cyg_Mutex::lock(void)
{
    CYG_REPORT_FUNCTYPE("returning %d");

    cyg_bool result = true;
    Cyg_Thread *self = Cyg_Thread::self();
    
    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_ASSERTCLASS( this, "Bad this pointer");
    
    CYG_INSTRUMENT_MUTEX(LOCK, this, 0);

    // Loop while the mutex is locked, sleeping each time around
    // the loop. This copes with the possibility of a higher priority
    // thread grabbing the mutex between the wakeup in unlock() and
    // this thread actually starting.
    
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL

    IF_PROTOCOL_ACTIVE
	self->count_mutex();

#endif

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
    
    IF_PROTOCOL_CEILING
        self->set_priority_ceiling(ceiling);

#endif        
               
    while( locked && result )
    {
        CYG_ASSERT( self != owner, "Locking mutex I already own");
        
        self->set_sleep_reason( Cyg_Thread::WAIT );
        
        self->sleep();
        
        queue.enqueue( self );

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT

        IF_PROTOCOL_INHERIT
            owner->inherit_priority(self);

#endif

        CYG_INSTRUMENT_MUTEX(WAIT, this, 0);

        // Allow other threads to run
        Cyg_Scheduler::reschedule();
        
        CYG_ASSERTCLASS( this, "Bad this pointer");

        switch( self->get_wake_reason() )
        {
        case Cyg_Thread::DESTRUCT:
        case Cyg_Thread::BREAK:
            result = false;
            break;
            
        case Cyg_Thread::EXIT:            
            self->exit();
            break;

        default:
            break;
        }

    }

    if( result )
    {
        locked      = true;
        owner       = self;

        CYG_INSTRUMENT_MUTEX(LOCKED, this, 0);
    }
    else
    {
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL

       IF_PROTOCOL_ACTIVE
           self->uncount_mutex();

#endif    
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT

        IF_PROTOCOL_INHERIT
            self->disinherit_priority();

#endif
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING

        IF_PROTOCOL_CEILING
            self->clear_priority_ceiling();

#endif
    }
    
    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();

    CYG_ASSERTCLASS( this, "Bad this pointer");    

    CYG_REPORT_RETVAL(result);

    return result;
}

// -------------------------------------------------------------------------
// Try to lock and return success

cyg_bool
Cyg_Mutex::trylock(void)
{
    CYG_REPORT_FUNCTYPE("returning %d");
        
    CYG_ASSERTCLASS( this, "Bad this pointer");
    
    cyg_bool result = true;
    
    // Prevent preemption
    Cyg_Scheduler::lock();

    // If the mutex is not locked, grab it
    // for ourself. Otherwise return failure.
    if( !locked )
    {
        Cyg_Thread *self = Cyg_Thread::self();
        
        locked  = true;
        owner   = self;

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL

       IF_PROTOCOL_ACTIVE
            self->count_mutex();

#endif
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING

        IF_PROTOCOL_CEILING
            self->set_priority_ceiling(ceiling);
        
#endif        
        
    }
    else result = false;

    CYG_INSTRUMENT_MUTEX(TRY, this, result);
    
    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();
    
    CYG_REPORT_RETVAL(result);
    return result;    
}

// -------------------------------------------------------------------------
// unlock

void
Cyg_Mutex::unlock(void)
{
    CYG_REPORT_FUNCTION();
        
    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_MUTEX(UNLOCK, this, 0);

    CYG_ASSERTCLASS( this, "Bad this pointer");
    CYG_ASSERT( locked, "Unlock mutex that is not locked");
    CYG_ASSERT( owner == Cyg_Thread::self(), "Unlock mutex I do not own");
        
    if( !queue.empty() ) {

        // The queue is non-empty, so grab the next
        // thread from it and wake it up.

        Cyg_Thread *thread = queue.dequeue();

        CYG_ASSERTCLASS( thread, "Bad thread pointer");

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT

        // Give the owner-to-be a chance to inherit from the remaining
        // queue or the relinquishing thread:

        IF_PROTOCOL_INHERIT
            thread->relay_priority(owner, &queue);

#endif

        thread->set_wake_reason( Cyg_Thread::DONE );
        
        thread->wake();

        CYG_INSTRUMENT_MUTEX(WAKE, this, thread);
        
    }

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL

    IF_PROTOCOL_ACTIVE
	owner->uncount_mutex();

#endif    
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT

    IF_PROTOCOL_INHERIT
        owner->disinherit_priority();
    
#endif
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING

    IF_PROTOCOL_CEILING
        owner->clear_priority_ceiling();
        
#endif
    
    locked      = false;
    owner       = NULL;
    
    CYG_ASSERTCLASS( this, "Bad this pointer");    
    
    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();

    CYG_REPORT_RETURN();
}

// -------------------------------------------------------------------------
// Release all waiting threads.

void Cyg_Mutex::release()
{
    CYG_REPORT_FUNCTION();

    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_MUTEX(RELEASE, this, 0);

    CYG_ASSERTCLASS( this, "Bad this pointer");
        
    while( !queue.empty() )
    {
        // The queue is non-empty, so grab each
        // thread from it and release it.

        Cyg_Thread *thread = queue.dequeue();

        CYG_ASSERTCLASS( thread, "Bad thread pointer");

        thread->release();

        CYG_INSTRUMENT_MUTEX(RELEASED, this, thread);
        
    }

    CYG_ASSERTCLASS( this, "Bad this pointer");    
    
    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();

    CYG_REPORT_RETURN();
}

// -------------------------------------------------------------------------
// Set ceiling priority for priority ceiling protocol

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING

void Cyg_Mutex::set_ceiling( cyg_priority priority )
{
    CYG_REPORT_FUNCTION();

//    CYG_ASSERT( priority >=  CYG_THREAD_MAX_PRIORITY, "Priority out of range");
//    CYG_ASSERT( priority <=  CYG_THREAD_MIN_PRIORITY, "Priority out of range");
    
    // Prevent preemption
    Cyg_Scheduler::lock();

    ceiling = priority;
    
    // Unlock the scheduler
    Cyg_Scheduler::unlock();

    CYG_REPORT_RETURN();    
}

#endif

// -------------------------------------------------------------------------
// Set priority inversion protocol

#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
void Cyg_Mutex::set_protocol( cyg_protcol new_protocol )
{
    CYG_REPORT_FUNCTION();

    // Prevent preemption
    Cyg_Scheduler::lock();
    
    protocol = new_protocol;
    
    // Unlock the scheduler
    Cyg_Scheduler::unlock();
    
    CYG_REPORT_RETURN();    
}

#endif


//==========================================================================
// Condition variables

Cyg_Condition_Variable::Cyg_Condition_Variable(
    Cyg_Mutex   &mx                // linked mutex
    )
{
    CYG_REPORT_FUNCTION();
        
    mutex       = &mx;

    CYG_ASSERTCLASS( mutex, "Invalid mutex argument");

    CYG_REPORT_RETURN();
}

Cyg_Condition_Variable::Cyg_Condition_Variable()
{
    CYG_REPORT_FUNCTION();
        
    mutex       = NULL;

    CYG_REPORT_RETURN();
}

// -------------------------------------------------------------------------
// Destructor

Cyg_Condition_Variable::~Cyg_Condition_Variable()
{
    CYG_REPORT_FUNCTION();
        
    CYG_ASSERT( queue.empty(), "Deleting condvar with waiting threads");

    CYG_REPORT_RETURN();
}

// -------------------------------------------------------------------------

#ifdef CYGDBG_USE_ASSERTS

cyg_bool
Cyg_Condition_Variable::check_this( cyg_assert_class_zeal zeal) const
{
    bool result = true;

    CYG_REPORT_FUNCTYPE("returning %d");
    CYG_REPORT_FUNCARG1("zeal = %d", zeal);
        
    // check that we have a non-NULL pointer first
    if( this == NULL )
        result = false;
    else {
        
        switch( zeal )
        {
        case cyg_system_test:
        case cyg_extreme:
        case cyg_thorough:
            if( mutex != NULL && !mutex->check_this(zeal) )
                result = false;
        case cyg_quick:
        case cyg_trivial:
        case cyg_none:
        default:
            break;
        }
    }

    CYG_REPORT_RETVAL(result);
    return result;
}

#endif

// -------------------------------------------------------------------------
// Wait for condition to be true    
// Note: if this function is entered with the scheduler locked (e.g. to
// suspend DSR processing) then there is no need to take the lock.  Also
// in this case, exit with the scheduler locked, which allows this function
// to be used in a totally thread-safe manner.

cyg_bool
Cyg_Condition_Variable::wait_inner( Cyg_Mutex *mx )
{
    CYG_REPORT_FUNCTION();

    cyg_bool result = true;
    Cyg_Thread *self = Cyg_Thread::self();

    Cyg_Scheduler::lock();

    CYG_ASSERTCLASS( this, "Bad this pointer");
    CYG_ASSERTCLASS( mx, "Corrupt mutex");
    CYG_ASSERTCLASS( self, "Bad self thread");

    CYG_INSTRUMENT_CONDVAR(WAIT, this, 0);
    
    mx->unlock();

    self->set_sleep_reason( Cyg_Thread::WAIT );
        
    self->sleep();
        
    queue.enqueue( self );

    // Avoid calling ASRs during the following unlock.
    self->set_asr_inhibit();
    
    // Unlock the scheduler and switch threads
    Cyg_Scheduler::unlock_reschedule();

    // Allow ASRs again
    self->clear_asr_inhibit();
            
    CYG_INSTRUMENT_CONDVAR(WOKE, this, self->get_wake_reason());

    CYG_ASSERTCLASS( this, "Bad this pointer");
    CYG_ASSERTCLASS( mx, "Corrupt mutex");

    switch( self->get_wake_reason() )
    {
    case Cyg_Thread::DESTRUCT:          // which, the cv or the mutex?
    case Cyg_Thread::BREAK:
        result = false;
        break;
            
    case Cyg_Thread::EXIT:            
        self->exit();
        break;

    default:
        break;
    }

    // When we awake, we must re-acquire the mutex.  Note that while
    // it is essential to release the mutex and queue on the CV
    // atomically relative to other threads, to avoid races, it is not
    // necessary for us to re-acquire the mutex in the same atomic
    // action. Hence we can do it after unlocking the scheduler.
    // We need to loop here in case the thread is released while waiting
    // for the mutex. It is essential that we exit this function with the
    // mutex claimed.

    while ( !mx->lock() )
        continue;

    CYG_ASSERTCLASS( this, "Bad this pointer");
    CYG_ASSERTCLASS( mx, "Corrupt mutex");
    CYG_ASSERT( mx->owner == self, "Not mutex owner");

    CYG_REPORT_RETURN();

    return result;
}

// -------------------------------------------------------------------------
// Wake one thread

void
Cyg_Condition_Variable::signal(void)
{
    CYG_REPORT_FUNCTION();
        
    CYG_ASSERTCLASS( this, "Bad this pointer");

    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_CONDVAR(SIGNAL, this, 0);
    
    if( !queue.empty() )
    {
        // The queue is non-empty, so grab the next
        // thread from it and wake it up.

        Cyg_Thread *thread = queue.dequeue();

        CYG_ASSERTCLASS( thread, "Bad thread pointer");
        
        thread->set_wake_reason( Cyg_Thread::DONE );
        
        thread->wake();

        CYG_INSTRUMENT_CONDVAR(WAKE, this, thread);
        
    }
    
    CYG_ASSERTCLASS( this, "Bad this pointer");

    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();

    CYG_REPORT_RETURN();
}

// -------------------------------------------------------------------------
// Set cond true, wake all threads

void
Cyg_Condition_Variable::broadcast(void)
{
    CYG_REPORT_FUNCTION();
        
    CYG_ASSERTCLASS( this, "Bad this pointer");

    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_CONDVAR(BROADCAST, this, 0);
    
    // Grab all the threads from the queue and let them
    // go.
    
    while( !queue.empty() )
    {
        Cyg_Thread *thread = queue.dequeue();

        CYG_ASSERTCLASS( thread, "Bad thread pointer");
        
        thread->set_wake_reason( Cyg_Thread::DONE );
        
        thread->wake();

        CYG_INSTRUMENT_CONDVAR(WAKE, this, thread);        
    }
    
    CYG_ASSERTCLASS( this, "Bad this pointer");
    
    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();    

    CYG_REPORT_RETURN();
}

// -------------------------------------------------------------------------
// Optional timed wait on a CV

#if defined(CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT)

cyg_bool
Cyg_Condition_Variable::wait_inner( Cyg_Mutex *mx, cyg_tick_count timeout )
{
    CYG_REPORT_FUNCTYPE("returning %d");
    CYG_REPORT_FUNCARG1("timeout = %d", timeout);
        
    CYG_ASSERTCLASS( this, "Bad this pointer");
    CYG_ASSERTCLASS( mx, "Corrupt mutex");

    cyg_bool result = true;
    
    Cyg_Thread *self = Cyg_Thread::self();

    CYG_ASSERTCLASS( self, "Bad self thread");
    
    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_CONDVAR(TIMED_WAIT, this, 0 );
    
    mx->unlock();

    // The ordering of sleep() and set_timer() here are
    // important. If the timeout is in the past, the thread
    // will be woken up immediately and will not sleep.
    
    self->sleep();
        
    // Set the timer and sleep reason
    self->set_timer( timeout, Cyg_Thread::TIMEOUT );

    // Only enqueue if the timeout has not already fired.
    if( self->get_wake_reason() == Cyg_Thread::NONE )
        queue.enqueue( self );

    // Avoid calling ASRs during the following unlock.
    self->set_asr_inhibit();
        
    // Unlock the scheduler and switch threads
    Cyg_Scheduler::unlock_reschedule();

    // Allow ASRs again
    self->clear_asr_inhibit();
                
    CYG_ASSERTCLASS( this, "Bad this pointer");
    CYG_ASSERTCLASS( mx, "Corrupt mutex");
    
    self->clear_timer();

    CYG_INSTRUMENT_CONDVAR(WOKE, this, self->get_wake_reason());
    
    switch( self->get_wake_reason() )
    {
    case Cyg_Thread::TIMEOUT:            
    case Cyg_Thread::DESTRUCT:          // which, the cv or the mutex?
    case Cyg_Thread::BREAK:
        result = false;
        break;
            
    case Cyg_Thread::EXIT:            
        self->exit();
        break;

    default:
        break;
    }

    
    // When we awake, we must re-acquire the mutex.  Note that while
    // it is essential to release the mutex and queue on the CV
    // atomically relative to other threads, to avoid races, it is not
    // necessary for us to re-acquire the mutex in the same atomic
    // action. Hence we can do it after unlocking the scheduler.

    while ( !mx->lock() )
        continue;
    
    CYG_ASSERTCLASS( this, "Bad this pointer");
    CYG_ASSERTCLASS( mx, "Corrupt mutex");

    CYG_REPORT_RETVAL(result);
    
    return result;
}

#endif


// -------------------------------------------------------------------------
// EOF sync/mutex.cxx