Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "event.h"
00024 #include "system.h"
00025 #include <common/thlist.h>
00026
00061 __STATIC __VOID __eventQueueThread(__PEVENT ev, __PTHREAD th, u32 timeout)
00062 {
00063
00064
00065
00066
00067 th->th_wait = CPU_MS_TO_TICKS(timeout);
00068 __threadSuspend(th, __THSTS_WAITING);
00069
00070
00071 __thlAddEvtPrio(th, &ev->ev_threads);
00072 }
00073
00083 __STATIC __VOID __eventDequeueThread(__PEVENT ev, __PTHREAD th)
00084 {
00085 __thlRemoveEvtPrio(th, &ev->ev_threads);
00086 }
00087
00094 __STATIC __VOID __eventSignal(__PEVENT ev, u8 val)
00095 {
00096 __PTHREAD th;
00097
00098
00099 ev->ev_state = val;
00100
00101
00102
00103
00104 if (ev->ev_threads)
00105 {
00106
00107 __systemStop();
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 if (__threadGetCurrent())
00119 {
00120 if (__threadGetCurrent()->th_priority > ev->ev_threads->th_priority)
00121 {
00122
00123
00124
00125
00126
00127 if (!__systemSchedulerDisabled())
00128 {
00129
00130
00131 __threadAddToReadyList(__threadGetCurrent());
00132
00133
00134 __threadSetCurrent(__NULL);
00135 }
00136
00137
00138 __systemScheduleThreadChange();
00139 }
00140 } else {
00141
00142
00143
00144
00145 __systemScheduleThreadChange();
00146 }
00147
00148
00149 th = ev->ev_threads;
00150 while (th)
00151 {
00152
00153
00154
00155
00156
00157 if (th->th_status == __THSTS_WAITING)
00158 {
00159 __threadRemoveFromSuspended(th);
00160 __threadAddToReadyList(th);
00161 }
00162
00163 th = th->th_evnext;
00164 }
00165
00166 __systemStart();
00167 }
00168 }
00169
00184 u8 __eventWait(__PEVENT ev, u32 timeout)
00185 {
00186 u8 ret = 0;
00187 u32 irqs = 0;
00188 __PTHREAD th;
00189
00190
00191 irqs = __systemGetIrqCount();
00192 if (!irqs)
00193 {
00194
00195 __systemStop();
00196 } else {
00197
00198
00199
00200
00201
00202
00203 while (__systemGetIrqCount() != 1) __systemStart();
00204 }
00205
00206
00207 if (ev->ev_state != __EV_RESET)
00208 {
00209
00210
00211
00212
00213
00214 if (!irqs) {
00215 __systemStart();
00216 }
00217 return(__EVRET_SUCCESS);
00218 }
00219
00220 th = __threadGetCurrent();
00221
00222
00223 __eventQueueThread(ev, th, timeout);
00224
00225
00226 __threadSetCurrent(__NULL);
00227
00228
00229 __systemScheduleThreadChange();
00230
00231
00232 __systemStart();
00233
00234 while (th->th_status != __THSTS_RUNNING);
00235
00236
00237
00238
00239
00240
00241 __systemStop();
00242
00243
00244 __eventDequeueThread(ev, th);
00245
00246
00247 if (irqs == 0) {
00248 __systemStart();
00249 } else {
00250
00251 while (__systemGetIrqCount() != irqs) __systemStop();
00252 }
00253
00254
00255 if (timeout && !th->th_wait)
00256 {
00257 ret = __EVRET_TIMEOUT;
00258 } else
00259 {
00260 ret = __EVRET_SUCCESS;
00261 }
00262
00263 if (ev->ev_state == __EV_ABORT)
00264 {
00265 ret = __EVRET_ABORT;
00266 }
00267
00268 return ret;
00269 }
00270
00280 __VOID __eventSet(__PEVENT ev)
00281 {
00282 __eventSignal(ev, __EV_SET);
00283 }
00284
00285
00295 __VOID __eventSetOne(__PEVENT ev)
00296 {
00297 __systemStop();
00298
00299 ev->ev_state = __EV_SET;
00300
00301
00302
00303
00304 if (ev->ev_threads)
00305 {
00306 if (__threadGetCurrent())
00307 {
00308 if (__threadGetCurrent()->th_priority > ev->ev_threads->th_priority)
00309 {
00310
00311
00312
00313
00314
00315 if (!__systemSchedulerDisabled())
00316 {
00317 __threadAddToReadyList(__threadGetCurrent());
00318 __threadSetCurrent(__NULL);
00319 __systemScheduleThreadChange();
00320 }
00321 }
00322 } else {
00323 __systemScheduleThreadChange();
00324 }
00325
00326 if (ev->ev_threads->th_status == __THSTS_WAITING)
00327 {
00328 __threadRemoveFromSuspended(ev->ev_threads);
00329 __threadAddToReadyList(ev->ev_threads);
00330 }
00331
00332 __thlRemoveEvtPrio(ev->ev_threads, (__PTHREAD*) &ev->ev_threads);
00333 }
00334
00335 __systemStart();
00336 }
00337
00345 __VOID __eventReset(__PEVENT ev)
00346 {
00347 __systemStop();
00348 ev->ev_state = __EV_RESET;
00349 __systemStart();
00350 }
00351
00364 __VOID __eventAbort(__PEVENT ev)
00365 {
00366 __eventSignal(ev, __EV_ABORT);
00367 }
00368
00377 __PTHREAD __eventGetWaitingThreads(__PEVENT ev)
00378 {
00379 return ev->ev_threads;
00380 }
00381