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

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