68fb95526ad873d19ed7f8d7d1b55354a3bc426b
[tpg/acess2.git] / Kernel / include / threads.h
1 /*
2  */
3 #ifndef _THREADS_H_
4 #define _THREADS_H_
5
6 #include <arch.h>
7 #include <signal.h>
8 #include <proc.h>
9
10 /**
11  * \brief IPC Message
12  */
13 typedef struct sMessage
14 {
15         struct sMessage *Next;  //!< Next message in thread's inbox
16         tTID    Source; //!< Source thread ID
17         Uint    Length; //!< Length of message data in bytes
18         Uint8   Data[]; //!< Message data
19 } tMsg;
20
21 /**
22  * \brief Core threading structure
23  * 
24  */
25 typedef struct sThread
26 {
27         // --- threads.c's
28         /**
29          * \brief Next thread in current list
30          * \note Required to be first for linked list hacks to work
31          */
32         struct sThread  *Next;
33         struct sThread  *GlobalNext;    //!< Next thread in global list
34         struct sThread  *GlobalPrev;    //!< Previous thread in global list
35         tShortSpinlock  IsLocked;       //!< Thread's spinlock
36         volatile int    Status;         //!< Thread Status
37         void    *WaitPointer;   //!< What (Mutex/Thread/other) is the thread waiting on
38          int    RetStatus;      //!< Return Status
39         
40         Uint    TID;    //!< Thread ID
41         Uint    TGID;   //!< Thread Group (Process)
42         struct sThread  *Parent;        //!< Parent Thread
43         Uint    UID, GID;       //!< User and Group
44         char    *ThreadName;    //!< Name of thread
45         
46         // --- arch/proc.c's responsibility
47         //! Kernel Stack Base
48         tVAddr  KernelStack;
49         
50         //! Memory Manager State
51         tMemoryState    MemState;
52         
53         //! State on task switch
54         tTaskState      SavedState;
55         
56         // --- threads.c's
57          int    CurFaultNum;    //!< Current fault number, 0: none
58         tVAddr  FaultHandler;   //!< Fault Handler
59         
60         tMsg * volatile Messages;       //!< Message Queue
61         tMsg    *LastMessage;   //!< Last Message (speeds up insertion)
62         
63          int    Quantum, Remaining;     //!< Quantum Size and remaining timesteps
64          int    Priority;       //!< Priority - 0: Realtime, higher means less time
65         
66         Uint    Config[NUM_CFG_ENTRIES];        //!< Per-process configuration
67         
68         volatile int    CurCPU;
69 } tThread;
70
71
72 enum {
73         THREAD_STAT_NULL,       // Invalid process
74         THREAD_STAT_ACTIVE,     // Running and schedulable process
75         THREAD_STAT_SLEEPING,   // Message Sleep
76         THREAD_STAT_MUTEXSLEEP, // Mutex Sleep
77         THREAD_STAT_SEMAPHORESLEEP,     // Semaphore Sleep
78         THREAD_STAT_WAITING,    // ??? (Waiting for a thread)
79         THREAD_STAT_PREINIT,    // Being created
80         THREAD_STAT_ZOMBIE,     // Died/Killed, but parent not informed
81         THREAD_STAT_DEAD,       // Awaiting burial (free)
82         THREAD_STAT_BURIED      // If it's still on the list here, something's wrong
83 };
84 static const char * const casTHREAD_STAT[] = {
85         "THREAD_STAT_NULL",
86         "THREAD_STAT_ACTIVE",
87         "THREAD_STAT_SLEEPING",
88         "THREAD_STAT_MUTEXSLEEP",
89         "THREAD_STAT_SEMAPHORESLEEP",
90         "THREAD_STAT_WAITING",
91         "THREAD_STAT_PREINIT",
92         "THREAD_STAT_ZOMBIE",
93         "THREAD_STAT_DEAD",
94         "THREAD_STAT_BURIED"
95 };
96
97 enum eFaultNumbers
98 {
99         FAULT_MISC,
100         FAULT_PAGE,
101         FAULT_ACCESS,
102         FAULT_DIV0,
103         FAULT_OPCODE,
104         FAULT_FLOAT
105 };
106
107 #define GETMSG_IGNORE   ((void*)-1)
108
109 // === GLOBALS ===
110 extern BOOL     gaThreads_NoTaskSwitch[MAX_CPUS];
111
112 // === FUNCTIONS ===
113 extern tThread  *Proc_GetCurThread(void);
114
115 extern tThread  *Threads_GetThread(Uint TID);
116 extern void     Threads_SetPriority(tThread *Thread, int Pri);
117 extern int      Threads_Wake(tThread *Thread);
118 extern void     Threads_Kill(tThread *Thread, int Status);
119 extern void     Threads_AddActive(tThread *Thread);
120 extern tThread  *Threads_RemActive(void);
121 extern tThread  *Threads_GetNextToRun(int CPU, tThread *Last);
122
123 extern void     Threads_SetFaultHandler(Uint Handler);
124
125 extern int      Threads_SetUID(Uint *Errno, tUID ID);
126 extern int      Threads_SetGID(Uint *Errno, tUID ID);
127 extern int      Threads_WaitTID(int TID, int *Status);
128
129 extern tThread  *Threads_CloneTCB(Uint *Err, Uint Flags);
130 extern tThread  *Threads_CloneThreadZero(void);
131
132 extern int      Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data);
133 extern int      Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer);
134
135 #endif

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