Removed client-side wait functions; all waiting is now done through
[wine] / include / thread.h
1 /*
2  * Thread definitions
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #ifndef __WINE_THREAD_H
8 #define __WINE_THREAD_H
9
10 #include "config.h"
11 #include "k32obj.h"
12 #include "windows.h"
13 #include "winbase.h"
14 #include "winnt.h"
15 #include "selectors.h"  /* for SET_FS */
16
17 #ifdef linux
18 #define HAVE_CLONE_SYSCALL
19 #endif
20
21 /* This is what we will use on *BSD, once threads using rfork() get
22  * implemented:
23  *
24  * #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
25  * #define HAVE_RFORK
26  * #endif
27  */
28
29 #if (defined(HAVE_CLONE_SYSCALL) || defined(HAVE_RFORK)) && !defined(NO_REENTRANT_LIBC)
30 #define USE_THREADS
31 #endif
32
33 struct _PDB32;
34
35 /* Thread exception block */
36 typedef struct _TEB
37 {
38     void        *except;         /* 00 Head of exception handling chain */
39     void        *stack_top;      /* 04 Top of thread stack */
40     void        *stack_low;      /* 08 Stack low-water mark */
41     HTASK16      htask16;        /* 0c Win16 task handle */
42     WORD         stack_sel;      /* 0e 16-bit stack selector */
43     DWORD        selman_list;    /* 10 Selector manager list */
44     DWORD        user_ptr;       /* 14 User pointer */
45     struct _TEB *self;           /* 18 Pointer to this structure */
46     WORD         flags;          /* 1c Flags */
47     WORD         mutex_count;    /* 1e Win16 mutex count */
48     DWORD        debug_context;  /* 20 Debug context */
49     DWORD       *ppriority;      /* 24 Pointer to current priority */
50     HQUEUE16     queue;          /* 28 Message queue */
51     WORD         pad1;           /* 2a */
52     LPVOID      *tls_ptr;        /* 2c Pointer to TLS array */
53     struct _PDB32 *process;      /* 30 owning process (used by NT3.51 applets)*/
54 } TEB;
55
56 /* Thread exception flags */
57 #define TEBF_WIN32  0x0001
58 #define TEBF_TRAP   0x0002
59
60 /* Event waiting structure */
61 typedef struct
62 {
63     DWORD         count;     /* Count of valid objects */
64     DWORD         signaled;  /* Index of signaled object (or WAIT_FAILED)*/
65     BOOL32        wait_all;  /* Wait for all objects flag */
66     BOOL32        wait_msg;  /* Wait for message flag */
67     K32OBJ       *objs[MAXIMUM_WAIT_OBJECTS];  /* Object pointers */
68     int           server[MAXIMUM_WAIT_OBJECTS];  /* Server handles */
69 } WAIT_STRUCT;
70
71 /* Thread database */
72 typedef struct _THDB
73 {
74     K32OBJ         header;         /*  00 Kernel object header */
75     struct _PDB32 *process;        /*  08 Process owning this thread */
76     K32OBJ        *event;          /*  0c Thread event */
77     TEB            teb;            /*  10 Thread exception block */
78     DWORD          flags;          /*  44 Flags */
79     DWORD          exit_code;      /*  48 Termination status */
80     WORD           teb_sel;        /*  4c Selector to TEB */
81     WORD           emu_sel;        /*  4e 80387 emulator selector */
82     int            thread_errno;   /*  50 Per-thread errno (was: unknown) */
83     WAIT_STRUCT   *wait_list;      /*  54 Event waiting list */
84     int            thread_h_errno; /*  50 Per-thread h_errno (was: unknown) */
85     void          *ring0_thread;   /*  5c Pointer to ring 0 thread */
86     void          *ptdbx;          /*  60 Pointer to TDBX structure */
87     void          *stack_base;     /*  64 Base of the stack */
88     void          *exit_stack;     /*  68 Stack pointer on thread exit */
89     void          *emu_data;       /*  6c Related to 80387 emulation */
90     DWORD          last_error;     /*  70 Last error code */
91     void          *debugger_CB;    /*  74 Debugger context block */
92     DWORD          debug_thread;   /*  78 Thread debugging this one (?) */
93     void          *pcontext;       /*  7c Thread register context */
94     DWORD          cur_stack;      /*  80 Current stack (was: unknown) */
95     DWORD          unknown3[2];    /*  84 Unknown */
96     WORD           current_ss;     /*  8c Another 16-bit stack selector */
97     WORD           pad2;           /*  8e */
98     void          *ss_table;       /*  90 Pointer to info about 16-bit stack */
99     WORD           thunk_ss;       /*  94 Yet another 16-bit stack selector */
100     WORD           pad3;           /*  96 */
101     LPVOID         tls_array[64];  /*  98 Thread local storage */
102     DWORD          delta_priority; /* 198 Priority delta */
103     DWORD          unknown4[7];    /* 19c Unknown */
104     void          *create_data;    /* 1b8 Pointer to creation structure */
105     DWORD          suspend_count;  /* 1bc SuspendThread() counter */
106     void          *entry_point;    /* 1c0 Thread entry point (was: unknown) */
107     void          *entry_arg;      /* 1c4 Entry point arg (was: unknown) */
108     int            unix_pid;       /* 1c8 Unix thread pid (was: unknown) */
109     DWORD          unknown5[3];    /* 1cc Unknown */
110     DWORD          sys_count[4];   /* 1d8 Syslevel mutex entry counters */
111     CRITICAL_SECTION *sys_mutex[4];/* 1e8 Syslevel mutex pointers */
112     DWORD          unknown6[2];    /* 1f8 Unknown */
113     /* The following are Wine-specific fields */
114     CONTEXT        context;        /* 200 Thread context */
115     WAIT_STRUCT    wait_struct;    /*     Event wait structure */
116     int            socket;         /*     Socket for server communication */
117     unsigned int   seq;            /*     Server sequence number */
118     void          *server_tid;     /*     Server id for this thread */
119 } THDB;
120
121 /* Thread queue entry */
122 typedef struct _THREAD_ENTRY
123 {
124     THDB                 *thread;
125     struct _THREAD_ENTRY *next;
126 } THREAD_ENTRY;
127
128 /* A thread queue is a circular list; a THREAD_QUEUE is a pointer */
129 /* to the end of the queue (i.e. where we add elements) */
130 typedef THREAD_ENTRY *THREAD_QUEUE;
131
132 /* THDB <-> Thread id conversion macros */
133 #define THREAD_OBFUSCATOR       ((DWORD)0xdeadbeef)
134 #define THREAD_ID_TO_THDB(id)   ((THDB *)((id) ^ THREAD_OBFUSCATOR))
135 #define THDB_TO_THREAD_ID(thdb) ((DWORD)(thdb) ^ THREAD_OBFUSCATOR)
136
137 /* The pseudo handle value returned by GetCurrentThread */
138 #define CURRENT_THREAD_PSEUDOHANDLE 0xfffffffe
139
140 #ifdef __i386__
141 /* On the i386, the current thread is in the %fs register */
142 # define SET_CUR_THREAD(thdb) SET_FS((thdb)->teb_sel)
143 #else
144 extern THDB *pCurrentThread;
145 # define SET_CUR_THREAD(thdb) (pCurrentThread = (thdb))
146 #endif  /* __i386__ */
147
148
149 /* scheduler/thread.c */
150 extern THDB *THREAD_Create( struct _PDB32 *pdb, DWORD stack_size,
151                             BOOL32 alloc_stack16,
152                             int *server_thandle, int *server_phandle,
153                             LPTHREAD_START_ROUTINE start_addr, LPVOID param );
154 extern THDB *THREAD_Current(void);
155 extern BOOL32 THREAD_IsWin16( THDB *thdb );
156 extern THDB *THREAD_IdToTHDB( DWORD id );
157 extern void THREAD_Start( THDB *thdb );
158 extern THDB *THREAD_GetPtr( HANDLE32 handle, DWORD access, int *server_handle );
159 extern void THREAD_AddQueue( THREAD_QUEUE *queue, THDB *thread );
160 extern void THREAD_RemoveQueue( THREAD_QUEUE *queue, THDB *thread );
161 extern DWORD THREAD_TlsAlloc( THDB *thread );
162
163 /* scheduler/synchro.c */
164 extern DWORD SYNC_DoWait( DWORD count, const HANDLE32 *handles,
165                           BOOL32 wait_all, DWORD timeout,
166                           BOOL32 alertable, BOOL32 wait_msg );
167
168
169 /* scheduler/sysdeps.c */
170 extern int SYSDEPS_SpawnThread( THDB *thread );
171 extern void SYSDEPS_ExitThread(void);
172 extern TEB * WINAPI NtCurrentTeb(void);
173
174 #endif  /* __WINE_THREAD_H */