Commit 91befe1d authored by Alexandre Julliard's avatar Alexandre Julliard

Made process and thread ids small integers instead of pointers.

parent bb2210bc
...@@ -114,6 +114,79 @@ static const struct object_ops startup_info_ops = ...@@ -114,6 +114,79 @@ static const struct object_ops startup_info_ops =
}; };
struct ptid_entry
{
void *ptr; /* entry ptr */
unsigned int next; /* next free entry */
};
static struct ptid_entry *ptid_entries; /* array of ptid entries */
static unsigned int used_ptid_entries; /* number of entries in use */
static unsigned int alloc_ptid_entries; /* number of allocated entries */
static unsigned int next_free_ptid; /* next free entry */
static unsigned int last_free_ptid; /* last free entry */
#define PTID_OFFSET 8 /* offset for first ptid value */
/* allocate a new process or thread id */
unsigned int alloc_ptid( void *ptr )
{
struct ptid_entry *entry;
unsigned int id;
if (used_ptid_entries < alloc_ptid_entries)
{
id = used_ptid_entries + PTID_OFFSET;
entry = &ptid_entries[used_ptid_entries++];
}
else if (next_free_ptid)
{
id = next_free_ptid;
entry = &ptid_entries[id - PTID_OFFSET];
if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
}
else /* need to grow the array */
{
unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
if (!count) count = 64;
if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
{
set_error( STATUS_NO_MEMORY );
return 0;
}
ptid_entries = entry;
alloc_ptid_entries = count;
id = used_ptid_entries + PTID_OFFSET;
entry = &ptid_entries[used_ptid_entries++];
}
entry->ptr = ptr;
return id;
}
/* free a process or thread id */
void free_ptid( unsigned int id )
{
struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
entry->ptr = NULL;
entry->next = 0;
/* append to end of free list so that we don't reuse it too early */
if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
else next_free_ptid = id;
last_free_ptid = id;
}
/* retrieve the pointer corresponding to a process or thread id */
void *get_ptid_entry( unsigned int id )
{
if (id < PTID_OFFSET) return NULL;
if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
return ptid_entries[id - PTID_OFFSET].ptr;
}
/* set the state of the process startup info */ /* set the state of the process startup info */
static void set_process_startup_state( struct process *process, enum startup_state state ) static void set_process_startup_state( struct process *process, enum startup_state state )
{ {
...@@ -225,6 +298,8 @@ struct thread *create_process( int fd ) ...@@ -225,6 +298,8 @@ struct thread *create_process( int fd )
if ((process->next = first_process) != NULL) process->next->prev = process; if ((process->next = first_process) != NULL) process->next->prev = process;
first_process = process; first_process = process;
if (!(process->id = alloc_ptid( process ))) goto error;
/* create the main thread */ /* create the main thread */
if (pipe( request_pipe ) == -1) if (pipe( request_pipe ) == -1)
{ {
...@@ -339,6 +414,7 @@ static void process_destroy( struct object *obj ) ...@@ -339,6 +414,7 @@ static void process_destroy( struct object *obj )
if (process->atom_table) release_object( process->atom_table ); if (process->atom_table) release_object( process->atom_table );
if (process->exe.file) release_object( process->exe.file ); if (process->exe.file) release_object( process->exe.file );
if (process->exe.filename) free( process->exe.filename ); if (process->exe.filename) free( process->exe.filename );
if (process->id) free_ptid( process->id );
} }
/* dump a process on stdout for debugging purposes */ /* dump a process on stdout for debugging purposes */
...@@ -347,8 +423,8 @@ static void process_dump( struct object *obj, int verbose ) ...@@ -347,8 +423,8 @@ static void process_dump( struct object *obj, int verbose )
struct process *process = (struct process *)obj; struct process *process = (struct process *)obj;
assert( obj->ops == &process_ops ); assert( obj->ops == &process_ops );
fprintf( stderr, "Process next=%p prev=%p handles=%p\n", fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
process->next, process->prev, process->handles ); process->id, process->next, process->prev, process->handles );
} }
static int process_signaled( struct object *obj, struct thread *thread ) static int process_signaled( struct object *obj, struct thread *thread )
...@@ -400,11 +476,11 @@ static int startup_info_signaled( struct object *obj, struct thread *thread ) ...@@ -400,11 +476,11 @@ static int startup_info_signaled( struct object *obj, struct thread *thread )
/* get a process from an id (and increment the refcount) */ /* get a process from an id (and increment the refcount) */
struct process *get_process_from_id( process_id_t id ) struct process *get_process_from_id( process_id_t id )
{ {
struct process *p = first_process; struct object *obj = get_ptid_entry( id );
while (p && (get_process_id(p) != id)) p = p->next;
if (p) grab_object( p ); if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
else set_error( STATUS_INVALID_PARAMETER ); set_error( STATUS_INVALID_PARAMETER );
return p; return NULL;
} }
/* get a process from a handle (and increment the refcount) */ /* get a process from a handle (and increment the refcount) */
......
...@@ -56,6 +56,8 @@ struct process ...@@ -56,6 +56,8 @@ struct process
struct thread *thread_list; /* head of the thread list */ struct thread *thread_list; /* head of the thread list */
struct thread *debugger; /* thread debugging this process */ struct thread *debugger; /* thread debugging this process */
struct handle_table *handles; /* handle entries */ struct handle_table *handles; /* handle entries */
process_id_t id; /* id of the process */
process_id_t group_id; /* group id of the process */
int exit_code; /* process exit code */ int exit_code; /* process exit code */
int running_threads; /* number of threads running in this process */ int running_threads; /* number of threads running in this process */
struct timeval start_time; /* absolute time at process start */ struct timeval start_time; /* absolute time at process start */
...@@ -73,7 +75,6 @@ struct process ...@@ -73,7 +75,6 @@ struct process
struct process_dll exe; /* main exe file */ struct process_dll exe; /* main exe file */
void *ldt_copy; /* pointer to LDT copy in client addr space */ void *ldt_copy; /* pointer to LDT copy in client addr space */
void *ldt_flags; /* pointer to LDT flags in client addr space */ void *ldt_flags; /* pointer to LDT flags in client addr space */
process_id_t group_id; /* group ID of the process */
}; };
struct process_snapshot struct process_snapshot
...@@ -94,6 +95,9 @@ struct module_snapshot ...@@ -94,6 +95,9 @@ struct module_snapshot
/* process functions */ /* process functions */
extern unsigned int alloc_ptid( void *ptr );
extern void free_ptid( unsigned int id );
extern void *get_ptid_entry( unsigned int id );
extern struct thread *create_process( int fd ); extern struct thread *create_process( int fd );
extern struct process *get_process_from_id( process_id_t id ); extern struct process *get_process_from_id( process_id_t id );
extern struct process *get_process_from_handle( obj_handle_t handle, unsigned int access ); extern struct process *get_process_from_handle( obj_handle_t handle, unsigned int access );
...@@ -115,7 +119,8 @@ extern struct process_snapshot *process_snap( int *count ); ...@@ -115,7 +119,8 @@ extern struct process_snapshot *process_snap( int *count );
extern struct module_snapshot *module_snap( struct process *process, int *count ); extern struct module_snapshot *module_snap( struct process *process, int *count );
extern void enum_processes( int (*cb)(struct process*, void*), void *user); extern void enum_processes( int (*cb)(struct process*, void*), void *user);
inline static process_id_t get_process_id( struct process *process ) { return (process_id_t)process; } inline static process_id_t get_process_id( struct process *process ) { return process->id; }
inline static int is_process_init_done( struct process *process ) inline static int is_process_init_done( struct process *process )
{ {
return process->startup_state == STARTUP_DONE; return process->startup_state == STARTUP_DONE;
......
...@@ -75,7 +75,7 @@ static int handle_child_status( struct thread *thread, int pid, int status ) ...@@ -75,7 +75,7 @@ static int handle_child_status( struct thread *thread, int pid, int status )
{ {
int sig = WSTOPSIG(status); int sig = WSTOPSIG(status);
if (debug_level && thread) if (debug_level && thread)
fprintf( stderr, "%08x: *signal* signal=%d\n", (unsigned int)thread, sig ); fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
switch(sig) switch(sig)
{ {
case SIGSTOP: /* continue at once if not suspended */ case SIGSTOP: /* continue at once if not suspended */
...@@ -97,11 +97,11 @@ static int handle_child_status( struct thread *thread, int pid, int status ) ...@@ -97,11 +97,11 @@ static int handle_child_status( struct thread *thread, int pid, int status )
if (debug_level) if (debug_level)
{ {
if (WIFSIGNALED(status)) if (WIFSIGNALED(status))
fprintf( stderr, "%08x: *exited* signal=%d\n", fprintf( stderr, "%04x: *exited* signal=%d\n",
(unsigned int)thread, WTERMSIG(status) ); thread->id, WTERMSIG(status) );
else else
fprintf( stderr, "%08x: *exited* status=%d\n", fprintf( stderr, "%04x: *exited* status=%d\n",
(unsigned int)thread, WEXITSTATUS(status) ); thread->id, WEXITSTATUS(status) );
} }
} }
return 0; return 0;
...@@ -146,7 +146,7 @@ static int attach_thread( struct thread *thread ) ...@@ -146,7 +146,7 @@ static int attach_thread( struct thread *thread )
if (errno == ESRCH) thread->unix_pid = 0; /* process got killed */ if (errno == ESRCH) thread->unix_pid = 0; /* process got killed */
return 0; return 0;
} }
if (debug_level) fprintf( stderr, "%08x: *attached*\n", (unsigned int)thread ); if (debug_level) fprintf( stderr, "%04x: *attached*\n", thread->id );
thread->attached = 1; thread->attached = 1;
wait4_thread( thread, SIGSTOP ); wait4_thread( thread, SIGSTOP );
return 1; return 1;
...@@ -161,7 +161,7 @@ void detach_thread( struct thread *thread, int sig ) ...@@ -161,7 +161,7 @@ void detach_thread( struct thread *thread, int sig )
/* make sure it is stopped */ /* make sure it is stopped */
suspend_thread( thread, 0 ); suspend_thread( thread, 0 );
if (sig) kill( thread->unix_pid, sig ); if (sig) kill( thread->unix_pid, sig );
if (debug_level) fprintf( stderr, "%08x: *detached*\n", (unsigned int)thread ); if (debug_level) fprintf( stderr, "%04x: *detached*\n", thread->id );
ptrace( PTRACE_DETACH, thread->unix_pid, (caddr_t)1, sig ); ptrace( PTRACE_DETACH, thread->unix_pid, (caddr_t)1, sig );
thread->suspend = 0; /* detach makes it continue */ thread->suspend = 0; /* detach makes it continue */
thread->attached = 0; thread->attached = 0;
......
...@@ -351,15 +351,15 @@ int receive_fd( struct process *process ) ...@@ -351,15 +351,15 @@ int receive_fd( struct process *process )
if (!thread || thread->process != process || thread->state == TERMINATED) if (!thread || thread->process != process || thread->state == TERMINATED)
{ {
if (debug_level) if (debug_level)
fprintf( stderr, "%08x: *fd* %d <- %d bad thread id\n", fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
(unsigned int)data.tid, data.fd, fd ); data.tid, data.fd, fd );
close( fd ); close( fd );
} }
else else
{ {
if (debug_level) if (debug_level)
fprintf( stderr, "%08x: *fd* %d <- %d\n", fprintf( stderr, "%04x: *fd* %d <- %d\n",
(unsigned int)thread, data.fd, fd ); thread->id, data.fd, fd );
thread_add_inflight_fd( thread, data.fd, fd ); thread_add_inflight_fd( thread, data.fd, fd );
} }
if (thread) release_object( thread ); if (thread) release_object( thread );
...@@ -391,7 +391,8 @@ int send_client_fd( struct process *process, int fd, obj_handle_t handle ) ...@@ -391,7 +391,8 @@ int send_client_fd( struct process *process, int fd, obj_handle_t handle )
int ret; int ret;
if (debug_level) if (debug_level)
fprintf( stderr, "%08x: *fd* %p -> %d\n", (unsigned int)current, handle, fd ); fprintf( stderr, "%04x: *fd* %p -> %d\n",
current ? current->id : process->id, handle, fd );
#ifdef HAVE_MSGHDR_ACCRIGHTS #ifdef HAVE_MSGHDR_ACCRIGHTS
msghdr.msg_accrightslen = sizeof(fd); msghdr.msg_accrightslen = sizeof(fd);
......
...@@ -167,6 +167,12 @@ struct thread *create_thread( int fd, struct process *process ) ...@@ -167,6 +167,12 @@ struct thread *create_thread( int fd, struct process *process )
if ((thread->next = first_thread) != NULL) thread->next->prev = thread; if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
first_thread = thread; first_thread = thread;
if (!(thread->id = alloc_ptid( thread )))
{
release_object( thread );
return NULL;
}
set_select_events( &thread->obj, POLLIN ); /* start listening to events */ set_select_events( &thread->obj, POLLIN ); /* start listening to events */
add_process_thread( thread->process, thread ); add_process_thread( thread->process, thread );
return thread; return thread;
...@@ -236,6 +242,7 @@ static void destroy_thread( struct object *obj ) ...@@ -236,6 +242,7 @@ static void destroy_thread( struct object *obj )
if (thread->info) release_object( thread->info ); if (thread->info) release_object( thread->info );
cleanup_thread( thread ); cleanup_thread( thread );
release_object( thread->process ); release_object( thread->process );
if (thread->id) free_ptid( thread->id );
} }
/* dump a thread on stdout for debugging purposes */ /* dump a thread on stdout for debugging purposes */
...@@ -244,8 +251,8 @@ static void dump_thread( struct object *obj, int verbose ) ...@@ -244,8 +251,8 @@ static void dump_thread( struct object *obj, int verbose )
struct thread *thread = (struct thread *)obj; struct thread *thread = (struct thread *)obj;
assert( obj->ops == &thread_ops ); assert( obj->ops == &thread_ops );
fprintf( stderr, "Thread pid=%d teb=%p state=%d\n", fprintf( stderr, "Thread id=%04x unix pid=%d teb=%p state=%d\n",
thread->unix_pid, thread->teb, thread->state ); thread->id, thread->unix_pid, thread->teb, thread->state );
} }
static int thread_signaled( struct object *obj, struct thread *thread ) static int thread_signaled( struct object *obj, struct thread *thread )
...@@ -257,11 +264,11 @@ static int thread_signaled( struct object *obj, struct thread *thread ) ...@@ -257,11 +264,11 @@ static int thread_signaled( struct object *obj, struct thread *thread )
/* get a thread pointer from a thread id (and increment the refcount) */ /* get a thread pointer from a thread id (and increment the refcount) */
struct thread *get_thread_from_id( thread_id_t id ) struct thread *get_thread_from_id( thread_id_t id )
{ {
struct thread *t = first_thread; struct object *obj = get_ptid_entry( id );
while (t && (get_thread_id(t) != id)) t = t->next;
if (t) grab_object( t ); if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
else set_error( STATUS_INVALID_PARAMETER ); set_error( STATUS_INVALID_PARAMETER );
return t; return NULL;
} }
/* get a thread from a handle (and increment the refcount) */ /* get a thread from a handle (and increment the refcount) */
...@@ -465,8 +472,8 @@ static int wake_thread( struct thread *thread ) ...@@ -465,8 +472,8 @@ static int wake_thread( struct thread *thread )
if ((signaled = check_wait( thread )) == -1) break; if ((signaled = check_wait( thread )) == -1) break;
cookie = thread->wait->cookie; cookie = thread->wait->cookie;
if (debug_level) fprintf( stderr, "%08x: *wakeup* signaled=%d cookie=%p\n", if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
(unsigned int)thread, signaled, cookie ); thread->id, signaled, cookie );
end_wait( thread ); end_wait( thread );
if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */ if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
break; break;
...@@ -484,8 +491,8 @@ static void thread_timeout( void *ptr ) ...@@ -484,8 +491,8 @@ static void thread_timeout( void *ptr )
wait->user = NULL; wait->user = NULL;
if (thread->wait != wait) return; /* not the top-level wait, ignore it */ if (thread->wait != wait) return; /* not the top-level wait, ignore it */
if (debug_level) fprintf( stderr, "%08x: *wakeup* signaled=%d cookie=%p\n", if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
(unsigned int)thread, STATUS_TIMEOUT, cookie ); thread->id, STATUS_TIMEOUT, cookie );
end_wait( thread ); end_wait( thread );
if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return; if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
/* check if other objects have become signaled in the meantime */ /* check if other objects have become signaled in the meantime */
...@@ -717,8 +724,8 @@ void kill_thread( struct thread *thread, int violent_death ) ...@@ -717,8 +724,8 @@ void kill_thread( struct thread *thread, int violent_death )
thread->exit_time = time(NULL); thread->exit_time = time(NULL);
if (current == thread) current = NULL; if (current == thread) current = NULL;
if (debug_level) if (debug_level)
fprintf( stderr,"%08x: *killed* exit_code=%d\n", fprintf( stderr,"%04x: *killed* exit_code=%d\n",
(unsigned int)thread, thread->exit_code ); thread->id, thread->exit_code );
if (thread->wait) if (thread->wait)
{ {
while (thread->wait) end_wait( thread ); while (thread->wait) end_wait( thread );
......
...@@ -63,6 +63,7 @@ struct thread ...@@ -63,6 +63,7 @@ struct thread
struct thread *proc_next; /* per-process thread list */ struct thread *proc_next; /* per-process thread list */
struct thread *proc_prev; struct thread *proc_prev;
struct process *process; struct process *process;
thread_id_t id; /* thread id */
struct mutex *mutex; /* list of currently owned mutexes */ struct mutex *mutex; /* list of currently owned mutexes */
struct debug_ctx *debug_ctx; /* debugger context if this thread is a debugger */ struct debug_ctx *debug_ctx; /* debugger context if this thread is a debugger */
struct debug_event *debug_event; /* debug event being sent to debugger */ struct debug_event *debug_event; /* debug event being sent to debugger */
...@@ -143,6 +144,6 @@ static inline unsigned int get_error(void) { return current ? current->err ...@@ -143,6 +144,6 @@ static inline unsigned int get_error(void) { return current ? current->err
static inline void set_error( unsigned int err ) { global_error = err; if (current) current->error = err; } static inline void set_error( unsigned int err ) { global_error = err; if (current) current->error = err; }
static inline void clear_error(void) { set_error(0); } static inline void clear_error(void) { set_error(0); }
static inline thread_id_t get_thread_id( struct thread *thread ) { return (thread_id_t)thread; } static inline thread_id_t get_thread_id( struct thread *thread ) { return thread->id; }
#endif /* __WINE_SERVER_THREAD_H */ #endif /* __WINE_SERVER_THREAD_H */
...@@ -34,8 +34,8 @@ ...@@ -34,8 +34,8 @@
"obj_handle_t" => "%p", "obj_handle_t" => "%p",
"atom_t" => "%04x", "atom_t" => "%04x",
"user_handle_t" => "%p", "user_handle_t" => "%p",
"process_id_t" => "%08x", "process_id_t" => "%04x",
"thread_id_t" => "%08x", "thread_id_t" => "%04x",
"rectangle_t" => "&dump_rectangle", "rectangle_t" => "&dump_rectangle",
"char_info_t" => "&dump_char_info", "char_info_t" => "&dump_char_info",
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment