15 return KE_ILLEGAL_CONTEXT;
18 if (mbx_param->attr & ~(MBA_THFIFO | MBA_THPRI | MBA_MSFIFO | MBA_MSPRI)) {
19 return KE_ILLEGAL_ATTR;
24 mbx = heap_alloc(TAG_MBX,
sizeof(*mbx));
30 mbx->tag.id = ++thctx.mbox_id;
31 mbx->event.attr = mbx_param->attr;
32 mbx->event.option = mbx_param->option;
33 list_init(&mbx->event.waiters);
34 list_insert(&thctx.mbox, &mbx->mbox_list);
38 return MAKE_HANDLE(mbx);
41int DeleteMbx(
int mbxid)
49 return KE_ILLEGAL_CONTEXT;
54 mbx = HANDLE_PTR(mbxid);
55 if (!HANDLE_VERIFY(mbxid, TAG_MBX)) {
57 return KE_UNKNOWN_MBXID;
60 list_for_each_safe (waiter, &mbx->event.waiters, queue) {
61 waiter->saved_regs->v0 = KE_WAIT_DELETE;
62 list_remove(&waiter->queue);
63 waiter->status = THS_READY;
64 readyq_insert_back(waiter);
67 waiter_count = mbx->event.waiter_count;
68 list_remove(&mbx->mbox_list);
72 thctx.run_next = NULL;
73 return thread_leave(KE_OK, 0, state, 0);
81int SendMbx(
int mbxid,
void *msg)
89 return KE_ILLEGAL_CONTEXT;
94 mbx = HANDLE_PTR(mbxid);
95 if (!HANDLE_VERIFY(mbxid, TAG_MBX)) {
97 return KE_UNKNOWN_MBXID;
100 if (mbx->event.waiter_count == 0) {
106 thread = list_first_entry(&mbx->event.waiters,
struct thread, queue);
107 mbx->event.waiter_count--;
108 list_remove(&
thread->queue);
110 msg_dest = (
void **)
thread->saved_regs->v0;
112 thread->saved_regs->v0 = KE_OK;
118 return thread_start(
thread, state);
121int iSendMbx(
int mbxid,
void *msg)
128 return KE_ILLEGAL_CONTEXT;
131 mbx = HANDLE_PTR(mbxid);
132 if (!HANDLE_VERIFY(mbxid, TAG_MBX)) {
133 return KE_UNKNOWN_MBXID;
136 if (mbx->event.waiter_count == 0) {
141 thread = list_first_entry(&mbx->event.waiters,
struct thread, queue);
142 mbx->event.waiter_count--;
143 list_remove(&
thread->queue);
144 msg_dest = (
void **)
thread->saved_regs->v0;
145 thread->saved_regs->v0 = KE_OK;
151 readyq_insert_back(
thread);
152 thctx.run_next = NULL;
157int ReceiveMbx(
void **msgvar,
int mbxid)
165 return KE_ILLEGAL_CONTEXT;
168 if (
CpuSuspendIntr(&state) == KE_CPUDI && (thctx.debug_flags & 8)) {
169 Kprintf(
"WARNING: ReceiveMbx:KE_CAN_NOT_WAIT\n");
172 mbx = HANDLE_PTR(mbxid);
173 if (!HANDLE_VERIFY(mbxid, TAG_MBX)) {
175 return KE_UNKNOWN_MBXID;
178 if (mbx->msg_count == 0) {
179 thread = thctx.current_thread;
180 thread->status = THS_WAIT;
181 thread->wait_type = TSW_MBX;
182 thread->wait_event = &mbx->event;
183 thctx.run_next = NULL;
185 if (mbx->event.attr & MBA_THPRI) {
188 list_insert(&mbx->event.waiters, &
thread->queue);
191 return thread_leave((
int)msgvar, 0, state, 1);
195 msg = mbx->newest_msg->next;
196 if (msg == msg->next) {
197 mbx->newest_msg = NULL;
199 mbx->newest_msg->next = msg->next;
209int PollMbx(
void **msgvar,
int mbxid)
216 return KE_ILLEGAL_CONTEXT;
221 mbx = HANDLE_PTR(mbxid);
222 if (!HANDLE_VERIFY(mbxid, TAG_MBX)) {
224 return KE_UNKNOWN_MBXID;
227 if (mbx->msg_count == 0) {
229 return KE_MBOX_NOMSG;
233 msg = mbx->newest_msg->next;
234 if (msg == msg->next) {
235 mbx->newest_msg = NULL;
237 mbx->newest_msg->next = msg->next;
253 return KE_ILLEGAL_CONTEXT;
258 mbx = HANDLE_PTR(mbxid);
259 if (!HANDLE_VERIFY(mbxid, TAG_MBX)) {
261 return KE_UNKNOWN_MBXID;
264 mbx_get_status(mbx,
info);
276 return KE_ILLEGAL_CONTEXT;
279 mbx = HANDLE_PTR(mbxid);
280 if (!HANDLE_VERIFY(mbxid, TAG_MBX)) {
281 return KE_UNKNOWN_MBXID;
284 mbx_get_status(mbx,
info);
293 latest = mbx->newest_msg;
294 oldest = latest->next;
298 if (!mbx->newest_msg) {
299 mbx->newest_msg = new_msg;
300 new_msg->next = new_msg;
304 if ((mbx->event.attr & MBA_MSPRI) == 0) {
305 new_msg->next = oldest;
306 latest->next = new_msg;
307 mbx->newest_msg = new_msg;
314 prio = latest->next->priority;
316 piVar1 = latest->next;
319 if (new_msg->priority < prio) {
320 new_msg->next = piVar2->next;
321 piVar2->next = new_msg;
327 if (piVar1 == latest) {
328 mbx->newest_msg = new_msg;
329 new_msg->next = piVar2->next;
330 piVar2->next = new_msg;
334 prio = piVar1->next->priority;
335 piVar1 = piVar1->next;
342 info->attr = mbx->event.attr;
343 info->option = mbx->event.option;
344 info->numWaitThreads = mbx->event.waiter_count;
345 info->numMessage = mbx->msg_count;
346 info->topPacket = mbx->newest_msg->next;
int CpuResumeIntr(int state)
int QueryIntrContext(void)
int CpuSuspendIntr(int *state)