summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/libalpm/handle.c10
-rw-r--r--lib/libalpm/handle.h12
-rw-r--r--lib/libalpm/log.c11
-rw-r--r--lib/libalpm/thread.h43
4 files changed, 76 insertions, 0 deletions
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 <curl/curl.h>
@@ -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 <stdarg.h>
#include <errno.h>
#include <syslog.h>
+#include <pthread.h>
/* 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 <pacman-dev@archlinux.org>
+ * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+#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: */