Make POSIX threads library optional

Guard out use of pthreads in hash and list and don't build the
thread queue when pthreads is not available.
This commit is contained in:
Matthew Brush 2013-03-04 03:03:11 -08:00
parent 5b2f19e1c4
commit ec404e96a9
5 changed files with 52 additions and 4 deletions

View File

@ -37,6 +37,7 @@ if test "$have_pthreads" = "yes"
then then
AC_SUBST([PTHREAD_LIBS], [-lpthread]) AC_SUBST([PTHREAD_LIBS], [-lpthread])
fi fi
AM_CONDITIONAL([USE_PTHREADS], [test x$have_pthreads = xyes])
# Allow enabling debug mode using configure argument # Allow enabling debug mode using configure argument
AC_ARG_ENABLE(debug, AC_ARG_ENABLE(debug,

View File

@ -1,10 +1,16 @@
lib_LIBRARIES = libcfu.a lib_LIBRARIES = libcfu.a
libcfu_a_SOURCES = cfuhash.c cfutime.c cfustring.c cfulist.c cfuconf.c \ libcfu_a_SOURCES = cfuhash.c cfutime.c cfustring.c cfulist.c cfuconf.c \
cfuthread_queue.c cfu.c cfuopt.c cfu.c cfuopt.c
include_HEADERS = cfu.h cfuhash.h cfutime.h cfustring.h cfulist.h \ include_HEADERS = cfu.h cfuhash.h cfutime.h cfustring.h cfulist.h \
cfuconf.h cfuthread_queue.h cfuopt.h cfuconf.h cfuopt.h
if USE_PTHREADS
libcfu_a_SOURCES += cfuthread_queue.c
include_HEADERS += cfuthread_queue.h
endif
bin_PROGRAMS = libcfu-config bin_PROGRAMS = libcfu-config

View File

@ -38,13 +38,20 @@
OF THE POSSIBILITY OF SUCH DAMAGE. OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "cfu.h" #include "cfu.h"
#include <pthread.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif
#include "cfuhash.h" #include "cfuhash.h"
#include "cfustring.h" #include "cfustring.h"
@ -77,7 +84,9 @@ struct cfuhash_table {
size_t num_buckets; size_t num_buckets;
size_t entries; /* Total number of entries in the table. */ size_t entries; /* Total number of entries in the table. */
cfuhash_entry **buckets; cfuhash_entry **buckets;
#ifdef HAVE_PTHREAD_H
pthread_mutex_t mutex; pthread_mutex_t mutex;
#endif
u_int32_t flags; u_int32_t flags;
cfuhash_function_t hash_func; cfuhash_function_t hash_func;
size_t each_bucket_index; size_t each_bucket_index;
@ -166,7 +175,10 @@ _cfuhash_new(size_t size, u_int32_t flags) {
ht->entries = 0; ht->entries = 0;
ht->flags = flags; ht->flags = flags;
ht->buckets = (cfuhash_entry **)calloc(size, sizeof(cfuhash_entry *)); ht->buckets = (cfuhash_entry **)calloc(size, sizeof(cfuhash_entry *));
#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&ht->mutex, NULL); pthread_mutex_init(&ht->mutex, NULL);
#endif
ht->hash_func = hash_func; ht->hash_func = hash_func;
ht->high = 0.75; ht->high = 0.75;
@ -287,25 +299,33 @@ static inline void
lock_hash(cfuhash_table_t *ht) { lock_hash(cfuhash_table_t *ht) {
if (!ht) return; if (!ht) return;
if (ht->flags & CFUHASH_NO_LOCKING) return; if (ht->flags & CFUHASH_NO_LOCKING) return;
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&ht->mutex); pthread_mutex_lock(&ht->mutex);
#endif
} }
static inline void static inline void
unlock_hash(cfuhash_table_t *ht) { unlock_hash(cfuhash_table_t *ht) {
if (!ht) return; if (!ht) return;
if (ht->flags & CFUHASH_NO_LOCKING) return; if (ht->flags & CFUHASH_NO_LOCKING) return;
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&ht->mutex); pthread_mutex_unlock(&ht->mutex);
#endif
} }
extern int extern int
cfuhash_lock(cfuhash_table_t *ht) { cfuhash_lock(cfuhash_table_t *ht) {
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&ht->mutex); pthread_mutex_lock(&ht->mutex);
#endif
return 1; return 1;
} }
extern int extern int
cfuhash_unlock(cfuhash_table_t *ht) { cfuhash_unlock(cfuhash_table_t *ht) {
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&ht->mutex); pthread_mutex_unlock(&ht->mutex);
#endif
return 1; return 1;
} }
@ -757,7 +777,9 @@ cfuhash_destroy_with_free_fn(cfuhash_table_t *ht, cfuhash_free_fn_t ff) {
} }
free(ht->buckets); free(ht->buckets);
unlock_hash(ht); unlock_hash(ht);
#ifdef HAVE_PTHREAD_H
pthread_mutex_destroy(&ht->mutex); pthread_mutex_destroy(&ht->mutex);
#endif
free(ht); free(ht);
return 1; return 1;

View File

@ -38,10 +38,18 @@
OF THE POSSIBILITY OF SUCH DAMAGE. OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "cfu.h" #include "cfu.h"
#include <stdlib.h> #include <stdlib.h>
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif
#include "cfulist.h" #include "cfulist.h"
#include "cfustring.h" #include "cfustring.h"
@ -68,7 +76,9 @@ struct cfulist {
cfulist_entry *entries; cfulist_entry *entries;
cfulist_entry *tail; cfulist_entry *tail;
size_t num_entries; size_t num_entries;
#ifdef HAVE_PTHREAD_H
pthread_mutex_t mutex; pthread_mutex_t mutex;
#endif
cfulist_entry *each_ptr; cfulist_entry *each_ptr;
cfulist_free_fn_t free_fn; cfulist_free_fn_t free_fn;
}; };
@ -76,7 +86,9 @@ struct cfulist {
extern cfulist_t * extern cfulist_t *
cfulist_new() { cfulist_new() {
cfulist_t *list = (cfulist_t *)calloc(1, sizeof(cfulist_t)); cfulist_t *list = (cfulist_t *)calloc(1, sizeof(cfulist_t));
#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&list->mutex, NULL); pthread_mutex_init(&list->mutex, NULL);
#endif
list->type = libcfu_t_list; list->type = libcfu_t_list;
return list; return list;
} }
@ -95,12 +107,16 @@ cfulist_num_entries(cfulist_t *list) {
static inline void static inline void
lock_list(cfulist_t *list) { lock_list(cfulist_t *list) {
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&list->mutex); pthread_mutex_lock(&list->mutex);
#endif
} }
static inline void static inline void
unlock_list(cfulist_t *list) { unlock_list(cfulist_t *list) {
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&list->mutex); pthread_mutex_unlock(&list->mutex);
#endif
} }
static inline cfulist_entry * static inline cfulist_entry *
@ -526,7 +542,9 @@ cfulist_destroy(cfulist_t *list) {
} }
} }
unlock_list(list); unlock_list(list);
#ifdef HAVE_PTHREAD_H
pthread_mutex_destroy(&list->mutex); pthread_mutex_destroy(&list->mutex);
#endif
free(list); free(list);
} }
@ -547,6 +565,8 @@ cfulist_destroy_with_free_fn(cfulist_t *list, cfulist_free_fn_t free_fn) {
} }
} }
unlock_list(list); unlock_list(list);
#ifdef HAVE_PTHREAD_H
pthread_mutex_destroy(&list->mutex); pthread_mutex_destroy(&list->mutex);
#endif
free(list); free(list);
} }

View File

@ -46,7 +46,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {