2 * AcessOS Microkernel Version
7 #include <threads_int.h>
11 extern tShortSpinlock glThreadListLock;
15 * \fn int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data)
16 * \brief Send an IPC message
17 * \param Err Pointer to the errno variable
18 * \param Dest Destination Thread
19 * \param Length Length of the message
20 * \param Data Message data
22 int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data)
27 Log("Proc_SendMessage: (Err=%p, Dest=%i, Length=%i, Data=%p)", Err, Dest, Length, Data);
29 if(Length <= 0 || !Data) {
35 thread = Threads_GetThread( Dest );
38 if(!thread) { return -1; }
41 SHORTLOCK( &thread->IsLocked );
43 // Check if thread is still alive
44 if(thread->Status == THREAD_STAT_DEAD) {
45 SHORTREL( &thread->IsLocked );
50 msg = malloc( sizeof(tMsg)+Length );
52 msg->Source = Proc_GetCurThread()->TID;
54 memcpy(msg->Data, Data, Length);
56 // If there are already messages
57 if(thread->LastMessage) {
58 thread->LastMessage->Next = msg;
59 thread->LastMessage = msg;
61 thread->Messages = msg;
62 thread->LastMessage = msg;
65 SHORTREL(&thread->IsLocked);
67 SHORTLOCK(&glThreadListLock);
68 Threads_Wake( thread );
69 SHORTREL(&glThreadListLock);
75 * \fn int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer)
76 * \brief Gets a message
77 * \param Err Pointer to \a errno
78 * \param Source Where to put the source TID
79 * \param Buffer Buffer to place the message data (set to NULL to just get message length)
81 int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer)
85 tThread *cur = Proc_GetCurThread();
87 // Check if queue has any items
92 SHORTLOCK( &cur->IsLocked );
95 *Source = cur->Messages->Source;
99 ret = cur->Messages->Length;
100 SHORTREL( &cur->IsLocked );
105 if(Buffer != GETMSG_IGNORE)
107 if( !CheckMem( Buffer, cur->Messages->Length ) )
110 SHORTREL( &cur->IsLocked );
113 memcpy(Buffer, cur->Messages->Data, cur->Messages->Length);
115 ret = cur->Messages->Length;
119 cur->Messages = cur->Messages->Next;
121 SHORTREL( &cur->IsLocked );
123 free(tmp); // Free outside of lock