1 /*-------------------------------------------------------------
3 message.c -- Thread subsystem II
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
29 -------------------------------------------------------------*/
34 #include <lwp_messages.h>
35 #include <lwp_objmgr.h>
36 #include <lwp_config.h>
39 #define LWP_OBJTYPE_MBOX 6
41 #define LWP_CHECK_MBOX(hndl) \
43 if(((hndl)==MQ_BOX_NULL) || (LWP_OBJTYPE(hndl)!=LWP_OBJTYPE_MBOX)) \
47 typedef struct _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
)
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()
76 __lwp_thread_dispatchdisable();
77 mqbox
= (mqbox_st
*)__lwp_objmgr_allocate(&_lwp_mqbox_objects
);
79 __lwp_objmgr_open(&_lwp_mqbox_objects
,&mqbox
->object
);
82 __lwp_thread_dispatchenable();
86 s32
MQ_Init(mqbox_t
*mqbox
,u32 count
)
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
)
112 mbox
= __lwp_mqbox_open(mqbox
);
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
)
125 u32 wait
= (flags
==MQ_MSG_BLOCK
)?TRUE
:FALSE
;
127 mbox
= __lwp_mqbox_open(mqbox
);
128 if(!mbox
) return 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();
137 BOOL
MQ_Receive(mqbox_t mqbox
,mqmsg_t
*msg
,u32 flags
)
141 u32 tmp
,wait
= (flags
==MQ_MSG_BLOCK
)?TRUE
:FALSE
;
143 mbox
= __lwp_mqbox_open(mqbox
);
144 if(!mbox
) return 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();
153 BOOL
MQ_Jam(mqbox_t mqbox
,mqmsg_t msg
,u32 flags
)
157 u32 wait
= (flags
==MQ_MSG_BLOCK
)?TRUE
:FALSE
;
159 mbox
= __lwp_mqbox_open(mqbox
);
160 if(!mbox
) return 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();