3 * - By John Hodge (thePowersGang)
11 #include <threads_int.h>
17 * \fn int Proc_SendMessage(Uint Dest, int Length, void *Data)
18 * \brief Send an IPC message
19 * \param Dest Destination Thread
20 * \param Length Length of the message
21 * \param Data Message data
23 int Proc_SendMessage(Uint Dest, int Length, void *Data)
28 ENTER("pErr iDest iLength pData", Err, Dest, Length, Data);
30 if(Length <= 0 || !Data) {
35 // TODO: Check message length against global/per-thread maximums
36 // TODO: Restrict queue length
39 thread = Threads_GetThread( Dest );
42 if(!thread) LEAVE_RET('i', -1);
45 SHORTLOCK( &thread->IsLocked );
47 // Check if thread is still alive
48 if(thread->Status == THREAD_STAT_DEAD) {
49 SHORTREL( &thread->IsLocked );
54 msg = malloc( sizeof(tMsg)+Length );
56 msg->Source = Proc_GetCurThread()->TID;
58 memcpy(msg->Data, Data, Length);
60 // If there are already messages
61 if(thread->LastMessage) {
62 thread->LastMessage->Next = msg;
63 thread->LastMessage = msg;
65 thread->Messages = msg;
66 thread->LastMessage = msg;
69 SHORTREL(&thread->IsLocked);
72 LOG("Waking %p (%i %s)", thread, thread->TID, thread->ThreadName);
73 Threads_PostEvent( thread, THREAD_EVENT_IPCMSG );
79 * \fn int Proc_GetMessage(Uint *Source, void *Buffer)
80 * \brief Gets a message
81 * \param Source Where to put the source TID
82 * \param Buffer Buffer to place the message data (set to NULL to just get message length)
83 * \return Message length
85 int Proc_GetMessage(Uint *Source, void *Buffer)
89 tThread *cur = Proc_GetCurThread();
91 ENTER("pSource pBuffer", Source, Buffer);
93 // Check if queue has any items
99 SHORTLOCK( &cur->IsLocked );
102 *Source = cur->Messages->Source;
103 LOG("*Source = %i", *Source);
106 // Get message length
108 ret = cur->Messages->Length;
109 SHORTREL( &cur->IsLocked );
115 if(Buffer != GETMSG_IGNORE)
117 if( !CheckMem( Buffer, cur->Messages->Length ) )
119 LOG("Invalid buffer");
121 SHORTREL( &cur->IsLocked );
125 LOG("Copied to buffer");
126 memcpy(Buffer, cur->Messages->Data, cur->Messages->Length);
128 ret = cur->Messages->Length;
132 cur->Messages = cur->Messages->Next;
133 if(cur->Messages == NULL) cur->LastMessage = NULL;
135 SHORTREL( &cur->IsLocked );
137 free(tmp); // Free outside of lock