NativeLib - Including kernelland locks
[tpg/acess2.git] / Tools / nativelib / include / threads_int.h
1 /*
2  * Acess2 libnative (Kernel Simulation Library)
3  * - By John Hodge (thePowersGang)
4  *
5  * threads_int.h
6  * - Threads handling definitions
7  */
8 #ifndef _THREADS_INT_H_
9 #define _THREADS_INT_H_
10
11 #include <shortlock.h>
12
13 typedef struct sThread  tThread;
14
15 #define THREAD_EVENT_RWLOCK     (1 << 8)
16
17 typedef struct sThreadIntMutex  tThreadIntMutex;        // actually pthreads
18 typedef struct sThreadIntSem    tThreadIntSem;
19
20 struct sProcess
21 {
22         struct sProcess *Next;
23         struct sThread  *Threads;
24
25          int    PID;
26          int    UID, GID;
27         
28         char    *CWD;
29         char    *Chroot;
30
31          int    MaxFDs;
32         void    *Handles;
33 };
34
35 struct sThread
36 {
37         struct sThread  *Next;
38         struct sThread  *ListNext;
39
40         struct sProcess *Process;
41         struct sThread  *ProcNext;
42
43         void    *ThreadHandle;
44          int    TID;
45
46          int    Status;
47
48         tShortSpinlock  IsLocked;
49
50         uint32_t        PendingEvents;
51         uint32_t        WaitingEvents;
52         tThreadIntSem   *WaitSemaphore; // pthreads
53
54         char    *ThreadName;
55
56          int    bInstrTrace;    // Used for semaphore tracing
57
58          int    RetStatus;
59         void    *WaitPointer;
60
61         // Init Only
62         void    (*SpawnFcn)(void*);
63         void    *SpawnData;
64 };
65
66 enum eThreadStatus {
67         THREAD_STAT_NULL,       // Invalid process
68         THREAD_STAT_ACTIVE,     // Running and schedulable process
69         THREAD_STAT_SLEEPING,   // Message Sleep
70         THREAD_STAT_MUTEXSLEEP, // Mutex Sleep
71         THREAD_STAT_RWLOCKSLEEP,        // Read-Writer lock Sleep
72         THREAD_STAT_SEMAPHORESLEEP,     // Semaphore Sleep
73         THREAD_STAT_QUEUESLEEP, // Queue
74         THREAD_STAT_EVENTSLEEP, // Event sleep
75         THREAD_STAT_WAITING,    // ??? (Waiting for a thread)
76         THREAD_STAT_PREINIT,    // Being created
77         THREAD_STAT_ZOMBIE,     // Died/Killed, but parent not informed
78         THREAD_STAT_DEAD,       // Awaiting burial (free)
79         THREAD_STAT_BURIED      // If it's still on the list here, something's wrong
80 };
81 static const char * const casTHREAD_STAT[] = {
82         "THREAD_STAT_NULL",
83         "THREAD_STAT_ACTIVE",
84         "THREAD_STAT_SLEEPING",
85         "THREAD_STAT_MUTEXSLEEP",
86         "THREAD_STAT_RWLOCKSLEEP",
87         "THREAD_STAT_SEMAPHORESLEEP",
88         "THREAD_STAT_QUEUESLEEP",
89         "THREAD_STAT_EVENTSLEEP",
90         "THREAD_STAT_WAITING",
91         "THREAD_STAT_PREINIT",
92         "THREAD_STAT_ZOMBIE",
93         "THREAD_STAT_DEAD",
94         "THREAD_STAT_BURIED"
95 };
96
97 extern struct sThread __thread  *lpThreads_This;
98 extern tShortSpinlock   glThreadListLock;
99
100 extern int      Threads_int_CreateThread(struct sThread *Thread);
101 extern int      Threads_int_ThreadingEnabled(void);
102
103 extern tThread  *Proc_GetCurThread(void);
104 extern tThread  *Threads_RemActive(void);
105 extern void     Threads_AddActive(tThread *Thread);
106 extern void     Threads_int_WaitForStatusEnd(enum eThreadStatus Status);
107
108 extern tThreadIntMutex  *Threads_int_MutexCreate(void);
109 extern void     Threads_int_MutexDestroy(tThreadIntMutex *Mutex);
110 extern void     Threads_int_MutexLock(tThreadIntMutex *Mutex);
111 extern void     Threads_int_MutexRelease(tThreadIntMutex *Mutex);
112
113 extern tThreadIntSem    *Threads_int_SemCreate(void);
114 extern void     Threads_int_SemDestroy(tThreadIntSem *Sem);
115 extern void     Threads_int_SemSignal(tThreadIntSem *Sem);
116 extern void     Threads_int_SemWait(tThreadIntSem *Sem);
117 extern void     Threads_int_SemWaitAll(tThreadIntSem *Sem);
118
119 #endif
120

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