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