diff options
author | Richard Braun <rbraun@sceen.net> | 2012-11-25 23:12:41 +0100 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2012-11-27 23:03:06 +0100 |
commit | 3aef6957c38c5f6fcdcadc878ae16fb690485f95 (patch) | |
tree | 4f739c11a9bec9ad1f2e9c8436fac7ee3e6dcf60 /libddekit | |
parent | e1b0e34210b385d05e50900486262709a9ee3a31 (diff) |
Move DDE to pthreads
Makefiles, headers, types, macros and function calls are renamed where
appropriate. ds_server now takes a void* and returns a void*.
* dde26_test/Makefile: Switch from cthreads to pthreads.
* dde_e100/Makefile: Likewise.
* dde_e100/main.c: Likewise.
* dde26_test/Makefile: Likewise.
* dde_e100/Makefile: Likewise.
* dde_e100/main.c: Likewise.
* dde_e1000/Makefile: Likewise.
* dde_e1000/main.c: Likewise.
* dde_ne2k_pci/Makefile: Likewise.
* dde_ne2k_pci/main.c: Likewise.
* dde_pcnet32/Makefile: Likewise.
* dde_pcnet32/main.c: Likewise.
* dde_pcnet32_test/Makefile: Likewise.
* dde_rtl8139/Makefile: Likewise.
* dde_rtl8139/main.c: Likewise.
* libbpf/Makefile: Likewise.
* libddekit/Makefile: Likewise.
* libddekit/condvar.c: Likewise.
* libddekit/lock.c: Likewise.
* libddekit/pgtab.c: Likewise.
* libddekit/thread.c: Likewise.
* libddekit/timer.c: Likewise.
* libhurd-slab/Makefile: Likewise.
* libhurd-slab/slab.c: Likewise.
* libhurd-slab/slab.h: Likewise.
* libmachdev/Makefile: Likewise.
* libmachdev/dev_hdr.h: Likewise.
* libmachdev/ds_routines.c: Likewise.
* libmachdev/if_hdr.h: Likewise.
* libmachdev/io_req.h: Likewise.
* libmachdev/machdev.h: Likewise.
* libmachdev/net.c: Likewise.
* libmachdev/queue.h: Likewise.
Diffstat (limited to 'libddekit')
-rw-r--r-- | libddekit/Makefile | 3 | ||||
-rw-r--r-- | libddekit/condvar.c | 24 | ||||
-rw-r--r-- | libddekit/lock.c | 21 | ||||
-rw-r--r-- | libddekit/pgtab.c | 32 | ||||
-rw-r--r-- | libddekit/thread.c | 160 | ||||
-rw-r--r-- | libddekit/timer.c | 5 |
6 files changed, 111 insertions, 134 deletions
diff --git a/libddekit/Makefile b/libddekit/Makefile index 29000cd9..a11534b7 100644 --- a/libddekit/Makefile +++ b/libddekit/Makefile @@ -40,7 +40,8 @@ LCLHDRS = $(installhdrs) \ MIGSTUBS = deviceUser.o machUser.o OBJS = $(sort $(SRCS:.c=.o) $(MIGSTUBS)) -HURDLIBS = threads ports shouldbeinlibc hurd-slab +HURDLIBS = ports shouldbeinlibc hurd-slab +OTHERLIBS = -lpthread MIGCOMSFLAGS = -prefix dde_ diff --git a/libddekit/condvar.c b/libddekit/condvar.c index 55c57f8b..64df3ed6 100644 --- a/libddekit/condvar.c +++ b/libddekit/condvar.c @@ -4,31 +4,35 @@ * * \author Thomas Friebel <tf13@os.inf.tu-dresden.de> */ -#include <cthreads.h> +#include <pthread.h> +#include "ddekit/memory.h" #include "ddekit/condvar.h" struct ddekit_condvar { - struct condition cond; + pthread_cond_t cond; }; ddekit_condvar_t *ddekit_condvar_init() { - struct condition *cvp; + ddekit_condvar_t *cvp; - cvp = condition_alloc (); - condition_init (cvp); + cvp = ddekit_simple_malloc (sizeof (*cvp)); - return (ddekit_condvar_t *) cvp; + if (cvp == NULL) + return NULL; + + pthread_cond_init (&cvp->cond, NULL); + return cvp; } void ddekit_condvar_deinit(ddekit_condvar_t *cvp) { - condition_free (&cvp->cond); + ddekit_simple_free (cvp); } void ddekit_condvar_wait(ddekit_condvar_t *cvp, ddekit_lock_t *mp) { /* This isn't nice. The encapsulation is broken. * TODO I can merge the two files condvar.c and lock.c. */ - condition_wait (&cvp->cond, (struct mutex *) *mp); + pthread_cond_wait (&cvp->cond, (pthread_mutex_t *) *mp); } int ddekit_condvar_wait_timed(ddekit_condvar_t *cvp, @@ -40,9 +44,9 @@ int ddekit_condvar_wait_timed(ddekit_condvar_t *cvp, void ddekit_condvar_signal(ddekit_condvar_t *cvp) { - condition_signal (&cvp->cond); + pthread_cond_signal (&cvp->cond); } void ddekit_condvar_broadcast(ddekit_condvar_t *cvp) { - condition_broadcast (&cvp->cond); + pthread_cond_broadcast (&cvp->cond); } diff --git a/libddekit/lock.c b/libddekit/lock.c index e534bebe..26d5cfc8 100644 --- a/libddekit/lock.c +++ b/libddekit/lock.c @@ -1,38 +1,39 @@ -#include <cthreads.h> +#include <pthread.h> #include "ddekit/lock.h" #include "ddekit/memory.h" +#include "ddekit/thread.h" #define DDEKIT_DEBUG_LOCKS 0 struct ddekit_lock { - struct mutex lock; - cthread_t helder; + pthread_mutex_t lock; + ddekit_thread_t *helder; }; void _ddekit_lock_init(struct ddekit_lock **mtx) { struct ddekit_lock *lock; lock = (struct ddekit_lock *) ddekit_simple_malloc (sizeof *lock); - mutex_init (&lock->lock); + pthread_mutex_init (&lock->lock, NULL); lock->helder = NULL; *mtx = lock; } void _ddekit_lock_deinit(struct ddekit_lock **mtx) { - mutex_free (*mtx); + ddekit_simple_free (*mtx); *mtx = NULL; } void _ddekit_lock_lock(struct ddekit_lock **mtx) { - mutex_lock (&(*mtx)->lock); - (*mtx)->helder = cthread_self (); + pthread_mutex_lock (&(*mtx)->lock); + (*mtx)->helder = ddekit_thread_myself (); } /* returns 0 on success, != 0 if it would block */ int _ddekit_lock_try_lock(struct ddekit_lock **mtx) { - if (mutex_try_lock (&(*mtx)->lock)) { /* lock succeessfully */ - (*mtx)->helder = cthread_self (); + if (!pthread_mutex_trylock (&(*mtx)->lock)) { /* lock succeessfully */ + (*mtx)->helder = ddekit_thread_myself (); return 0; } return -1; @@ -41,7 +42,7 @@ int _ddekit_lock_try_lock(struct ddekit_lock **mtx) { void _ddekit_lock_unlock(struct ddekit_lock **mtx) { // TODO I wonder if it can cause any trouble. (*mtx)->helder = NULL; - mutex_unlock (&(*mtx)->lock); + pthread_mutex_unlock (&(*mtx)->lock); } diff --git a/libddekit/pgtab.c b/libddekit/pgtab.c index e7a94599..81138108 100644 --- a/libddekit/pgtab.c +++ b/libddekit/pgtab.c @@ -15,7 +15,7 @@ #include <error.h> #include <errno.h> #include <mach.h> -#include <cthreads.h> +#include <pthread.h> #include "ddekit/pgtab.h" #include "util.h" @@ -35,7 +35,7 @@ static struct entry *regions; static int num_regions; /* The size of the array REGIONS */ static int capability; -static struct mutex lock; +static pthread_mutex_t lock; #define INIT_SIZE 128 /***************************** @@ -78,15 +78,15 @@ static struct entry *get_entry_from_virt (const ddekit_addr_t virt) ddekit_addr_t ddekit_pgtab_get_physaddr(const void *virtual) { struct entry *e; - mutex_lock (&lock); + pthread_mutex_lock (&lock); e = get_entry_from_virt ((ddekit_addr_t) virtual); if (e) { ddekit_addr_t phys = e->physical + (virtual - e->virtual); - mutex_unlock (&lock); + pthread_mutex_unlock (&lock); return phys; } - mutex_unlock (&lock); + pthread_mutex_unlock (&lock); ddekit_printf ("a virtual address %p doesn't exist.\n", virtual); return -1; @@ -102,16 +102,16 @@ ddekit_addr_t ddekit_pgtab_get_virtaddr(const ddekit_addr_t physical) { struct entry *e; - mutex_lock (&lock); + pthread_mutex_lock (&lock); e = get_entry_from_phys (physical); if (e) { ddekit_addr_t virt = (ddekit_addr_t) e->virtual + (physical - e->physical); - mutex_unlock (&lock); + pthread_mutex_unlock (&lock); return virt; } - mutex_unlock (&lock); + pthread_mutex_unlock (&lock); ddekit_printf ("a physical address %p doesn't exist.\n", physical); @@ -139,15 +139,15 @@ int ddekit_pgtab_get_type(const void *virtual) int ddekit_pgtab_get_size(const void *virtual) { struct entry *e; - mutex_lock (&lock); + pthread_mutex_lock (&lock); e = get_entry_from_virt ((ddekit_addr_t) virtual); if (e) { int size = e->size; - mutex_unlock (&lock); + pthread_mutex_unlock (&lock); return size; } - mutex_unlock (&lock); + pthread_mutex_unlock (&lock); return 0; } @@ -162,14 +162,14 @@ void ddekit_pgtab_clear_region(void *virtual, int type) { struct entry *e; - mutex_lock (&lock); + pthread_mutex_lock (&lock); e = get_entry_from_virt ((ddekit_addr_t) virtual); if (e) { *e = regions[num_regions - 1]; num_regions--; } - mutex_unlock (&lock); + pthread_mutex_unlock (&lock); } @@ -188,7 +188,7 @@ void ddekit_pgtab_set_region(void *virtual, ddekit_addr_t physical, int pages, i void ddekit_pgtab_set_region_with_size(void *virt, ddekit_addr_t phys, int size, int type) { - mutex_lock (&lock); + pthread_mutex_lock (&lock); if (num_regions == capability) { capability *= 2; @@ -201,13 +201,13 @@ void ddekit_pgtab_set_region_with_size(void *virt, ddekit_addr_t phys, int size, regions[num_regions].size = size; regions[num_regions].type = type; num_regions++; - mutex_unlock (&lock); + pthread_mutex_unlock (&lock); } int pgtab_init () { capability = INIT_SIZE; regions = malloc (sizeof (struct entry) * capability); - mutex_init (&lock); + pthread_mutex_init (&lock, NULL); return 0; } diff --git a/libddekit/thread.c b/libddekit/thread.c index 10b86de8..8f7b3ed9 100644 --- a/libddekit/thread.c +++ b/libddekit/thread.c @@ -1,8 +1,9 @@ #include <stdio.h> #include <string.h> -#include <cthreads.h> +#include <pthread.h> #include <time.h> #include <error.h> +#include <mach.h> #include <hurd.h> #include <sys/time.h> #include <assert.h> @@ -26,39 +27,30 @@ struct _ddekit_private_data { }; struct ddekit_thread { - struct cthread thread; + pthread_t thread; + char *name; + struct _ddekit_private_data *private; + void *user; }; struct ddekit_sem { - spin_lock_t lock; + pthread_spinlock_t lock; /* A list of thread waiting for the semaphore. */ struct list head; int value; }; -static struct mutex global_lock = MUTEX_INITIALIZER; +static __thread struct ddekit_thread *thread_self; static void _thread_cleanup () { - const char *name; - struct _ddekit_private_data *data; - cthread_t t = cthread_self (); - - /* I have to free the sleep condition variable - * before the thread exits. */ - mutex_lock (&global_lock); - data = cthread_ldata (t); - cthread_set_ldata (t, NULL); - mutex_unlock (&global_lock); mach_port_destroy (mach_task_self (), - data->wakeupmsg.msgh_remote_port); - ddekit_condvar_deinit (data->sleep_cond); - ddekit_simple_free (data); - - name = cthread_name (t); - cthread_set_name (t, NULL); - ddekit_simple_free ((char *) name); + thread_self->private->wakeupmsg.msgh_remote_port); + ddekit_condvar_deinit (thread_self->private->sleep_cond); + ddekit_simple_free (thread_self->private); + ddekit_simple_free (thread_self->name); + ddekit_simple_free (thread_self); } /* Prepare a wakeup message. */ @@ -105,15 +97,9 @@ static void setup_thread (struct ddekit_thread *t, const char *name) { else strcpy (cpy, name); - cthread_set_name (&t->thread, cpy); + t->name = cpy; } - /* - * ldata isn't used by cthread. Since cthread isn't exposed to - * the user of this library. It's very safe to store - * the condition variable in ldata. - */ - private_data = (struct _ddekit_private_data *) ddekit_simple_malloc (sizeof (*private_data)); @@ -126,15 +112,13 @@ static void setup_thread (struct ddekit_thread *t, const char *name) { if (err) error (1, err, "_create_wakeupmsg"); - mutex_lock (&global_lock); - cthread_set_ldata (&t->thread, private_data); - mutex_unlock (&global_lock); + t->private = private_data; } ddekit_thread_t *ddekit_thread_setup_myself(const char *name) { - ddekit_thread_t *td = ddekit_thread_myself(); - + ddekit_thread_t *td = (ddekit_thread_t *) malloc (sizeof (*td)); setup_thread (td, name); + thread_self = td; return td; } @@ -142,54 +126,58 @@ typedef struct { void (*fun)(void *); void *arg; - struct condition cond; - struct mutex lock; + struct ddekit_thread *td; + pthread_cond_t cond; + pthread_mutex_t lock; int status; } priv_arg_t; static void* _priv_fun (void *arg) { priv_arg_t *priv_arg = arg; + thread_self = priv_arg->td; /* We wait until the initialization of the thread is finished. */ - mutex_lock (&priv_arg->lock); + pthread_mutex_lock (&priv_arg->lock); while (!priv_arg->status) - condition_wait (&priv_arg->cond, &priv_arg->lock); - mutex_unlock (&priv_arg->lock); + pthread_cond_wait (&priv_arg->cond, &priv_arg->lock); + pthread_mutex_unlock (&priv_arg->lock); priv_arg->fun(priv_arg->arg); + free (priv_arg->arg); _thread_cleanup (); return NULL; } ddekit_thread_t *ddekit_thread_create(void (*fun)(void *), void *arg, const char *name) { - ddekit_thread_t *td; - priv_arg_t *priv_arg = (priv_arg_t *) malloc (sizeof (*priv_arg)); + ddekit_thread_t *td = (ddekit_thread_t *) malloc (sizeof (*td)); + setup_thread (td, name); + priv_arg_t *priv_arg = (priv_arg_t *) malloc (sizeof (*priv_arg)); priv_arg->fun = fun; priv_arg->arg = arg; - condition_init (&priv_arg->cond); - mutex_init (&priv_arg->lock); + priv_arg->td = td; + pthread_cond_init (&priv_arg->cond, NULL); + pthread_mutex_init (&priv_arg->lock, NULL); priv_arg->status = 0; - td = (ddekit_thread_t *) cthread_fork (_priv_fun, priv_arg); - cthread_detach (&td->thread); - setup_thread (td, name); + pthread_create (&td->thread, NULL, _priv_fun, priv_arg); + pthread_detach (td->thread); /* Tell the new thread that initialization has been finished. */ - mutex_lock (&priv_arg->lock); + pthread_mutex_lock (&priv_arg->lock); priv_arg->status = 1; - cond_signal (&priv_arg->cond); - mutex_unlock (&priv_arg->lock); + pthread_cond_signal (&priv_arg->cond); + pthread_mutex_unlock (&priv_arg->lock); return td; } ddekit_thread_t *ddekit_thread_myself(void) { - return (ddekit_thread_t *) cthread_self (); + return thread_self; } void ddekit_thread_set_data(ddekit_thread_t *thread, void *data) { - cthread_set_data ((cthread_t) thread, data); + thread->user = data; } void ddekit_thread_set_my_data(void *data) { @@ -197,7 +185,7 @@ void ddekit_thread_set_my_data(void *data) { } void *ddekit_thread_get_data(ddekit_thread_t *thread) { - return cthread_data ((cthread_t) thread); + return thread->user; } void *ddekit_thread_get_my_data() { @@ -239,47 +227,36 @@ void ddekit_thread_nsleep(unsigned long nsecs) { } void ddekit_thread_sleep(ddekit_lock_t *lock) { - struct _ddekit_private_data *data; - - mutex_lock (&global_lock); - data= cthread_ldata (cthread_self ()); - mutex_unlock (&global_lock); - - // TODO condition_wait cannot guarantee that the thread is + // TODO pthread_cond_wait cannot guarantee that the thread is // woke up by another thread, maybe by signals. // Does it matter here? - ddekit_condvar_wait (data->sleep_cond, lock); + // If it does, use pthread_hurd_cond_wait_np. + ddekit_condvar_wait (thread_self->private->sleep_cond, lock); } void ddekit_thread_wakeup(ddekit_thread_t *td) { - struct _ddekit_private_data *data; - - mutex_lock (&global_lock); - data = cthread_ldata (&td->thread); - mutex_unlock (&global_lock); - - if (data == NULL) + if (td->private == NULL) return; - ddekit_condvar_signal (data->sleep_cond); + ddekit_condvar_signal (td->private->sleep_cond); } void ddekit_thread_exit() { _thread_cleanup (); - cthread_exit (0); + pthread_exit (NULL); } const char *ddekit_thread_get_name(ddekit_thread_t *thread) { - return cthread_name ((cthread_t) thread); + return thread->name; } void ddekit_thread_schedule(void) { - cthread_yield(); + swtch_pri (0); } void ddekit_yield(void) { - cthread_yield(); + swtch_pri (0); } void ddekit_init_threads() { @@ -324,34 +301,29 @@ static void _block (struct _ddekit_private_data *data) static int _sem_timedwait_internal (ddekit_sem_t *restrict sem, const int timeout) { - struct _ddekit_private_data *self_private_data; - - spin_lock (&sem->lock); + pthread_spin_lock (&sem->lock); if (sem->value > 0) { /* Successful down. */ sem->value --; - spin_unlock (&sem->lock); + pthread_spin_unlock (&sem->lock); return 0; } if (timeout < 0) { + pthread_spin_unlock (&sem->lock); errno = EINVAL; return -1; } /* Add ourselves to the queue. */ - mutex_lock (&global_lock); - self_private_data = cthread_ldata (cthread_self ()); - mutex_unlock (&global_lock); - - add_entry_head (&sem->head, (struct list *) self_private_data); - spin_unlock (&sem->lock); + add_entry_head (&sem->head, &thread_self->private->list); + pthread_spin_unlock (&sem->lock); /* Block the thread. */ if (timeout) { error_t err; - err = _timedblock (self_private_data, timeout); + err = _timedblock (thread_self->private, timeout); if (err) { /* We timed out. We may need to disconnect ourself from the waiter queue. @@ -361,16 +333,16 @@ static int _sem_timedwait_internal (ddekit_sem_t *restrict sem, block. */ assert (err == ETIMEDOUT); - spin_lock (&sem->lock); - remove_entry ((struct list *) self_private_data); - spin_unlock (&sem->lock); + pthread_spin_lock (&sem->lock); + remove_entry (&thread_self->private->list); + pthread_spin_unlock (&sem->lock); errno = err; return -1; } } else - _block (self_private_data); + _block (thread_self->private); return 0; } @@ -390,7 +362,7 @@ ddekit_sem_t *ddekit_sem_init(int value) { ddekit_sem_t *sem = (ddekit_sem_t *) ddekit_simple_malloc (sizeof (*sem)); - sem->lock = SPIN_LOCK_INITIALIZER; + sem->lock = PTHREAD_SPINLOCK_INITIALIZER; sem->head.prev = &sem->head; sem->head.next = &sem->head; sem->value = value; @@ -411,14 +383,14 @@ void ddekit_sem_down(ddekit_sem_t *sem) { /* returns 0 on success, != 0 when it would block */ int ddekit_sem_down_try(ddekit_sem_t *sem) { - spin_lock (&sem->lock); + pthread_spin_lock (&sem->lock); if (sem->value > 0) { /* Successful down. */ sem->value --; - spin_unlock (&sem->lock); + pthread_spin_unlock (&sem->lock); return 0; } - spin_unlock (&sem->lock); + pthread_spin_unlock (&sem->lock); return -1; } @@ -432,19 +404,19 @@ int ddekit_sem_down_timed(ddekit_sem_t *sem, int timo) { void ddekit_sem_up(ddekit_sem_t *sem) { struct _ddekit_private_data *wakeup; - spin_lock (&sem->lock); + pthread_spin_lock (&sem->lock); if (sem->value > 0) { /* Do a quick up. */ assert (EMPTY_LIST (&sem->head)); sem->value ++; - spin_unlock (&sem->lock); + pthread_spin_unlock (&sem->lock); return; } if (EMPTY_LIST (&sem->head)) { /* No one waiting. */ sem->value = 1; - spin_unlock (&sem->lock); + pthread_spin_unlock (&sem->lock); return; } @@ -455,7 +427,7 @@ void ddekit_sem_up(ddekit_sem_t *sem) { struct _ddekit_private_data, list); /* Then drop the lock and transfer control. */ - spin_unlock (&sem->lock); + pthread_spin_unlock (&sem->lock); if (wakeup) _thread_wakeup (wakeup); } diff --git a/libddekit/timer.c b/libddekit/timer.c index a8a91a85..169f867c 100644 --- a/libddekit/timer.c +++ b/libddekit/timer.c @@ -1,6 +1,5 @@ #include <error.h> #include <maptime.h> -#include <cthreads.h> #include <stdio.h> #include "ddekit/lock.h" @@ -322,12 +321,12 @@ void ddekit_init_timers(void) { error_t err; struct timeval tp; - static boolean_t initialized = FALSE; + static int initialized = 0; if (initialized) return; - initialized = TRUE; + initialized = 1; err = maptime_map (0, 0, &mapped_time); if (err) error (2, err, "cannot map time device"); |