Merge branch 'master' of git://localhost/acess2
[tpg/acess2.git] / KernelLand / Kernel / include / threads_int.h
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * include/threads_int.h
6  * - Internal Threading header
7  * - Only for use by stuff that needs access to the thread type.
8  */
9 #ifndef _THREADS_INT_H_
10 #define _THREADS_INT_H_
11
12 #include <threads.h>
13 #include <proc.h>
14 #include <timers_int.h>
15
16 typedef struct sProcess tProcess;
17
18 /**
19  * \brief IPC Message
20  */
21 typedef struct sMessage
22 {
23         struct sMessage *Next;  //!< Next message in thread's inbox
24         tTID    Source; //!< Source thread ID
25         Uint    Length; //!< Length of message data in bytes
26         Uint8   Data[]; //!< Message data
27 } tMsg;
28
29 /**
30  * \brief Process state
31  */
32 struct sProcess
33 {
34         struct sProcess *Next;
35         tPGID   PGID;
36         tPID    PID;
37
38          int    nThreads;
39         struct sThread  *FirstThread;
40         
41         tUID    UID;    //!< User ID
42         tGID    GID;    //!< User and Group
43         tMemoryState    MemState;
44
45          int    MaxFD;
46         char    *CurrentWorkingDir;
47         char    *RootDir;
48 };
49
50 /**
51  * \brief Core threading structure
52  * 
53  */
54 struct sThread
55 {
56         // --- threads.c's
57         /**
58          * \brief Next thread in current list
59          * \note Required to be first for linked list hacks to work
60          */
61         struct sThread  *Next;
62         struct sThread  *GlobalNext;    //!< Next thread in global list
63         struct sThread  *GlobalPrev;    //!< Previous thread in global list
64         struct sThread  *ProcessNext;
65         tShortSpinlock  IsLocked;       //!< Thread's spinlock
66         volatile int    Status;         //!< Thread Status
67         void    *WaitPointer;   //!< What (Mutex/Thread/other) is the thread waiting on
68          int    RetStatus;      //!< Return Status
69         
70         tTID    TID;    //!< Thread ID
71         struct sProcess *Process;       //!< Thread Group / Process
72         struct sThread  *Parent;        //!< Parent Thread
73         char    *ThreadName;    //!< Name of thread
74
75         struct sThread  *LastDeadChild; //!< Last child to die (will have the \a DeadChildren lock)
76         tMutex  DeadChildLock;  //!< Lock to prevent clobbering of \a LastDeadChild, acquired by child, released by parent
77
78         // --- arch/proc.c's responsibility
79         //! Kernel Stack Base
80         tVAddr  KernelStack;
81         
82         //! State on task switch
83         tTaskState      SavedState;
84         
85         // --- threads.c's
86          int    CurFaultNum;    //!< Current fault number, 0: none
87         tVAddr  FaultHandler;   //!< Fault Handler
88         
89         tMsg * volatile Messages;       //!< Message Queue
90         tMsg    *LastMessage;   //!< Last Message (speeds up insertion)
91         
92          int    Quantum, Remaining;     //!< Quantum Size and remaining timesteps
93          int    Priority;       //!< Priority - 0: Realtime, higher means less time
94         
95          int    _errno;
96         
97         volatile int    CurCPU;
98         
99         bool    bInstrTrace;
100         
101         // --- event.c
102         Uint32  EventState;
103         // --- timer.c
104         tTimer  ThreadTimer;
105 };
106
107
108 enum eThreadStatus {
109         THREAD_STAT_NULL,       // Invalid process
110         THREAD_STAT_ACTIVE,     // Running and schedulable process
111         THREAD_STAT_SLEEPING,   // Message Sleep
112         THREAD_STAT_MUTEXSLEEP, // Mutex Sleep
113         THREAD_STAT_RWLOCKSLEEP,        // Read-Writer lock Sleep
114         THREAD_STAT_SEMAPHORESLEEP,     // Semaphore Sleep
115         THREAD_STAT_QUEUESLEEP, // Queue
116         THREAD_STAT_EVENTSLEEP, // Event sleep
117         THREAD_STAT_WAITING,    // ??? (Waiting for a thread)
118         THREAD_STAT_PREINIT,    // Being created
119         THREAD_STAT_ZOMBIE,     // Died/Killed, but parent not informed
120         THREAD_STAT_DEAD,       // Awaiting burial (free)
121         THREAD_STAT_BURIED      // If it's still on the list here, something's wrong
122 };
123 static const char * const casTHREAD_STAT[] = {
124         "THREAD_STAT_NULL",
125         "THREAD_STAT_ACTIVE",
126         "THREAD_STAT_SLEEPING",
127         "THREAD_STAT_MUTEXSLEEP",
128         "THREAD_STAT_SEMAPHORESLEEP",
129         "THREAD_STAT_QUEUESLEEP",
130         "THREAD_STAT_EVENTSLEEP",
131         "THREAD_STAT_WAITING",
132         "THREAD_STAT_PREINIT",
133         "THREAD_STAT_ZOMBIE",
134         "THREAD_STAT_DEAD",
135         "THREAD_STAT_BURIED"
136 };
137
138 // === GLOBALS ===
139 extern BOOL     gaThreads_NoTaskSwitch[MAX_CPUS];
140 extern tShortSpinlock   glThreadListLock;
141
142 // === FUNCTIONS ===
143 extern tThread  *Threads_GetThread(Uint TID);
144 extern void     Threads_SetPriority(tThread *Thread, int Pri);
145 extern int      Threads_Wake(tThread *Thread);
146 extern void     Threads_Kill(tThread *Thread, int Status);
147 extern void     Threads_AddActive(tThread *Thread);
148 extern tThread  *Threads_RemActive(void);
149 extern void     Threads_Delete(tThread *Thread);
150 extern tThread  *Threads_GetNextToRun(int CPU, tThread *Last);
151
152 extern tThread  *Threads_CloneTCB(Uint Flags);
153 extern tThread  *Threads_CloneThreadZero(void);
154
155 #endif

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