diff --git a/configure.ac b/configure.ac index bfdd64b..cdcfae6 100644 --- a/configure.ac +++ b/configure.ac @@ -37,6 +37,7 @@ if test "$have_pthreads" = "yes" then AC_SUBST([PTHREAD_LIBS], [-lpthread]) fi +AM_CONDITIONAL([USE_PTHREADS], [test x$have_pthreads = xyes]) # Allow enabling debug mode using configure argument AC_ARG_ENABLE(debug, diff --git a/src/Makefile.am b/src/Makefile.am index d4f9c06..c7c55ec 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,10 +1,16 @@ lib_LIBRARIES = libcfu.a 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 \ - 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 diff --git a/src/cfuhash.c b/src/cfuhash.c index 382c5da..0914ddf 100644 --- a/src/cfuhash.c +++ b/src/cfuhash.c @@ -38,13 +38,20 @@ OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include "cfu.h" -#include #include #include #include +#ifdef HAVE_PTHREAD_H +# include +#endif + #include "cfuhash.h" #include "cfustring.h" @@ -77,7 +84,9 @@ struct cfuhash_table { size_t num_buckets; size_t entries; /* Total number of entries in the table. */ cfuhash_entry **buckets; +#ifdef HAVE_PTHREAD_H pthread_mutex_t mutex; +#endif u_int32_t flags; cfuhash_function_t hash_func; size_t each_bucket_index; @@ -166,7 +175,10 @@ _cfuhash_new(size_t size, u_int32_t flags) { ht->entries = 0; ht->flags = flags; ht->buckets = (cfuhash_entry **)calloc(size, sizeof(cfuhash_entry *)); + +#ifdef HAVE_PTHREAD_H pthread_mutex_init(&ht->mutex, NULL); +#endif ht->hash_func = hash_func; ht->high = 0.75; @@ -287,25 +299,33 @@ static inline void lock_hash(cfuhash_table_t *ht) { if (!ht) return; if (ht->flags & CFUHASH_NO_LOCKING) return; +#ifdef HAVE_PTHREAD_H pthread_mutex_lock(&ht->mutex); +#endif } static inline void unlock_hash(cfuhash_table_t *ht) { if (!ht) return; if (ht->flags & CFUHASH_NO_LOCKING) return; +#ifdef HAVE_PTHREAD_H pthread_mutex_unlock(&ht->mutex); +#endif } extern int cfuhash_lock(cfuhash_table_t *ht) { +#ifdef HAVE_PTHREAD_H pthread_mutex_lock(&ht->mutex); +#endif return 1; } extern int cfuhash_unlock(cfuhash_table_t *ht) { +#ifdef HAVE_PTHREAD_H pthread_mutex_unlock(&ht->mutex); +#endif return 1; } @@ -757,7 +777,9 @@ cfuhash_destroy_with_free_fn(cfuhash_table_t *ht, cfuhash_free_fn_t ff) { } free(ht->buckets); unlock_hash(ht); +#ifdef HAVE_PTHREAD_H pthread_mutex_destroy(&ht->mutex); +#endif free(ht); return 1; diff --git a/src/cfulist.c b/src/cfulist.c index f941370..29d4d53 100644 --- a/src/cfulist.c +++ b/src/cfulist.c @@ -38,10 +38,18 @@ OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include "cfu.h" #include +#ifdef HAVE_PTHREAD_H +# include +#endif + #include "cfulist.h" #include "cfustring.h" @@ -68,7 +76,9 @@ struct cfulist { cfulist_entry *entries; cfulist_entry *tail; size_t num_entries; +#ifdef HAVE_PTHREAD_H pthread_mutex_t mutex; +#endif cfulist_entry *each_ptr; cfulist_free_fn_t free_fn; }; @@ -76,7 +86,9 @@ struct cfulist { extern cfulist_t * cfulist_new() { cfulist_t *list = (cfulist_t *)calloc(1, sizeof(cfulist_t)); +#ifdef HAVE_PTHREAD_H pthread_mutex_init(&list->mutex, NULL); +#endif list->type = libcfu_t_list; return list; } @@ -95,12 +107,16 @@ cfulist_num_entries(cfulist_t *list) { static inline void lock_list(cfulist_t *list) { +#ifdef HAVE_PTHREAD_H pthread_mutex_lock(&list->mutex); +#endif } static inline void unlock_list(cfulist_t *list) { +#ifdef HAVE_PTHREAD_H pthread_mutex_unlock(&list->mutex); +#endif } static inline cfulist_entry * @@ -526,7 +542,9 @@ cfulist_destroy(cfulist_t *list) { } } unlock_list(list); +#ifdef HAVE_PTHREAD_H pthread_mutex_destroy(&list->mutex); +#endif free(list); } @@ -547,6 +565,8 @@ cfulist_destroy_with_free_fn(cfulist_t *list, cfulist_free_fn_t free_fn) { } } unlock_list(list); +#ifdef HAVE_PTHREAD_H pthread_mutex_destroy(&list->mutex); +#endif free(list); } diff --git a/src/cfulist.h b/src/cfulist.h index 16acf6c..2da7f1d 100644 --- a/src/cfulist.h +++ b/src/cfulist.h @@ -46,7 +46,6 @@ #include #include #include -#include #ifdef __cplusplus extern "C" {