X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fmessages.c;h=ce23d6b14f043a90483d229a379e9a2b5be6f5d9;hb=e6795eb552a6be88b7870dae14a958ab391bfae8;hp=31ae8f3731063128d48f2c3684d07708b875271d;hpb=8bc40333b1401d7616b225945fee53d972c2f418;p=tpg%2Facess2.git diff --git a/Kernel/messages.c b/Kernel/messages.c index 31ae8f37..ce23d6b1 100644 --- a/Kernel/messages.c +++ b/Kernel/messages.c @@ -2,45 +2,55 @@ * AcessOS Microkernel Version * messages.c */ -#include -#include +#define DEBUG 1 +#include +#include +#include #include +// === IMPORTS === +extern tShortSpinlock glThreadListLock; + // === CODE === /** * \fn int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data) * \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,65 @@ 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); + 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(); // Check if queue has any items - if(!gCurrentThread->Messages) { + if(!cur->Messages) { return 0; } - LOCK( &gCurrentThread->IsLocked ); + SHORTLOCK( &cur->IsLocked ); if(Source) - *Source = gCurrentThread->Messages->Source; + *Source = cur->Messages->Source; // Get message length if( !Buffer ) { - ret = gCurrentThread->Messages->Length; - RELEASE( &gCurrentThread->IsLocked ); + ret = cur->Messages->Length; + SHORTREL( &cur->IsLocked ); 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 ) ) + { + *Err = -EINVAL; + SHORTREL( &cur->IsLocked ); + return -1; + } + 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; + + SHORTREL( &cur->IsLocked ); - RELEASE( &gCurrentThread->IsLocked ); + free(tmp); // Free outside of lock return ret; }