Condition variable functions in separate DLL
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 28 Apr 2010 11:35:52 +0000 (13:35 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 28 Apr 2010 11:35:52 +0000 (13:35 +0200)
The condition variable functions are in a separate DLL on Windows -
named yaz_cond4.dll (release), yaz_cond4d.dll (debug). The split is
done because Condition Variable functions is only supported in Vista
or Windows Server 2008 or later.

src/Makefile.am
src/condvar.c [new file with mode: 0644]
src/mutex-p.h [new file with mode: 0644]
src/mutex.c
win/makefile

index f0a7ae6..00d897a 100644 (file)
@@ -102,7 +102,8 @@ libyaz_la_SOURCES=version.c options.c log.c \
   copy_types.c match_glob.c poll.c daemon.c \
   iconv_encode_marc8.c iconv_encode_iso_8859_1.c iconv_encode_wchar.c \
   iconv_decode_marc8.c iconv_decode_iso5426.c iconv_decode_danmarc.c sc.c \
-  json.c xml_include.c file_glob.c dirent.c mutex.c thread_id.c gettimeofday.c
+  json.c xml_include.c file_glob.c dirent.c mutex-p.h mutex.c condvar.c \
+  thread_id.c gettimeofday.c
 
 libyaz_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO)
 
diff --git a/src/condvar.c b/src/condvar.c
new file mode 100644 (file)
index 0000000..5bbd92a
--- /dev/null
@@ -0,0 +1,135 @@
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2010 Index Data
+ * See the file LICENSE for details.
+ */
+
+/**
+ * \file condvar.c
+ * \brief Wraps condition variables
+ *
+ */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stddef.h>
+#include <yaz/xmalloc.h>
+#include <yaz/nmem.h>
+#include <yaz/log.h>
+#include <yaz/mutex.h>
+#include <yaz/gettimeofday.h>
+#ifdef WIN32
+#include <windows.h>
+#include <sys/timeb.h>
+#endif
+#include <time.h>
+
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
+#if YAZ_POSIX_THREADS
+#include <pthread.h>
+#endif
+
+#include "mutex-p.h"
+
+struct yaz_cond {
+#ifdef WIN32
+    CONDITION_VARIABLE cond;
+#elif YAZ_POSIX_THREADS
+    pthread_cond_t cond;
+#endif
+};
+
+void yaz_cond_create(YAZ_COND *p)
+{
+    *p = (YAZ_COND) malloc(sizeof(**p));
+#ifdef WIN32
+    InitializeConditionVariable(&(*p)->cond);
+#elif YAZ_POSIX_THREADS
+    pthread_cond_init(&(*p)->cond, 0);
+#endif
+}
+
+void yaz_cond_destroy(YAZ_COND *p)
+{
+    if (*p)
+    {
+#ifdef WIN32
+#elif YAZ_POSIX_THREADS
+        pthread_cond_destroy(&(*p)->cond);
+#endif
+        free(*p);
+        *p = 0;
+    }
+}
+
+int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timeval *abstime)
+{
+#ifdef WIN32
+    if (abstime)
+    {
+        struct timeval tval_now;
+        int sec, msec;
+
+        yaz_gettimeofday(&tval_now);
+
+        sec = abstime->tv_sec - tval_now.tv_sec;
+        msec = (abstime->tv_usec - tval_now.tv_usec) / 1000;
+        return SleepConditionVariableCS(&p->cond, &m->handle, sec*1000 + msec);
+    }
+    else
+        return SleepConditionVariableCS(&p->cond, &m->handle, INFINITE);
+#elif YAZ_POSIX_THREADS
+    if (abstime)
+    {
+        struct timespec s;
+        s.tv_sec = abstime->tv_sec;
+        s.tv_nsec = abstime->tv_usec * 1000;
+        return pthread_cond_timedwait(&p->cond, &m->handle, &s);
+    }
+    else
+        return pthread_cond_wait(&p->cond, &m->handle);
+#else
+    return -1;
+#endif
+}
+
+int yaz_cond_signal(YAZ_COND p)
+{
+#ifdef WIN32
+    WakeConditionVariable(&p->cond);
+    return 0;
+#elif YAZ_POSIX_THREADS
+    return pthread_cond_signal(&p->cond);
+#else
+    return -1;
+#endif
+}
+
+int yaz_cond_broadcast(YAZ_COND p)
+{
+#ifdef WIN32
+    WakeAllConditionVariable(&p->cond);
+    return 0;
+#elif YAZ_POSIX_THREADS
+    return pthread_cond_broadcast(&p->cond);
+#else
+    return -1;
+#endif
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+
diff --git a/src/mutex-p.h b/src/mutex-p.h
new file mode 100644 (file)
index 0000000..783f119
--- /dev/null
@@ -0,0 +1,15 @@
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2010 Index Data
+ * See the file LICENSE for details.
+ */
+
+struct yaz_mutex {
+#ifdef WIN32
+    CRITICAL_SECTION handle;
+#elif YAZ_POSIX_THREADS
+    pthread_mutex_t handle;
+#endif
+    char *name;
+    int log_level;
+};
+
index 6b80fe5..c4f6d5b 100644 (file)
 #include <pthread.h>
 #endif
 
-struct yaz_mutex {
-#ifdef WIN32
-    CRITICAL_SECTION handle;
-#elif YAZ_POSIX_THREADS
-    pthread_mutex_t handle;
-#endif
-    char *name;
-    int log_level;
-};
-
-struct yaz_cond {
-#ifdef WIN32
-    CONDITION_VARIABLE cond;
-#elif YAZ_POSIX_THREADS
-    pthread_cond_t cond;
-#endif
-};
+#include "mutex-p.h"
 
 void yaz_mutex_create(YAZ_MUTEX *p)
 {
@@ -167,85 +151,6 @@ void yaz_mutex_destroy(YAZ_MUTEX *p)
     }
 }
 
-
-void yaz_cond_create(YAZ_COND *p)
-{
-    *p = (YAZ_COND) malloc(sizeof(**p));
-#ifdef WIN32
-    InitializeConditionVariable(&(*p)->cond);
-#elif YAZ_POSIX_THREADS
-    pthread_cond_init(&(*p)->cond, 0);
-#endif
-}
-
-void yaz_cond_destroy(YAZ_COND *p)
-{
-    if (*p)
-    {
-#ifdef WIN32
-#elif YAZ_POSIX_THREADS
-        pthread_cond_destroy(&(*p)->cond);
-#endif
-        free(*p);
-        *p = 0;
-    }
-}
-
-int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timeval *abstime)
-{
-#ifdef WIN32
-    if (abstime)
-    {
-        struct timeval tval_now;
-        int sec, msec;
-
-        yaz_gettimeofday(&tval_now);
-
-        sec = abstime->tv_sec - tval_now.tv_sec;
-        msec = (abstime->tv_usec - tval_now.tv_usec) / 1000;
-        return SleepConditionVariableCS(&p->cond, &m->handle, sec*1000 + msec);
-    }
-    else
-        return SleepConditionVariableCS(&p->cond, &m->handle, INFINITE);
-#elif YAZ_POSIX_THREADS
-    if (abstime)
-    {
-        struct timespec s;
-        s.tv_sec = abstime->tv_sec;
-        s.tv_nsec = abstime->tv_usec * 1000;
-        return pthread_cond_timedwait(&p->cond, &m->handle, &s);
-    }
-    else
-        return pthread_cond_wait(&p->cond, &m->handle);
-#else
-    return -1;
-#endif
-}
-
-int yaz_cond_signal(YAZ_COND p)
-{
-#ifdef WIN32
-    WakeConditionVariable(&p->cond);
-    return 0;
-#elif YAZ_POSIX_THREADS
-    return pthread_cond_signal(&p->cond);
-#else
-    return -1;
-#endif
-}
-
-int yaz_cond_broadcast(YAZ_COND p)
-{
-#ifdef WIN32
-    WakeAllConditionVariable(&p->cond);
-    return 0;
-#elif YAZ_POSIX_THREADS
-    return pthread_cond_broadcast(&p->cond);
-#else
-    return -1;
-#endif
-}
-
 /*
  * Local variables:
  * c-basic-offset: 4
index 0afd57a..7ba0dcb 100644 (file)
@@ -92,11 +92,15 @@ YAZ_DLL=$(BINDIR)\yaz4d.dll
 YAZ_IMPLIB=$(LIBDIR)\yaz4d.lib
 YAZ_ICU_DLL=$(BINDIR)\yaz_icu4d.dll
 YAZ_ICU_IMPLIB=$(LIBDIR)\yaz_icu4d.lib
+YAZ_COND_DLL=$(BINDIR)\yaz_cond4d.dll
+YAZ_COND_IMPLIB=$(LIBDIR)\yaz_cond4d.lib
 !else
 YAZ_DLL=$(BINDIR)\yaz4.dll
 YAZ_IMPLIB=$(LIBDIR)\yaz4.lib
 YAZ_ICU_DLL=$(BINDIR)\yaz_icu4.dll
 YAZ_ICU_IMPLIB=$(LIBDIR)\yaz_icu4.lib
+YAZ_COND_DLL=$(BINDIR)\yaz_cond4.dll
+YAZ_COND_IMPLIB=$(LIBDIR)\yaz_cond4.lib
 !endif
 
 CLIENT=$(BINDIR)\yaz-client.exe
@@ -125,7 +129,7 @@ TST_TIMING=$(BINDIR)\test_timing.exe
 TEST_MUTEX=$(BINDIR)\test_mutex.exe
 
 # shortcut names defined here
-dll: dirs generate $(YAZ_DLL) 
+dll: dirs generate $(YAZ_DLL) $(YAZ_COND_DLL)
 client: dirs generate $(CLIENT)
 ztest: dirs generate $(ZTEST)
 
@@ -324,6 +328,7 @@ YAZ_CLIENT_OBJS= \
    $(OBJDIR)\fhistory.obj
 
 YAZ_ICU_OBJS= $(OBJDIR)\yaz-icu.obj 
+COND_DLL_OBJS= $(OBJDIR)\condvar.obj
 
 ZTEST_OBJS= \
     $(OBJDIR)\dummy-opac.obj \
@@ -843,6 +848,14 @@ $(YAZ_ICU_DLL) $(YAZ_ICU_IMPLIB): "$(BINDIR)" $(ICU_DLL_OBJS) $(YAZ_ICU_RES)
                /implib:"$(YAZ_ICU_IMPLIB)" 
        $(MT) -manifest $@.manifest -outputresource:$@;2
 
+$(YAZ_COND_DLL) $(YAZ_COND_IMPLIB): "$(BINDIR)" $(COND_DLL_OBJS) $(YAZ_COND_RES)
+       $(LINK_DLL) $(COND_LIB) $(YAZ_IMPLIB)\
+               $(COND_DLL_OBJS) \
+               $(YAZ_COND_RES) \
+               /out:$@ \
+               /implib:"$(YAZ_COND_IMPLIB)" 
+       $(MT) -manifest $@.manifest -outputresource:$@;2
+
 $(CLIENT) : "$(BINDIR)" $(YAZ_CLIENT_OBJS) $(YAZ_DLL)
        $(LINK_PROGRAM) $(YAZ_CLIENT_OBJS) /out:$@
        $(MT) -manifest $@.manifest -outputresource:$@;1
@@ -925,8 +938,8 @@ $(TST_TIMING) : "$(BINDIR)" $(TST_TIMING_OBJS) $(YAZ_DLL)
        $(LINK_PROGRAM) $(TST_TIMING_OBJS) /out:$@
        $(MT) -manifest $@.manifest -outputresource:$@;1
 
-$(TEST_MUTEX) : "$(BINDIR)" $(TEST_MUTEX_OBJS) $(YAZ_DLL)
-       $(LINK_PROGRAM) $(TEST_MUTEX_OBJS) /out:$@
+$(TEST_MUTEX) : "$(BINDIR)" $(TEST_MUTEX_OBJS) $(YAZ_COND_DLL)
+       $(LINK_PROGRAM) $(YAZ_COND_IMPLIB) $(TEST_MUTEX_OBJS) /out:$@
        $(MT) -manifest $@.manifest -outputresource:$@;1
 
 # Other rules