Fix libogc hardware lighting (GX_SetChanCtrl) - patch from https://sourceforge.net...
[libogc.git] / libogc / message.c
blob3dd9b2bf638c1ad327339facd44be5c0b540b619
1 /*-------------------------------------------------------------
3 message.c -- Thread subsystem II
5 Copyright (C) 2004
6 Michael Wiedenbauer (shagkur)
7 Dave Murphy (WinterMute)
9 This software is provided 'as-is', without any express or implied
10 warranty. In no event will the authors be held liable for any
11 damages arising from the use of this software.
13 Permission is granted to anyone to use this software for any
14 purpose, including commercial applications, and to alter it and
15 redistribute it freely, subject to the following restrictions:
17 1. The origin of this software must not be misrepresented; you
18 must not claim that you wrote the original software. If you use
19 this software in a product, an acknowledgment in the product
20 documentation would be appreciated but is not required.
22 2. Altered source versions must be plainly marked as such, and
23 must not be misrepresented as being the original software.
25 3. This notice may not be removed or altered from any source
26 distribution.
29 -------------------------------------------------------------*/
32 #include <asm.h>
33 #include <stdlib.h>
34 #include <lwp_messages.h>
35 #include <lwp_objmgr.h>
36 #include <lwp_config.h>
37 #include "message.h"
39 #define LWP_OBJTYPE_MBOX 6
41 #define LWP_CHECK_MBOX(hndl) \
42 { \
43 if(((hndl)==MQ_BOX_NULL) || (LWP_OBJTYPE(hndl)!=LWP_OBJTYPE_MBOX)) \
44 return NULL; \
47 typedef struct _mqbox_st
49 lwp_obj object;
50 mq_cntrl mqueue;
51 } mqbox_st;
53 lwp_objinfo _lwp_mqbox_objects;
55 void __lwp_mqbox_init()
57 __lwp_objmgr_initinfo(&_lwp_mqbox_objects,LWP_MAX_MQUEUES,sizeof(mqbox_st));
60 static __inline__ mqbox_st* __lwp_mqbox_open(mqbox_t mbox)
62 LWP_CHECK_MBOX(mbox);
63 return (mqbox_st*)__lwp_objmgr_get(&_lwp_mqbox_objects,LWP_OBJMASKID(mbox));
66 static __inline__ void __lwp_mqbox_free(mqbox_st *mqbox)
68 __lwp_objmgr_close(&_lwp_mqbox_objects,&mqbox->object);
69 __lwp_objmgr_free(&_lwp_mqbox_objects,&mqbox->object);
72 static mqbox_st* __lwp_mqbox_allocate()
74 mqbox_st *mqbox;
76 __lwp_thread_dispatchdisable();
77 mqbox = (mqbox_st*)__lwp_objmgr_allocate(&_lwp_mqbox_objects);
78 if(mqbox) {
79 __lwp_objmgr_open(&_lwp_mqbox_objects,&mqbox->object);
80 return mqbox;
82 __lwp_thread_dispatchenable();
83 return NULL;
86 s32 MQ_Init(mqbox_t *mqbox,u32 count)
88 mq_attr attr;
89 mqbox_st *ret = NULL;
91 if(!mqbox) return -1;
93 ret = __lwp_mqbox_allocate();
94 if(!ret) return MQ_ERROR_TOOMANY;
96 attr.mode = LWP_MQ_FIFO;
97 if(!__lwpmq_initialize(&ret->mqueue,&attr,count,sizeof(mqmsg_t))) {
98 __lwp_mqbox_free(ret);
99 __lwp_thread_dispatchenable();
100 return MQ_ERROR_TOOMANY;
103 *mqbox = (mqbox_t)(LWP_OBJMASKTYPE(LWP_OBJTYPE_MBOX)|LWP_OBJMASKID(ret->object.id));
104 __lwp_thread_dispatchenable();
105 return MQ_ERROR_SUCCESSFUL;
108 void MQ_Close(mqbox_t mqbox)
110 mqbox_st *mbox;
112 mbox = __lwp_mqbox_open(mqbox);
113 if(!mbox) return;
115 __lwpmq_close(&mbox->mqueue,0);
116 __lwp_thread_dispatchenable();
118 __lwp_mqbox_free(mbox);
121 BOOL MQ_Send(mqbox_t mqbox,mqmsg_t msg,u32 flags)
123 BOOL ret;
124 mqbox_st *mbox;
125 u32 wait = (flags==MQ_MSG_BLOCK)?TRUE:FALSE;
127 mbox = __lwp_mqbox_open(mqbox);
128 if(!mbox) return FALSE;
130 ret = FALSE;
131 if(__lwpmq_submit(&mbox->mqueue,mbox->object.id,msg,sizeof(mqmsg_t),LWP_MQ_SEND_REQUEST,wait,LWP_THREADQ_NOTIMEOUT)==LWP_MQ_STATUS_SUCCESSFUL) ret = TRUE;
132 __lwp_thread_dispatchenable();
134 return ret;
137 BOOL MQ_Receive(mqbox_t mqbox,mqmsg_t *msg,u32 flags)
139 BOOL ret;
140 mqbox_st *mbox;
141 u32 tmp,wait = (flags==MQ_MSG_BLOCK)?TRUE:FALSE;
143 mbox = __lwp_mqbox_open(mqbox);
144 if(!mbox) return FALSE;
146 ret = FALSE;
147 if(__lwpmq_seize(&mbox->mqueue,mbox->object.id,(void*)msg,&tmp,wait,LWP_THREADQ_NOTIMEOUT)==LWP_MQ_STATUS_SUCCESSFUL) ret = TRUE;
148 __lwp_thread_dispatchenable();
150 return ret;
153 BOOL MQ_Jam(mqbox_t mqbox,mqmsg_t msg,u32 flags)
155 BOOL ret;
156 mqbox_st *mbox;
157 u32 wait = (flags==MQ_MSG_BLOCK)?TRUE:FALSE;
159 mbox = __lwp_mqbox_open(mqbox);
160 if(!mbox) return FALSE;
162 ret = FALSE;
163 if(__lwpmq_submit(&mbox->mqueue,mbox->object.id,msg,sizeof(mqmsg_t),LWP_MQ_SEND_URGENT,wait,LWP_THREADQ_NOTIMEOUT)==LWP_MQ_STATUS_SUCCESSFUL) ret = TRUE;
164 __lwp_thread_dispatchenable();
166 return ret;