Usermode/libc - Changes to get NASM/irssi/bash ... compiling
[tpg/acess2.git] / Kernel / messages.c
1 /*
2  * AcessOS Microkernel Version
3  * messages.c
4  */
5 #define DEBUG   1
6 #include <acess.h>
7 #include <threads.h>
8 #include <threads_int.h>
9 #include <errno.h>
10
11 // === IMPORTS ===
12 extern tShortSpinlock   glThreadListLock;
13
14 // === CODE ===
15 /**
16  * \fn int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data)
17  * \brief Send an IPC message
18  * \param Err   Pointer to the errno variable
19  * \param Dest  Destination Thread
20  * \param Length        Length of the message
21  * \param Data  Message data
22  */
23 int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data)
24 {
25         tThread *thread;
26         tMsg    *msg;
27         
28         ENTER("pErr iDest iLength pData", Err, Dest, Length, Data);
29         
30         if(Length <= 0 || !Data) {
31                 *Err = -EINVAL;
32                 LEAVE_RET('i', -1);
33         }
34         
35         // Get thread
36         thread = Threads_GetThread( Dest );
37         
38         // Error check
39         if(!thread)     LEAVE_RET('i', -1);
40         
41         // Get Spinlock
42         SHORTLOCK( &thread->IsLocked );
43         
44         // Check if thread is still alive
45         if(thread->Status == THREAD_STAT_DEAD) {
46                 SHORTREL( &thread->IsLocked );
47                 LEAVE_RET('i', -1);
48         }
49         
50         // Create message
51         msg = malloc( sizeof(tMsg)+Length );
52         msg->Next = NULL;
53         msg->Source = Proc_GetCurThread()->TID;
54         msg->Length = Length;
55         memcpy(msg->Data, Data, Length);
56         
57         // If there are already messages
58         if(thread->LastMessage) {
59                 thread->LastMessage->Next = msg;
60                 thread->LastMessage = msg;
61         } else {
62                 thread->Messages = msg;
63                 thread->LastMessage = msg;
64         }
65         
66         SHORTREL(&thread->IsLocked);
67         
68         SHORTLOCK(&glThreadListLock);
69         Threads_Wake( thread );
70         SHORTREL(&glThreadListLock);
71         
72         LEAVE_RET('i', 0);
73 }
74
75 /**
76  * \fn int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer)
77  * \brief Gets a message
78  * \param Err   Pointer to \a errno
79  * \param Source        Where to put the source TID
80  * \param Buffer        Buffer to place the message data (set to NULL to just get message length)
81  */
82 int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer)
83 {
84          int    ret;
85         void    *tmp;
86         tThread *cur = Proc_GetCurThread();
87         
88         // Check if queue has any items
89         if(!cur->Messages) {
90                 return 0;
91         }
92
93         SHORTLOCK( &cur->IsLocked );
94         
95         if(Source)
96                 *Source = cur->Messages->Source;
97         
98         // Get message length
99         if( !Buffer ) {
100                 ret = cur->Messages->Length;
101                 SHORTREL( &cur->IsLocked );
102                 return ret;
103         }
104         
105         // Get message
106         if(Buffer != GETMSG_IGNORE)
107         {
108                 if( !CheckMem( Buffer, cur->Messages->Length ) )
109                 {
110                         *Err = -EINVAL;
111                         SHORTREL( &cur->IsLocked );
112                         return -1;
113                 }
114                 memcpy(Buffer, cur->Messages->Data, cur->Messages->Length);
115         }
116         ret = cur->Messages->Length;
117         
118         // Remove from list
119         tmp = cur->Messages;
120         cur->Messages = cur->Messages->Next;
121         
122         SHORTREL( &cur->IsLocked );
123         
124         free(tmp);      // Free outside of lock
125         
126         return ret;
127 }

UCC git Repository :: git.ucc.asn.au