From f263abb8b777b61bcce7eee88716a137d0f880c3 Mon Sep 17 00:00:00 2001 From: waker Date: Fri, 28 Aug 2009 20:30:20 +0200 Subject: lastfm multithreading WIP --- threading_pthread.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'threading_pthread.c') diff --git a/threading_pthread.c b/threading_pthread.c index ae8bf258..7bb3d50b 100644 --- a/threading_pthread.c +++ b/threading_pthread.c @@ -20,7 +20,8 @@ #include #include "threading.h" -void thread_start (void (*fn)(uintptr_t ctx), uintptr_t ctx) { +void +thread_start (void (*fn)(uintptr_t ctx), uintptr_t ctx) { pthread_t tid; pthread_attr_t attr; int s = pthread_attr_init (&attr); @@ -39,30 +40,71 @@ void thread_start (void (*fn)(uintptr_t ctx), uintptr_t ctx) { return; } } -uintptr_t mutex_create (void) { +uintptr_t +mutex_create (void) { pthread_mutex_t *mtx = malloc (sizeof (pthread_mutex_t)); if (pthread_mutex_init (mtx, NULL)) { printf ("pthread_mutex_init failed!\n"); } return (uintptr_t)mtx; } -void mutex_free (uintptr_t _mtx) { + +void +mutex_free (uintptr_t _mtx) { pthread_mutex_t *mtx = (pthread_mutex_t *)_mtx; mutex_lock (_mtx); mutex_unlock (_mtx); pthread_mutex_destroy (mtx); free (mtx); } -int mutex_lock (uintptr_t _mtx) { + +int +mutex_lock (uintptr_t _mtx) { pthread_mutex_t *mtx = (pthread_mutex_t *)_mtx; if (pthread_mutex_lock (mtx)) { printf ("pthread_mutex_lock failed\n"); } } -int mutex_unlock (uintptr_t _mtx) { + +int +mutex_unlock (uintptr_t _mtx) { pthread_mutex_t *mtx = (pthread_mutex_t *)_mtx; if (pthread_mutex_unlock (mtx)) { printf ("pthread_mutex_unlock failed\n"); }; } +uintptr_t +cond_create (void) { + pthread_cond_t *cond = malloc (sizeof (pthread_cond_t)); + pthread_cond_init (cond, NULL); + return (uintptr_t)cond; +} + +void +cond_free (uintptr_t c) { + if (c) { + pthread_cond_t *cond = (pthread_cond_t *)c; + pthread_cond_destroy (cond); + free (cond); + } +} + +int +cond_wait (uintptr_t c, uintptr_t m) { + pthread_cond_t *cond = (pthread_cond_t *)c; + pthread_mutex_t *mutex = (pthread_mutex_t *)m; + return pthread_cond_wait (cond, mutex); +} + +int +cond_signal (uintptr_t c) { + pthread_cond_t *cond = (pthread_cond_t *)c; + return pthread_cond_signal (cond); +} + +int +cond_broadcast (uintptr_t c) { + pthread_cond_t *cond = (pthread_cond_t *)c; + return pthread_cond_broadcast (cond); +} -- cgit v1.2.3