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

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