15 return KE_ILLEGAL_CONTEXT;
18 if ((event_params->attr & ~
EA_MULTI) != 0) {
19 return KE_ILLEGAL_ATTR;
24 event = heap_alloc(TAG_EVF,
sizeof(*
event));
30 event->tag.id = ++thctx.evflag_id;
31 event->event.attr = event_params->attr;
32 event->event.option = event_params->option;
33 event->init_bits = event_params->bits;
34 event->bits = event_params->bits;
36 list_init(&
event->event.waiters);
38 list_insert(&thctx.event_flag, &
event->evf_list);
42 return MAKE_HANDLE(
event);
45int DeleteEventFlag(
int ef)
53 return KE_ILLEGAL_CONTEXT;
58 event = HANDLE_PTR(ef);
59 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
61 return KE_UNKNOWN_EVFID;
64 list_for_each_safe (waiter, &
event->event.waiters, queue) {
65 waiter->saved_regs->v0 = KE_WAIT_DELETE;
66 list_remove(&waiter->queue);
67 waiter->status = THS_READY;
68 readyq_insert_back(waiter);
71 waiter_count =
event->event.waiter_count;
72 list_remove(&
event->evf_list);
73 heap_free(&
event->tag);
76 thctx.run_next = NULL;
77 return thread_leave(KE_OK, 0, state, 0);
85int SetEventFlag(
int ef, u32 bits)
89 int state, num_wakeups;
90 u32 shared_bits, *resbits;
93 return KE_ILLEGAL_CONTEXT;
98 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
100 return KE_UNKNOWN_EVFID;
111 list_for_each_safe (
thread, &evt->event.waiters, queue) {
116 shared_bits =
thread->event_bits & evt->bits;
117 if ((
thread->event_mode & WEF_OR) == 0) {
118 shared_bits = shared_bits ==
thread->event_bits;
124 resbits = (u32 *)
thread->saved_regs->v0;
127 *resbits = evt->bits;
130 if (
thread->event_mode & WEF_CLEAR) {
136 evt->event.waiter_count--;
137 list_remove(&
thread->queue);
138 thread->status = THS_READY;
139 readyq_insert_back(
thread);
143 if (num_wakeups == 1) {
145 return thread_start(
thread, state);
148 if (num_wakeups > 0) {
150 return thread_leave(KE_OK, 0, state, 0);
158int iSetEventFlag(
int ef, u32 bits)
162 u32 shared_bits, *resbits;
165 return KE_ILLEGAL_CONTEXT;
168 evt = HANDLE_PTR(ef);
169 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
170 return KE_UNKNOWN_EVFID;
179 list_for_each_safe (
thread, &evt->event.waiters, queue) {
184 shared_bits =
thread->event_bits & evt->bits;
185 if ((
thread->event_mode & WEF_OR) == 0) {
186 shared_bits = shared_bits ==
thread->event_bits;
192 resbits = (u32 *)
thread->saved_regs->v0;
195 *resbits = evt->bits;
198 if (
thread->event_mode & WEF_CLEAR) {
202 evt->event.waiter_count--;
203 list_remove(&
thread->queue);
204 thread->status = THS_READY;
205 readyq_insert_back(
thread);
206 thctx.run_next = NULL;
214int ClearEventFlag(
int ef, u32 bits)
220 return KE_ILLEGAL_CONTEXT;
225 evt = HANDLE_PTR(ef);
226 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
228 return KE_UNKNOWN_EVFID;
238int iClearEventFlag(
int ef, u32 bits)
243 return KE_ILLEGAL_CONTEXT;
246 evt = HANDLE_PTR(ef);
247 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
248 return KE_UNKNOWN_EVFID;
256int WaitEventFlag(
int ef, u32 bits,
int mode, u32 *resbits)
264 return KE_ILLEGAL_CONTEXT;
267 if (mode & ~(WEF_AND | WEF_OR | WEF_CLEAR)) {
268 return KE_ILLEGAL_MODE;
275 if (
CpuSuspendIntr(&state) == KE_CPUDI && (thctx.debug_flags & 8)) {
276 Kprintf(
"WARNING: DelayThread KE_CAN_NOT_WAIT\n");
279 check_thread_stack();
281 evt = HANDLE_PTR(ef);
282 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
284 return KE_UNKNOWN_EVFID;
287 if ((evt->event.attr &
EA_MULTI) == 0 && evt->event.waiter_count >= 0) {
293 shared_bits = evt->bits & bits;
295 shared_bits = (evt->bits & bits) == bits;
300 *resbits = evt->bits;
303 if (mode & WEF_CLEAR) {
311 thread = thctx.current_thread;
312 thread->status = THS_WAIT;
313 thread->wait_type = TSW_EVENTFLAG;
314 thread->wait_event = &evt->event;
315 thread->event_bits = bits;
316 thread->event_mode = bits;
317 thctx.run_next = NULL;
318 evt->event.waiter_count++;
319 list_insert(&evt->event.waiters, &
thread->queue);
323 return thread_leave((
int)resbits, 0, state, 1);
326int PollEventFlag(
int ef, u32 bits,
int mode, u32 *resbits)
333 return KE_ILLEGAL_CONTEXT;
336 if (mode & ~(WEF_AND | WEF_OR | WEF_CLEAR)) {
337 return KE_ILLEGAL_MODE;
346 evt = HANDLE_PTR(ef);
347 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
349 return KE_UNKNOWN_EVFID;
352 if ((evt->event.attr &
EA_MULTI) == 0 && evt->event.waiter_count >= 0) {
358 shared_bits = evt->bits & bits;
360 shared_bits = (evt->bits & bits) == bits;
363 if (shared_bits == 0) {
369 *resbits = evt->bits;
372 if (mode & WEF_CLEAR) {
387 return KE_ILLEGAL_CONTEXT;
392 event = HANDLE_PTR(ef);
393 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
395 return KE_UNKNOWN_EVFID;
410 return KE_ILLEGAL_CONTEXT;
413 event = HANDLE_PTR(ef);
414 if (!HANDLE_VERIFY(ef, TAG_EVF)) {
415 return KE_UNKNOWN_EVFID;
425 info->attr =
event->event.attr;
426 info->currBits =
event->bits;
427 info->initBits =
event->init_bits;
428 info->numThreads =
event->event.waiter_count;
429 info->option =
event->event.option;
int CpuResumeIntr(int state)
int QueryIntrContext(void)
int CpuSuspendIntr(int *state)