/*
- * AcessOS Microkernel Version
+ * Acess2 Kernel
+ * - By John Hodge (thePowersGang)
+ *
* messages.c
+ * - IPC Messages
*/
-#include <common.h>
-#include <proc.h>
+#define DEBUG 0
+#include <acess.h>
+#include <threads.h>
+#include <threads_int.h>
#include <errno.h>
// === CODE ===
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 = 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 );
thread->LastMessage = msg;
}
- RELEASE(&thread->IsLocked);
+ SHORTREL(&thread->IsLocked);
+ SHORTLOCK(&glThreadListLock);
+ LOG("Waking %p (%i %s)", thread, thread->TID, thread->ThreadName);
Threads_Wake( thread );
+ SHORTREL(&glThreadListLock);
- return 0;
+ LEAVE_RET('i', 0);
}
/**
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(!cur->Messages) {
+ LEAVE('i', 0);
return 0;
}
- LOCK( &cur->IsLocked );
+ SHORTLOCK( &cur->IsLocked );
- if(Source)
+ if(Source) {
*Source = cur->Messages->Source;
+ LOG("*Source = %i", *Source);
+ }
// Get message length
if( !Buffer ) {
ret = cur->Messages->Length;
- RELEASE( &cur->IsLocked );
+ SHORTREL( &cur->IsLocked );
+ LEAVE('i', ret);
return ret;
}
// Get message
if(Buffer != GETMSG_IGNORE)
+ {
+ 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 = cur->Messages->Next;
- free(cur->Messages);
- cur->Messages = tmp;
+ tmp = cur->Messages;
+ cur->Messages = cur->Messages->Next;
+ if(cur->Messages == NULL) cur->LastMessage = NULL;
- RELEASE( &cur->IsLocked );
+ SHORTREL( &cur->IsLocked );
+ free(tmp); // Free outside of lock
+
+ LEAVE('i', ret);
return ret;
}