thread.h 6.87 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Wine server threads
 *
 * Copyright (C) 1998 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20 21 22 23
 */

#ifndef __WINE_SERVER_THREAD_H
#define __WINE_SERVER_THREAD_H

24
#include "object.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
25 26 27 28

/* thread structure */

struct process;
Alexandre Julliard's avatar
Alexandre Julliard committed
29
struct thread_wait;
30
struct thread_apc;
31
struct debug_ctx;
32
struct debug_event;
33
struct msg_queue;
Alexandre Julliard's avatar
Alexandre Julliard committed
34

35 36 37 38 39 40
enum run_state
{
    RUNNING,    /* running normally */
    TERMINATED  /* terminated */
};

41 42 43 44 45 46 47 48
/* descriptor for fds currently in flight from client to server */
struct inflight_fd
{
    int client;  /* fd on the client side (or -1 if entry is free) */
    int server;  /* fd on the server side */
};
#define MAX_INFLIGHT_FDS 16  /* max number of fds in flight per thread */

Alexandre Julliard's avatar
Alexandre Julliard committed
49 50
struct thread
{
51
    struct object          obj;           /* object header */
52
    struct list            entry;         /* entry in system-wide thread list */
53
    struct list            proc_entry;    /* entry in per-process thread list */
54
    struct process        *process;
55
    thread_id_t            id;            /* thread id */
56
    struct list            mutex_list;    /* list of currently owned mutexes */
57 58
    struct debug_ctx      *debug_ctx;     /* debugger context if this thread is a debugger */
    struct debug_event    *debug_event;   /* debug event being sent to debugger */
59
    int                    debug_break;   /* debug breakpoint pending? */
60 61
    struct msg_queue      *queue;         /* message queue */
    struct thread_wait    *wait;          /* current wait condition if sleeping */
62 63
    struct list            system_apc;    /* queue of system async procedure calls */
    struct list            user_apc;      /* queue of user async procedure calls */
64 65 66 67 68 69 70 71
    struct inflight_fd     inflight[MAX_INFLIGHT_FDS];  /* fds currently in flight */
    unsigned int           error;         /* current error code */
    union generic_request  req;           /* current request */
    void                  *req_data;      /* variable-size data for request */
    unsigned int           req_toread;    /* amount of data still to read in request */
    void                  *reply_data;    /* variable-size data for reply */
    unsigned int           reply_size;    /* size of reply data */
    unsigned int           reply_towrite; /* amount of data still to write in reply */
72 73 74
    struct fd             *request_fd;    /* fd for receiving client requests */
    struct fd             *reply_fd;      /* fd to send a reply to a client */
    struct fd             *wait_fd;       /* fd to use to wake a sleeping client */
75 76 77
    enum run_state         state;         /* running state */
    int                    exit_code;     /* thread exit code */
    int                    unix_pid;      /* Unix pid of client */
78
    int                    unix_tid;      /* Unix tid of client */
79 80
    context_t             *context;       /* current context if in an exception handler */
    context_t             *suspend_context; /* current context if suspended */
81
    client_ptr_t           teb;           /* TEB address (in client address space) */
82
    affinity_t             affinity;      /* affinity mask */
83 84
    int                    priority;      /* priority level */
    int                    suspend;       /* suspend count */
85
    obj_handle_t           desktop;       /* desktop handle */
86
    int                    desktop_users; /* number of objects using the thread desktop */
87 88
    timeout_t              creation_time; /* Thread creation time */
    timeout_t              exit_time;     /* Thread exit time */
89
    struct token          *token;         /* security token associated with this thread */
Alexandre Julliard's avatar
Alexandre Julliard committed
90 91
};

92 93 94 95 96 97 98
struct thread_snapshot
{
    struct thread  *thread;    /* thread ptr */
    int             count;     /* thread refcount */
    int             priority;  /* priority class */
};

Alexandre Julliard's avatar
Alexandre Julliard committed
99 100 101 102
extern struct thread *current;

/* thread functions */

103
extern struct thread *create_thread( int fd, struct process *process );
104
extern struct thread *get_thread_from_id( thread_id_t id );
105
extern struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access );
106
extern struct thread *get_thread_from_tid( int tid );
107
extern struct thread *get_thread_from_pid( int pid );
108
extern void stop_thread( struct thread *thread );
109
extern void stop_thread_if_suspended( struct thread *thread );
110
extern int wake_thread( struct thread *thread );
111
extern int add_queue( struct object *obj, struct wait_queue_entry *entry );
112
extern void remove_queue( struct object *obj, struct wait_queue_entry *entry );
113
extern void kill_thread( struct thread *thread, int violent_death );
114
extern void break_thread( struct thread *thread );
Alexandre Julliard's avatar
Alexandre Julliard committed
115
extern void wake_up( struct object *obj, int max );
116 117
extern int thread_queue_apc( struct thread *thread, struct object *owner, const apc_call_t *call_data );
extern void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type );
118 119
extern int thread_add_inflight_fd( struct thread *thread, int client, int server );
extern int thread_get_inflight_fd( struct thread *thread, int client );
120
extern struct thread_snapshot *thread_snap( int *count );
121
extern struct token *thread_get_impersonation_token( struct thread *thread );
122
extern int set_thread_affinity( struct thread *thread, affinity_t affinity );
Alexandre Julliard's avatar
Alexandre Julliard committed
123

124 125
/* ptrace functions */

126
extern void sigchld_callback(void);
127 128
extern void get_thread_context( struct thread *thread, context_t *context, unsigned int flags );
extern void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags );
129
extern int send_thread_signal( struct thread *thread, int sig );
130 131
extern void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
                                unsigned int *limit, unsigned char *flags );
132

133
extern unsigned int global_error;  /* global error code for when no thread is current */
134

135 136
static inline unsigned int get_error(void)       { return current ? current->error : global_error; }
static inline void set_error( unsigned int err ) { global_error = err; if (current) current->error = err; }
137 138
static inline void clear_error(void)             { set_error(0); }
static inline void set_win32_error( unsigned int err ) { set_error( 0xc0010000 | err ); }
Alexandre Julliard's avatar
Alexandre Julliard committed
139

140
static inline thread_id_t get_thread_id( struct thread *thread ) { return thread->id; }
141

Alexandre Julliard's avatar
Alexandre Julliard committed
142
#endif  /* __WINE_SERVER_THREAD_H */