From 20bc9067c16c790172fbccb28043e1148040a5c1 Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Thu, 22 Jan 2015 00:46:03 -0500 Subject: add _alpm_run_threaded Signed-off-by: Andrew Gregory --- lib/libalpm/handle.c | 22 ++++++++++++++++++++++ lib/libalpm/handle.h | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index c58718fc..1a8a4344 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -48,6 +48,7 @@ alpm_handle_t *_alpm_handle_new(void) handle->lockfd = -1; #ifdef HAVE_PTHREAD + handle->threads = 4; pthread_mutex_init(&(handle->tlock_cb), NULL); pthread_mutex_init(&(handle->tlock_log), NULL); pthread_mutex_init(&(handle->tlock_task), NULL); @@ -905,4 +906,25 @@ void _alpm_set_errno(alpm_handle_t *handle, alpm_errno_t err) { #endif } +void _alpm_run_threaded(alpm_handle_t *handle, + void *(*function) (void *), void *arg) +{ +#ifdef HAVE_PTHREAD + if(handle->threads > 1) { + pthread_t threads[handle->threads]; + int idx; + for(idx = 0; idx < handle->threads; idx++) { + pthread_create(&threads[idx], NULL, function, arg); + } + for(idx = 0; idx < handle->threads; idx++) { + pthread_join(threads[idx], NULL); + } + } else { + function(arg); + } +#else + function(arg); +#endif +} + /* vim: set noet: */ diff --git a/lib/libalpm/handle.h b/lib/libalpm/handle.h index 10a430ac..56f771c2 100644 --- a/lib/libalpm/handle.h +++ b/lib/libalpm/handle.h @@ -124,6 +124,7 @@ struct __alpm_handle_t { regex_t delta_regex; #ifdef HAVE_PTHREAD + int threads; pthread_mutex_t tlock_cb; pthread_mutex_t tlock_log; pthread_mutex_t tlock_task; @@ -145,6 +146,9 @@ alpm_errno_t _alpm_set_directory_option(const char *value, void _alpm_set_errno(alpm_handle_t *handle, alpm_errno_t err); +void _alpm_run_threaded(alpm_handle_t *handle, + void *(*function) (void *), void *arg); + #endif /* ALPM_HANDLE_H */ /* vim: set noet: */ -- cgit v1.2.3-24-g4f1b