X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fmessages.c;h=2c0e448cf8d31be664ea7f9eb27a08784d2fba91;hb=e20b7220513e6010d883ae76ca1cf2c8f0ec26af;hp=31ae8f3731063128d48f2c3684d07708b875271d;hpb=8bc40333b1401d7616b225945fee53d972c2f418;p=tpg%2Facess2.git diff --git a/Kernel/messages.c b/Kernel/messages.c index 31ae8f37..2c0e448c 100644 --- a/Kernel/messages.c +++ b/Kernel/messages.c @@ -1,9 +1,14 @@ /* - * AcessOS Microkernel Version + * Acess2 Kernel + * - By John Hodge (thePowersGang) + * * messages.c + * - IPC Messages */ -#include -#include +#define DEBUG 0 +#include +#include +#include #include // === CODE === @@ -12,35 +17,40 @@ * \brief Send an IPC message * \param Err Pointer to the errno variable * \param Dest Destination Thread + * \param Length Length of the message + * \param Data Message data */ int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data) { tThread *thread; tMsg *msg; - Log("Proc_SendMessage: (Err=%p, Dest=%i, Length=%i, Data=%p)", Err, Dest, Length, Data); + ENTER("pErr iDest iLength pData", Err, Dest, Length, Data); if(Length <= 0 || !Data) { *Err = -EINVAL; - return -1; + LEAVE_RET('i', -1); } // Get thread - thread = Proc_GetThread( Dest ); + thread = Threads_GetThread( Dest ); // Error check - if(!thread) { return -1; } + if(!thread) LEAVE_RET('i', -1); // Get Spinlock - LOCK( &thread->IsLocked ); + SHORTLOCK( &thread->IsLocked ); // Check if thread is still alive - if(thread->Status == THREAD_STAT_DEAD) return -1; + if(thread->Status == THREAD_STAT_DEAD) { + SHORTREL( &thread->IsLocked ); + LEAVE_RET('i', -1); + } // Create message msg = malloc( sizeof(tMsg)+Length ); msg->Next = NULL; - msg->Source = gCurrentThread->TID; + msg->Source = Proc_GetCurThread()->TID; msg->Length = Length; memcpy(msg->Data, Data, Length); @@ -53,50 +63,77 @@ int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data) thread->LastMessage = msg; } - RELEASE(&thread->IsLocked); + SHORTREL(&thread->IsLocked); - Thread_Wake( thread ); + SHORTLOCK(&glThreadListLock); + LOG("Waking %p (%i %s)", thread, thread->TID, thread->ThreadName); + Threads_Wake( thread ); + SHORTREL(&glThreadListLock); - return 0; + LEAVE_RET('i', 0); } /** * \fn int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer) * \brief Gets a message + * \param Err Pointer to \a errno + * \param Source Where to put the source TID + * \param Buffer Buffer to place the message data (set to NULL to just get message length) */ int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer) { int ret; - void *tmp; + void *tmp; + tThread *cur = Proc_GetCurThread(); + + ENTER("pSource pBuffer", Source, Buffer); // Check if queue has any items - if(!gCurrentThread->Messages) { + if(!cur->Messages) { + LEAVE('i', 0); return 0; } - LOCK( &gCurrentThread->IsLocked ); + SHORTLOCK( &cur->IsLocked ); - if(Source) - *Source = gCurrentThread->Messages->Source; + if(Source) { + *Source = cur->Messages->Source; + LOG("*Source = %i", *Source); + } // Get message length if( !Buffer ) { - ret = gCurrentThread->Messages->Length; - RELEASE( &gCurrentThread->IsLocked ); + ret = cur->Messages->Length; + SHORTREL( &cur->IsLocked ); + LEAVE('i', ret); return ret; } // Get message if(Buffer != GETMSG_IGNORE) - memcpy(Buffer, gCurrentThread->Messages->Data, gCurrentThread->Messages->Length); - ret = gCurrentThread->Messages->Length; + { + if( !CheckMem( Buffer, cur->Messages->Length ) ) + { + LOG("Invalid buffer"); + *Err = -EINVAL; + SHORTREL( &cur->IsLocked ); + LEAVE('i', -1); + return -1; + } + LOG("Copied to buffer"); + memcpy(Buffer, cur->Messages->Data, cur->Messages->Length); + } + ret = cur->Messages->Length; // Remove from list - tmp = gCurrentThread->Messages->Next; - free(gCurrentThread->Messages); - gCurrentThread->Messages = tmp; + tmp = cur->Messages; + cur->Messages = cur->Messages->Next; + if(cur->Messages == NULL) cur->LastMessage = NULL; - RELEASE( &gCurrentThread->IsLocked ); + SHORTREL( &cur->IsLocked ); + free(tmp); // Free outside of lock + + LEAVE('i', ret); return ret; }