From 4143760a944470d50975ee27da9e3380223e88b9 Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Sun, 28 Sep 2014 18:11:39 -0400 Subject: synchronize outside communication --- lib/libalpm/handle.c | 10 ++++++++++ lib/libalpm/handle.h | 12 ++++++++++++ lib/libalpm/log.c | 11 +++++++++++ lib/libalpm/thread.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 lib/libalpm/thread.h diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index 08625e3a..941bdd25 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -47,6 +47,11 @@ alpm_handle_t *_alpm_handle_new(void) handle->deltaratio = 0.0; handle->lockfd = -1; +#ifdef HAVE_PTHREAD + pthread_mutex_init(&(handle->tlock_cb), NULL); + pthread_mutex_init(&(handle->tlock_log), NULL); +#endif + return handle; } @@ -75,6 +80,11 @@ void _alpm_handle_free(alpm_handle_t *handle) FREELIST(handle->known_keys); #endif +#ifdef HAVE_PTHREAD + pthread_mutex_destroy(&(handle->tlock_cb)); + pthread_mutex_destroy(&(handle->tlock_log)); +#endif + regfree(&handle->delta_regex); /* free memory */ diff --git a/lib/libalpm/handle.h b/lib/libalpm/handle.h index 115c3481..9cf396ce 100644 --- a/lib/libalpm/handle.h +++ b/lib/libalpm/handle.h @@ -26,6 +26,7 @@ #include "alpm_list.h" #include "alpm.h" +#include "thread.h" #ifdef HAVE_LIBCURL #include @@ -34,19 +35,25 @@ #define EVENT(h, e) \ do { \ if((h)->eventcb) { \ + _ALPM_TLOCK_CB(h); \ (h)->eventcb((alpm_event_t *) (e)); \ + _ALPM_TUNLOCK_CB(h); \ } \ } while(0) #define QUESTION(h, q) \ do { \ if((h)->questioncb) { \ + _ALPM_TLOCK_CB(h); \ (h)->questioncb((alpm_question_t *) (q)); \ + _ALPM_TUNLOCK_CB(h); \ } \ } while(0) #define PROGRESS(h, e, p, per, n, r) \ do { \ if((h)->progresscb) { \ + _ALPM_TLOCK_CB(h); \ (h)->progresscb(e, p, per, n, r); \ + _ALPM_TUNLOCK_CB(h); \ } \ } while(0) @@ -114,6 +121,11 @@ struct __alpm_handle_t { /* for delta parsing efficiency */ int delta_regex_compiled; regex_t delta_regex; + +#ifdef HAVE_PTHREAD + pthread_mutex_t tlock_cb; + pthread_mutex_t tlock_log; +#endif }; alpm_handle_t *_alpm_handle_new(void); diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c index f7dd5336..82c0edad 100644 --- a/lib/libalpm/log.c +++ b/lib/libalpm/log.c @@ -22,12 +22,14 @@ #include #include #include +#include /* libalpm */ #include "log.h" #include "handle.h" #include "util.h" #include "alpm.h" +#include "thread.h" /** \addtogroup alpm_log Logging Functions * @brief Functions to log using libalpm @@ -62,6 +64,7 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, if(!(prefix && *prefix)) { prefix = "UNKNOWN"; } + _ALPM_TLOCK_LOG(handle); /* check if the logstream is open already, opening it if needed */ if(handle->logstream == NULL && handle->logfile != NULL) { @@ -83,6 +86,8 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, } } + _ALPM_TUNLOCK_LOG(handle); + va_start(args, fmt); if(handle->usesyslog) { @@ -118,7 +123,9 @@ void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag, const char *fmt, ... } va_start(args, fmt); + _ALPM_TLOCK_CB(handle); handle->logcb(flag, fmt, args); + _ALPM_TUNLOCK_CB(handle); va_end(args); } @@ -138,6 +145,8 @@ int _alpm_logaction(alpm_handle_t *handle, const char *prefix, prefix = "UNKNOWN"; } + _ALPM_TLOCK_LOG(handle); + if(handle->usesyslog) { /* we can't use a va_list more than once, so we need to copy it * so we can use the original when calling vfprintf below. */ @@ -162,6 +171,8 @@ int _alpm_logaction(alpm_handle_t *handle, const char *prefix, fflush(handle->logstream); } + _ALPM_TUNLOCK_LOG(handle); + return ret; } diff --git a/lib/libalpm/thread.h b/lib/libalpm/thread.h new file mode 100644 index 00000000..1773bdae --- /dev/null +++ b/lib/libalpm/thread.h @@ -0,0 +1,43 @@ +/* + * thread.h + * + * Copyright (c) 2006-2014 Pacman Development Team + * Copyright (c) 2002-2006 by Judd Vinet + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef _ALPM_THREAD_H +#define _ALPM_THREAD_H + +#ifdef HAVE_PTHREAD + +#define _ALPM_TLOCK_CB(h) pthread_mutex_lock(&((h)->tlock_cb)) +#define _ALPM_TLOCK_LOG(h) pthread_mutex_lock(&((h)->tlock_log)) + +#define _ALPM_TUNLOCK_CB(h) pthread_mutex_unlock(&((h)->tlock_cb)) +#define _ALPM_TUNLOCK_LOG(h) pthread_mutex_unlock(&((h)->tlock_log)) + +#else + +#define _ALPM_TLOCK_CB(h) +#define _ALPM_TLOCK_LOG(h) + +#define _ALPM_TUNLOCK_CB(h) +#define _ALPM_TUNLOCK_LOG(h) + +#endif /* HAVE_PTHREAD */ + +#endif /* _ALPM_THREAD_H */ + +/* vim: set noet: */ -- cgit v1.2.3-24-g4f1b