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 =
};
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 */
static void set_process_startup_state( struct process *process, enum startup_state state )
{
......@@ -225,6 +298,8 @@ struct thread *create_process( int fd )
if ((process->next = first_process) != NULL) process->next->prev = process;
first_process = process;
if (!(process->id = alloc_ptid( process ))) goto error;
/* create the main thread */
if (pipe( request_pipe ) == -1)
{
......@@ -339,6 +414,7 @@ static void process_destroy( struct object *obj )
if (process->atom_table) release_object( process->atom_table );
if (process->exe.file) release_object( process->exe.file );
if (process->exe.filename) free( process->exe.filename );
if (process->id) free_ptid( process->id );
}
/* dump a process on stdout for debugging purposes */
......@@ -347,8 +423,8 @@ static void process_dump( struct object *obj, int verbose )
struct process *process = (struct process *)obj;
assert( obj->ops == &process_ops );
fprintf( stderr, "Process next=%p prev=%p handles=%p\n",
process->next, process->prev, process->handles );
fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
process->id, process->next, process->prev, process->handles );
}
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 )
/* get a process from an id (and increment the refcount) */
struct process *get_process_from_id( process_id_t id )
{
struct process *p = first_process;
while (p && (get_process_id(p) != id)) p = p->next;
if (p) grab_object( p );
else set_error( STATUS_INVALID_PARAMETER );
return p;
struct object *obj = get_ptid_entry( id );
if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
set_error( STATUS_INVALID_PARAMETER );
return NULL;
}
/* get a process from a handle (and increment the refcount) */
......
......@@ -56,6 +56,8 @@ struct process
struct thread *thread_list; /* head of the thread list */
struct thread *debugger; /* thread debugging this process */
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 running_threads; /* number of threads running in this process */
struct timeval start_time; /* absolute time at process start */
......@@ -73,7 +75,6 @@ struct process
struct process_dll exe; /* main exe file */
void *ldt_copy; /* pointer to LDT copy 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
......@@ -94,6 +95,9 @@ struct module_snapshot
/* 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 process *get_process_from_id( process_id_t id );
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 );
extern struct module_snapshot *module_snap( struct process *process, int *count );
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 )
{
return process->startup_state == STARTUP_DONE;
......
......@@ -75,7 +75,7 @@ static int handle_child_status( struct thread *thread, int pid, int status )
{
int sig = WSTOPSIG(status);
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)
{
case SIGSTOP: /* continue at once if not suspended */
......@@ -97,11 +97,11 @@ static int handle_child_status( struct thread *thread, int pid, int status )
if (debug_level)
{
if (WIFSIGNALED(status))
fprintf( stderr, "%08x: *exited* signal=%d\n",
(unsigned int)thread, WTERMSIG(status) );
fprintf( stderr, "%04x: *exited* signal=%d\n",
thread->id, WTERMSIG(status) );
else
fprintf( stderr, "%08x: *exited* status=%d\n",
(unsigned int)thread, WEXITSTATUS(status) );
fprintf( stderr, "%04x: *exited* status=%d\n",
thread->id, WEXITSTATUS(status) );
}
}
return 0;
......@@ -146,7 +146,7 @@ static int attach_thread( struct thread *thread )
if (errno == ESRCH) thread->unix_pid = 0; /* process got killed */
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;
wait4_thread( thread, SIGSTOP );
return 1;
......@@ -161,7 +161,7 @@ void detach_thread( struct thread *thread, int sig )
/* make sure it is stopped */
suspend_thread( thread, 0 );
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 );
thread->suspend = 0; /* detach makes it continue */
thread->attached = 0;
......
......@@ -351,15 +351,15 @@ int receive_fd( struct process *process )
if (!thread || thread->process != process || thread->state == TERMINATED)
{
if (debug_level)
fprintf( stderr, "%08x: *fd* %d <- %d bad thread id\n",
(unsigned int)data.tid, data.fd, fd );
fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
data.tid, data.fd, fd );
close( fd );
}
else
{
if (debug_level)
fprintf( stderr, "%08x: *fd* %d <- %d\n",
(unsigned int)thread, data.fd, fd );
fprintf( stderr, "%04x: *fd* %d <- %d\n",
thread->id, data.fd, fd );
thread_add_inflight_fd( thread, data.fd, fd );
}
if (thread) release_object( thread );
......@@ -391,7 +391,8 @@ int send_client_fd( struct process *process, int fd, obj_handle_t handle )
int ret;
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
msghdr.msg_accrightslen = sizeof(fd);
......
......@@ -167,6 +167,12 @@ struct thread *create_thread( int fd, struct process *process )
if ((thread->next = first_thread) != NULL) thread->next->prev = 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 */
add_process_thread( thread->process, thread );
return thread;
......@@ -236,6 +242,7 @@ static void destroy_thread( struct object *obj )
if (thread->info) release_object( thread->info );
cleanup_thread( thread );
release_object( thread->process );
if (thread->id) free_ptid( thread->id );
}
/* dump a thread on stdout for debugging purposes */
......@@ -244,8 +251,8 @@ static void dump_thread( struct object *obj, int verbose )
struct thread *thread = (struct thread *)obj;
assert( obj->ops == &thread_ops );
fprintf( stderr, "Thread pid=%d teb=%p state=%d\n",
thread->unix_pid, thread->teb, thread->state );
fprintf( stderr, "Thread id=%04x unix pid=%d teb=%p state=%d\n",
thread->id, thread->unix_pid, thread->teb, thread->state );
}
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) */
struct thread *get_thread_from_id( thread_id_t id )
{
struct thread *t = first_thread;
while (t && (get_thread_id(t) != id)) t = t->next;
if (t) grab_object( t );
else set_error( STATUS_INVALID_PARAMETER );
return t;
struct object *obj = get_ptid_entry( id );
if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
set_error( STATUS_INVALID_PARAMETER );
return NULL;
}
/* get a thread from a handle (and increment the refcount) */
......@@ -465,8 +472,8 @@ static int wake_thread( struct thread *thread )
if ((signaled = check_wait( thread )) == -1) break;
cookie = thread->wait->cookie;
if (debug_level) fprintf( stderr, "%08x: *wakeup* signaled=%d cookie=%p\n",
(unsigned int)thread, signaled, cookie );
if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
thread->id, signaled, cookie );
end_wait( thread );
if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
break;
......@@ -484,8 +491,8 @@ static void thread_timeout( void *ptr )
wait->user = NULL;
if (thread->wait != wait) return; /* not the top-level wait, ignore it */
if (debug_level) fprintf( stderr, "%08x: *wakeup* signaled=%d cookie=%p\n",
(unsigned int)thread, STATUS_TIMEOUT, cookie );
if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
thread->id, STATUS_TIMEOUT, cookie );
end_wait( thread );
if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
/* check if other objects have become signaled in the meantime */
......@@ -717,8 +724,8 @@ void kill_thread( struct thread *thread, int violent_death )
thread->exit_time = time(NULL);
if (current == thread) current = NULL;
if (debug_level)
fprintf( stderr,"%08x: *killed* exit_code=%d\n",
(unsigned int)thread, thread->exit_code );
fprintf( stderr,"%04x: *killed* exit_code=%d\n",
thread->id, thread->exit_code );
if (thread->wait)
{
while (thread->wait) end_wait( thread );
......
......@@ -63,6 +63,7 @@ struct thread
struct thread *proc_next; /* per-process thread list */
struct thread *proc_prev;
struct process *process;
thread_id_t id; /* thread id */
struct mutex *mutex; /* list of currently owned mutexes */
struct debug_ctx *debug_ctx; /* debugger context if this thread is a 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
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 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 */
......@@ -34,8 +34,8 @@
"obj_handle_t" => "%p",
"atom_t" => "%04x",
"user_handle_t" => "%p",
"process_id_t" => "%08x",
"thread_id_t" => "%08x",
"process_id_t" => "%04x",
"thread_id_t" => "%04x",
"rectangle_t" => "&dump_rectangle",
"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