* threads.c
* - Common Thread Control
*/
-#include <common.h>
+#include <acess.h>
#include <threads.h>
#include <errno.h>
// === PROTOTYPES ===
void Threads_Init();
-void Threads_SetName(char *NewName);
+ int Threads_SetName(char *NewName);
char *Threads_GetName(int ID);
void Threads_SetTickets(int Num);
tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
void Threads_AddActive(tThread *Thread);
int Threads_GetPID();
int Threads_GetTID();
- int Threads_GetUID();
- int Threads_GetGID();
+tUID Threads_GetUID();
+ int Threads_SetUID(Uint *Errno, tUID ID);
+tGID Threads_GetGID();
+ int Threads_SetGID(Uint *Errno, tUID ID);
void Threads_Dump();
// === GLOBALS ===
* \fn void Threads_SetName(char *NewName)
* \brief Sets the current thread's name
*/
-void Threads_SetName(char *NewName)
+int Threads_SetName(char *NewName)
{
tThread *cur = Proc_GetCurThread();
if( IsHeap(cur->ThreadName) )
free( cur->ThreadName );
cur->ThreadName = malloc(strlen(NewName)+1);
strcpy(cur->ThreadName, NewName);
+ return 0;
}
/**
}
/**
- * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread)
+ * \fn tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
* \brief Gets the previous entry in a thead linked list
*/
tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
/**
* \fn void Threads_Kill(tThread *Thread, int Status)
* \brief Kill a thread
- * \param TID Thread ID (0 for current)
+ * \param Thread Thread to kill
+ * \param Status Status code to return to the parent
*/
void Threads_Kill(tThread *Thread, int Status)
{
tThread *cur = Proc_GetCurThread();
tThread *thread;
- Log("Proc_Sleep: %i going to sleep", cur->TID);
+ //Log_Log("Threads", "%i going to sleep", cur->TID);
// Acquire Spinlock
LOCK( &giThreadListLock );
{
case THREAD_STAT_ACTIVE: break;
case THREAD_STAT_SLEEPING:
+ //Log_Log("Threads", "Waking %i (%p) from sleeping", Thread->TID, Thread);
LOCK( &giThreadListLock );
prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
prev->Next = Thread->Next; // Remove from sleeping queue
Thread->Next = gActiveThreads; // Add to active queue
gActiveThreads = Thread;
+ giNumActiveThreads ++;
+ giTotalTickets += Thread->NumTickets;
Thread->Status = THREAD_STAT_ACTIVE;
RELEASE( &giThreadListLock );
break;
}
}
+void Threads_WakeTID(tTID Thread)
+{
+ Threads_Wake( Threads_GetThread(Thread) );
+}
+
/**
* \fn void Threads_AddActive(tThread *Thread)
* \brief Adds a thread to the active queue
#endif
// --- Process Structure Access Functions ---
-int Threads_GetPID()
+tPID Threads_GetPID()
{
return Proc_GetCurThread()->TGID;
}
-int Threads_GetTID()
+tTID Threads_GetTID()
{
return Proc_GetCurThread()->TID;
}
-int Threads_GetUID()
+tUID Threads_GetUID()
{
return Proc_GetCurThread()->UID;
}
-int Threads_GetGID()
+tGID Threads_GetGID()
{
return Proc_GetCurThread()->GID;
}
+int Threads_SetUID(Uint *Errno, tUID ID)
+{
+ tThread *t = Proc_GetCurThread();
+ if( t->UID != 0 ) {
+ *Errno = -EACCES;
+ return -1;
+ }
+ Log("Threads_SetUID - Setting User ID to %i", ID);
+ t->UID = ID;
+ return 0;
+}
+
+int Threads_SetGID(Uint *Errno, tGID ID)
+{
+ tThread *t = Proc_GetCurThread();
+ if( t->UID != 0 ) {
+ *Errno = -EACCES;
+ return -1;
+ }
+ Log("Threads_SetGID - Setting Group ID to %i", ID);
+ t->GID = ID;
+ return 0;
+}
+
/**
* \fn void Threads_Dump()
* \brief Dums a list of currently running threads
int ticket;
int number;
- if(giNumActiveThreads == 0) return NULL;
+ if(giNumActiveThreads == 0) {
+ //Log_Debug("Threads", "CPU%i has no threads to run", CPU);
+ return NULL;
+ }
// Special case: 1 thread
if(giNumActiveThreads == 1) {
+ //Log_Debug("Threads", "CPU%i has only one thread %i %s",
+ // CPU, gActiveThreads->TID, gActiveThreads->ThreadName);
return gActiveThreads;
}
giTotalTickets, number);
}
+ //Log_Debug("Threads", "Switching CPU%i to %p (%s)",
+ // CPU, thread, thread->ThreadName);
+
return thread;
}
Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr);
Threads_Exit( 0, -1 );
}
+
+// === EXPORTS ===
+EXPORT(Threads_GetUID);