blob: 1e95732d3a00aa47ad401643c6bddad6945398ee [file] [log] [blame]
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001/*
2 * Wine server threads
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard767e6f61998-08-09 12:47:43 +000019 */
20
21#ifndef __WINE_SERVER_THREAD_H
22#define __WINE_SERVER_THREAD_H
23
Alexandre Julliard43c190e1999-05-15 10:48:19 +000024#include "object.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000025
26/* thread structure */
27
28struct process;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000029struct thread_wait;
Alexandre Julliard62a8b431999-01-19 17:48:23 +000030struct thread_apc;
Alexandre Julliard57e11311999-05-16 16:59:38 +000031struct debug_ctx;
Alexandre Julliardf6507ed2000-04-06 22:05:16 +000032struct debug_event;
Alexandre Julliardc5e433a2000-05-30 19:48:18 +000033struct msg_queue;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000034
Alexandre Julliard0a707831999-11-08 05:31:47 +000035enum run_state
36{
37 RUNNING, /* running normally */
Alexandre Julliard0a707831999-11-08 05:31:47 +000038 TERMINATED /* terminated */
39};
40
Alexandre Julliardf5242402001-02-28 21:45:23 +000041/* descriptor for fds currently in flight from client to server */
42struct inflight_fd
43{
44 int client; /* fd on the client side (or -1 if entry is free) */
45 int server; /* fd on the server side */
46};
47#define MAX_INFLIGHT_FDS 16 /* max number of fds in flight per thread */
48
Alexandre Julliard767e6f61998-08-09 12:47:43 +000049struct thread
50{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000051 struct object obj; /* object header */
Alexandre Julliard73209eb2005-02-25 19:33:35 +000052 struct list entry; /* entry in system-wide thread list */
Alexandre Julliard05026382005-03-01 10:56:18 +000053 struct list proc_entry; /* entry in per-process thread list */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000054 struct process *process;
Alexandre Julliard91befe12003-02-01 01:38:40 +000055 thread_id_t id; /* thread id */
Alexandre Julliard20894e22005-02-25 16:58:43 +000056 struct list mutex_list; /* list of currently owned mutexes */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000057 struct debug_ctx *debug_ctx; /* debugger context if this thread is a debugger */
58 struct debug_event *debug_event; /* debug event being sent to debugger */
Alexandre Julliard17de8292006-04-19 19:45:39 +020059 int debug_break; /* debug breakpoint pending? */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000060 struct msg_queue *queue; /* message queue */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000061 struct thread_wait *wait; /* current wait condition if sleeping */
Alexandre Julliard81b6a1f2005-02-25 14:01:40 +000062 struct list system_apc; /* queue of system async procedure calls */
63 struct list user_apc; /* queue of user async procedure calls */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000064 struct inflight_fd inflight[MAX_INFLIGHT_FDS]; /* fds currently in flight */
65 unsigned int error; /* current error code */
66 union generic_request req; /* current request */
67 void *req_data; /* variable-size data for request */
68 unsigned int req_toread; /* amount of data still to read in request */
69 void *reply_data; /* variable-size data for reply */
70 unsigned int reply_size; /* size of reply data */
71 unsigned int reply_towrite; /* amount of data still to write in reply */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000072 struct fd *request_fd; /* fd for receiving client requests */
73 struct fd *reply_fd; /* fd to send a reply to a client */
74 struct fd *wait_fd; /* fd to use to wake a sleeping client */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000075 enum run_state state; /* running state */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000076 int exit_code; /* thread exit code */
77 int unix_pid; /* Unix pid of client */
Alexandre Julliarda8497bd2003-03-22 21:00:09 +000078 int unix_tid; /* Unix tid of client */
Alexandre Julliard5316dd02009-04-08 19:38:02 +020079 context_t *context; /* current context if in an exception handler */
80 context_t *suspend_context; /* current context if suspended */
Alexandre Julliardfa864382008-12-30 23:02:33 +010081 client_ptr_t teb; /* TEB address (in client address space) */
Alexandre Julliard913e7922009-01-19 14:15:51 +010082 affinity_t affinity; /* affinity mask */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000083 int priority; /* priority level */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000084 int suspend; /* suspend count */
Alexandre Julliard1bf96e02005-06-08 18:44:50 +000085 obj_handle_t desktop; /* desktop handle */
Alexandre Julliard92fec7b2005-06-28 19:37:52 +000086 int desktop_users; /* number of objects using the thread desktop */
Alexandre Julliardaaf477f2007-04-17 20:08:59 +020087 timeout_t creation_time; /* Thread creation time */
88 timeout_t exit_time; /* Thread exit time */
Mike McCormack36cd6f52003-07-24 00:07:00 +000089 struct token *token; /* security token associated with this thread */
Alexandre Julliard767e6f61998-08-09 12:47:43 +000090};
91
Alexandre Julliard07d84462000-04-16 19:45:05 +000092struct thread_snapshot
93{
94 struct thread *thread; /* thread ptr */
95 int count; /* thread refcount */
96 int priority; /* priority class */
97};
98
Alexandre Julliard767e6f61998-08-09 12:47:43 +000099extern struct thread *current;
100
101/* thread functions */
102
Alexandre Julliard5b4f3e82000-05-01 16:24:22 +0000103extern struct thread *create_thread( int fd, struct process *process );
Alexandre Julliard54f22872002-10-03 19:54:57 +0000104extern struct thread *get_thread_from_id( thread_id_t id );
Alexandre Julliard51885742002-05-30 20:12:58 +0000105extern struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access );
Alexandre Julliard48b74b32006-12-29 16:53:33 +0100106extern struct thread *get_thread_from_tid( int tid );
Alexandre Julliard578c1001999-11-14 21:23:21 +0000107extern struct thread *get_thread_from_pid( int pid );
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000108extern void stop_thread( struct thread *thread );
Peter Hunnisetta3c5ad42003-02-28 21:50:47 +0000109extern int wake_thread( struct thread *thread );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000110extern int add_queue( struct object *obj, struct wait_queue_entry *entry );
Alexandre Julliardc6e45ed1998-12-27 08:35:39 +0000111extern void remove_queue( struct object *obj, struct wait_queue_entry *entry );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000112extern void kill_thread( struct thread *thread, int violent_death );
Alexandre Julliard17de8292006-04-19 19:45:39 +0200113extern void break_thread( struct thread *thread );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000114extern void wake_up( struct object *obj, int max );
Alexandre Julliard5c8421d2007-01-04 13:40:09 +0100115extern int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data );
116extern void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000117extern int thread_add_inflight_fd( struct thread *thread, int client, int server );
118extern int thread_get_inflight_fd( struct thread *thread, int client );
Alexandre Julliard07d84462000-04-16 19:45:05 +0000119extern struct thread_snapshot *thread_snap( int *count );
Robert Shearmand2ea92d2005-04-22 21:17:15 +0000120extern struct token *thread_get_impersonation_token( struct thread *thread );
Alexandre Julliard2f3fa852010-02-02 15:58:02 +0100121extern int set_thread_affinity( struct thread *thread, affinity_t affinity );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000122
Alexandre Julliard578c1001999-11-14 21:23:21 +0000123/* ptrace functions */
124
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000125extern void sigchld_callback(void);
Alexandre Julliard5316dd02009-04-08 19:38:02 +0200126extern void get_thread_context( struct thread *thread, context_t *context, unsigned int flags );
127extern void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags );
Alexandre Julliard02a53c12003-02-25 04:17:22 +0000128extern int send_thread_signal( struct thread *thread, int sig );
Alexandre Julliardcb709312006-04-07 19:52:12 +0200129extern void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
130 unsigned int *limit, unsigned char *flags );
Alexandre Julliard578c1001999-11-14 21:23:21 +0000131
Alexandre Julliard908464d2000-11-01 03:11:12 +0000132extern unsigned int global_error; /* global error code for when no thread is current */
Alexandre Julliard578c1001999-11-14 21:23:21 +0000133
Alexandre Julliard908464d2000-11-01 03:11:12 +0000134static inline unsigned int get_error(void) { return current ? current->error : global_error; }
135static inline void set_error( unsigned int err ) { global_error = err; if (current) current->error = err; }
Alexandre Julliardd5b42322003-12-10 01:12:18 +0000136static inline void clear_error(void) { set_error(0); }
137static inline void set_win32_error( unsigned int err ) { set_error( 0xc0010000 | err ); }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000138
Alexandre Julliard91befe12003-02-01 01:38:40 +0000139static inline thread_id_t get_thread_id( struct thread *thread ) { return thread->id; }
Alexandre Julliardff81d782000-03-08 12:01:30 +0000140
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000141#endif /* __WINE_SERVER_THREAD_H */