3 * - By John Hodge (thePowersGang)
11 #include <threads_int.h>
15 extern tShortSpinlock glThreadListLock;
19 * \fn int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data)
20 * \brief Send an IPC message
21 * \param Err Pointer to the errno variable
22 * \param Dest Destination Thread
23 * \param Length Length of the message
24 * \param Data Message data
26 int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data)
31 ENTER("pErr iDest iLength pData", Err, Dest, Length, Data);
33 if(Length <= 0 || !Data) {
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);
71 SHORTLOCK(&glThreadListLock);
72 LOG("Waking %p (%i %s)", thread, thread->TID, thread->ThreadName);
73 Threads_Wake( thread );
74 SHORTREL(&glThreadListLock);
80 * \fn int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer)
81 * \brief Gets a message
82 * \param Err Pointer to \a errno
83 * \param Source Where to put the source TID
84 * \param Buffer Buffer to place the message data (set to NULL to just get message length)
86 int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer)
90 tThread *cur = Proc_GetCurThread();
92 ENTER("pSource pBuffer", Source, Buffer);
94 // Check if queue has any items
100 SHORTLOCK( &cur->IsLocked );
103 *Source = cur->Messages->Source;
104 LOG("*Source = %i", *Source);
107 // Get message length
109 ret = cur->Messages->Length;
110 SHORTREL( &cur->IsLocked );
116 if(Buffer != GETMSG_IGNORE)
118 if( !CheckMem( Buffer, cur->Messages->Length ) )
120 LOG("Invalid buffer");
122 SHORTREL( &cur->IsLocked );
126 LOG("Copied to buffer");
127 memcpy(Buffer, cur->Messages->Data, cur->Messages->Length);
129 ret = cur->Messages->Length;
133 cur->Messages = cur->Messages->Next;
134 if(cur->Messages == NULL) cur->LastMessage = NULL;
136 SHORTREL( &cur->IsLocked );
138 free(tmp); // Free outside of lock