diff options
author | Andrew Gregory <andrew.gregory.8@gmail.com> | 2015-01-22 06:46:03 +0100 |
---|---|---|
committer | Andrew Gregory <andrew.gregory.8@gmail.com> | 2017-05-11 06:45:30 +0200 |
commit | 20bc9067c16c790172fbccb28043e1148040a5c1 (patch) | |
tree | d8c3a74b66c8abce82f7815cb04cf88d97b33227 | |
parent | 2e63d6aa7fcdfd89f02758f02f5799a291405b40 (diff) | |
download | pacman-20bc9067c16c790172fbccb28043e1148040a5c1.tar.gz pacman-20bc9067c16c790172fbccb28043e1148040a5c1.tar.xz |
add _alpm_run_threaded
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
-rw-r--r-- | lib/libalpm/handle.c | 22 | ||||
-rw-r--r-- | lib/libalpm/handle.h | 4 |
2 files changed, 26 insertions, 0 deletions
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: */ |