Mercurial > ecos
annotate packages/kernel/current/src/sync/bin_sem.cxx @ 64:c38311975d4f ecos-sw-2000-01-28
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
| author | jlarmour |
|---|---|
| date | Fri, 28 Jan 2000 04:59:39 +0000 |
| parents | 443894e2e912 |
| children | bf00f99aec69 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // sync/bin_sem.cxx |
| 0 | 4 // |
| 2 | 5 // Binary semaphore implementation |
| 0 | 6 // |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // http://sourceware.cygnus.com/ecos |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // Software distributed under the License is distributed on an |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 2 | 34 // Author(s): nickg |
| 35 // Contributors: nickg | |
| 36 // Date: 1997-09-24 | |
| 37 // Purpose: Cyg_Binary_Semaphore implementation | |
| 38 // Description: This file contains the implementations of the binary semaphore | |
| 0 | 39 // class. |
| 40 // | |
| 41 //####DESCRIPTIONEND#### | |
| 42 // | |
| 43 //========================================================================== | |
| 44 | |
| 45 #include <pkgconf/kernel.h> | |
| 46 | |
| 47 #include <cyg/kernel/ktypes.h> // base kernel types | |
| 48 #include <cyg/infra/cyg_trac.h> // tracing macros | |
| 49 #include <cyg/infra/cyg_ass.h> // assertion macros | |
| 50 #include <cyg/kernel/instrmnt.h> // instrumentation | |
| 51 | |
| 52 #include <cyg/kernel/thread.inl> // Cyg_Thread inlines | |
| 53 | |
| 54 #include <cyg/kernel/sema.hxx> // our header | |
| 55 | |
| 56 #include <cyg/kernel/sched.inl> // scheduler inlines | |
| 57 | |
| 58 // ------------------------------------------------------------------------- | |
| 59 | |
| 60 Cyg_Binary_Semaphore::Cyg_Binary_Semaphore ( | |
| 61 cyg_bool init_state | |
| 62 ) | |
| 63 { | |
| 64 state = init_state; | |
| 65 } | |
| 66 | |
| 67 // ------------------------------------------------------------------------- | |
| 68 | |
| 69 Cyg_Binary_Semaphore::~Cyg_Binary_Semaphore ( ) | |
| 70 { | |
| 71 } | |
| 72 | |
| 73 // ------------------------------------------------------------------------- | |
| 74 | |
| 75 cyg_bool Cyg_Binary_Semaphore::wait() | |
| 76 { | |
| 77 cyg_bool result = true; | |
| 78 Cyg_Thread *self = Cyg_Thread::self(); | |
| 79 | |
| 80 // Prevent preemption | |
| 81 Cyg_Scheduler::lock(); | |
| 82 | |
| 83 CYG_INSTRUMENT_BINSEM( CLAIM, this, state ); | |
| 84 | |
| 85 while( !state && result ) | |
| 86 { | |
| 87 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 88 | |
| 89 self->sleep(); | |
| 90 | |
| 91 queue.enqueue( self ); | |
| 92 | |
| 93 CYG_INSTRUMENT_BINSEM( WAIT, this, 0 ); | |
| 94 | |
| 95 CYG_ASSERT( Cyg_Scheduler::get_sched_lock() == 1, "Called with non-zero scheduler lock"); | |
| 96 | |
| 97 Cyg_Scheduler::unlock(); | |
| 98 Cyg_Scheduler::lock(); | |
| 99 | |
| 100 CYG_INSTRUMENT_BINSEM( WOKE, this, state ); | |
| 101 | |
| 102 switch( self->get_wake_reason() ) | |
| 103 { | |
| 104 case Cyg_Thread::DESTRUCT: | |
| 105 case Cyg_Thread::BREAK: | |
| 106 result = false; | |
| 107 break; | |
| 108 | |
| 109 case Cyg_Thread::EXIT: | |
| 110 self->exit(); | |
| 111 break; | |
| 112 | |
| 113 default: | |
| 114 break; | |
| 115 } | |
| 116 | |
| 117 } | |
| 118 | |
| 119 if( result ) state = false; | |
| 120 | |
| 121 // Unlock the scheduler and maybe switch threads | |
| 122 Cyg_Scheduler::unlock(); | |
| 123 | |
| 124 return result; | |
| 125 } | |
| 126 | |
| 127 // ------------------------------------------------------------------------- | |
| 128 | |
| 129 cyg_bool Cyg_Binary_Semaphore::trywait() | |
| 130 { | |
| 131 cyg_bool result = true; | |
| 132 | |
| 133 // Prevent preemption | |
| 134 Cyg_Scheduler::lock(); | |
| 135 | |
| 136 if( state ) state = false; | |
| 137 else result = false; | |
| 138 | |
| 139 CYG_INSTRUMENT_BINSEM( TRY, this, result ); | |
| 140 | |
| 141 // Unlock the scheduler and maybe switch threads | |
| 142 Cyg_Scheduler::unlock(); | |
| 143 | |
| 144 return result; | |
| 145 } | |
| 146 | |
| 147 // ------------------------------------------------------------------------- | |
| 148 | |
| 149 void Cyg_Binary_Semaphore::post() | |
| 150 { | |
| 151 // Prevent preemption | |
| 152 Cyg_Scheduler::lock(); | |
| 153 | |
| 154 CYG_INSTRUMENT_BINSEM( POST, this, 0 ); | |
| 155 | |
| 156 state = true; | |
| 157 | |
| 158 if( !queue.empty() ) { | |
| 159 | |
| 160 // The queue is non-empty, so grab the next | |
| 161 // thread from it and wake it up. The waiter | |
| 162 // will clear the flag. | |
| 163 | |
| 164 Cyg_Thread *thread = queue.dequeue(); | |
| 165 | |
| 166 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 167 | |
| 168 thread->wake(); | |
| 169 | |
| 170 CYG_INSTRUMENT_BINSEM( WAKE, this, thread ); | |
| 171 } | |
| 172 | |
| 173 // Unlock the scheduler and maybe switch threads | |
| 174 Cyg_Scheduler::unlock(); | |
| 175 | |
| 176 } | |
| 177 | |
| 178 // ------------------------------------------------------------------------- | |
| 179 | |
| 180 cyg_bool Cyg_Binary_Semaphore::posted() | |
| 181 { | |
| 182 // This is a single read of the value of state. | |
| 183 // This is already atomic, hence there is no need | |
| 184 // to lock the scheduler. | |
| 185 | |
| 186 return state; | |
| 187 } | |
| 188 | |
| 189 // ------------------------------------------------------------------------- | |
| 190 // EOF sync/bin_sem.cxx |
