thread.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Wine server threads
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. *
  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
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef __WINE_SERVER_THREAD_H
  21. #define __WINE_SERVER_THREAD_H
  22. #include "object.h"
  23. #include "ntstatus.h"
  24. /* thread structure */
  25. struct process;
  26. struct thread_wait;
  27. struct thread_apc;
  28. struct mutex;
  29. struct debug_ctx;
  30. struct debug_event;
  31. struct msg_queue;
  32. enum run_state
  33. {
  34. RUNNING, /* running normally */
  35. TERMINATED /* terminated */
  36. };
  37. struct apc_queue
  38. {
  39. struct thread_apc *head;
  40. struct thread_apc *tail;
  41. };
  42. /* descriptor for fds currently in flight from client to server */
  43. struct inflight_fd
  44. {
  45. int client; /* fd on the client side (or -1 if entry is free) */
  46. int server; /* fd on the server side */
  47. };
  48. #define MAX_INFLIGHT_FDS 16 /* max number of fds in flight per thread */
  49. struct thread
  50. {
  51. struct object obj; /* object header */
  52. struct thread *next; /* system-wide thread list */
  53. struct thread *prev;
  54. struct thread *proc_next; /* per-process thread list */
  55. struct thread *proc_prev;
  56. struct process *process;
  57. thread_id_t id; /* thread id */
  58. struct mutex *mutex; /* list of currently owned mutexes */
  59. struct debug_ctx *debug_ctx; /* debugger context if this thread is a debugger */
  60. struct debug_event *debug_event; /* debug event being sent to debugger */
  61. struct msg_queue *queue; /* message queue */
  62. struct thread_wait *wait; /* current wait condition if sleeping */
  63. struct apc_queue system_apc; /* queue of system async procedure calls */
  64. struct apc_queue user_apc; /* queue of user async procedure calls */
  65. struct inflight_fd inflight[MAX_INFLIGHT_FDS]; /* fds currently in flight */
  66. unsigned int error; /* current error code */
  67. union generic_request req; /* current request */
  68. void *req_data; /* variable-size data for request */
  69. unsigned int req_toread; /* amount of data still to read in request */
  70. void *reply_data; /* variable-size data for reply */
  71. unsigned int reply_size; /* size of reply data */
  72. unsigned int reply_towrite; /* amount of data still to write in reply */
  73. struct fd *request_fd; /* fd for receiving client requests */
  74. struct fd *reply_fd; /* fd to send a reply to a client */
  75. struct fd *wait_fd; /* fd to use to wake a sleeping client */
  76. enum run_state state; /* running state */
  77. int attached; /* is thread attached with ptrace? */
  78. int exit_code; /* thread exit code */
  79. int unix_pid; /* Unix pid of client */
  80. int unix_tid; /* Unix tid of client */
  81. CONTEXT *context; /* current context if in an exception handler */
  82. void *teb; /* TEB address (in client address space) */
  83. int priority; /* priority level */
  84. int affinity; /* affinity mask */
  85. int suspend; /* suspend count */
  86. time_t creation_time; /* Thread creation time */
  87. time_t exit_time; /* Thread exit time */
  88. struct token *token; /* security token associated with this thread */
  89. };
  90. struct thread_snapshot
  91. {
  92. struct thread *thread; /* thread ptr */
  93. int count; /* thread refcount */
  94. int priority; /* priority class */
  95. };
  96. extern struct thread *current;
  97. /* thread functions */
  98. extern struct thread *create_thread( int fd, struct process *process );
  99. extern struct thread *get_thread_from_id( thread_id_t id );
  100. extern struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access );
  101. extern struct thread *get_thread_from_pid( int pid );
  102. extern void stop_thread( struct thread *thread );
  103. extern int wake_thread( struct thread *thread );
  104. extern int add_queue( struct object *obj, struct wait_queue_entry *entry );
  105. extern void remove_queue( struct object *obj, struct wait_queue_entry *entry );
  106. extern void kill_thread( struct thread *thread, int violent_death );
  107. extern void wake_up( struct object *obj, int max );
  108. extern int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
  109. enum apc_type type, int system, void *arg1, void *arg2, void *arg3 );
  110. extern void thread_cancel_apc( struct thread *thread, struct object *owner, int system );
  111. extern int thread_add_inflight_fd( struct thread *thread, int client, int server );
  112. extern int thread_get_inflight_fd( struct thread *thread, int client );
  113. extern struct thread_snapshot *thread_snap( int *count );
  114. /* ptrace functions */
  115. extern void sigchld_callback(void);
  116. extern int get_ptrace_pid( struct thread *thread );
  117. extern void detach_thread( struct thread *thread, int sig );
  118. extern int attach_process( struct process *process );
  119. extern void detach_process( struct process *process );
  120. extern int suspend_for_ptrace( struct thread *thread );
  121. extern void resume_after_ptrace( struct thread *thread );
  122. extern int read_thread_int( struct thread *thread, const int *addr, int *data );
  123. extern int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask );
  124. extern void *get_thread_ip( struct thread *thread );
  125. extern int get_thread_single_step( struct thread *thread );
  126. extern int tkill( int pid, int sig );
  127. extern int send_thread_signal( struct thread *thread, int sig );
  128. extern unsigned int global_error; /* global error code for when no thread is current */
  129. static inline unsigned int get_error(void) { return current ? current->error : global_error; }
  130. static inline void set_error( unsigned int err ) { global_error = err; if (current) current->error = err; }
  131. static inline void clear_error(void) { set_error(0); }
  132. static inline void set_win32_error( unsigned int err ) { set_error( 0xc0010000 | err ); }
  133. static inline thread_id_t get_thread_id( struct thread *thread ) { return thread->id; }
  134. #endif /* __WINE_SERVER_THREAD_H */