2002-12-15 Havoc Pennington <hp@pobox.com>
authorHavoc Pennington <hp@redhat.com>
Sun, 15 Dec 2002 05:39:31 +0000 (05:39 +0000)
committerHavoc Pennington <hp@redhat.com>
Sun, 15 Dec 2002 05:39:31 +0000 (05:39 +0000)
* dbus/dbus-threads.c: add thread stubs that a higher library
layer can fill in. e.g. the GLib wrapper might fill them in with
GThread stuff. We still need to use this thread API to
thread-safe-ize the library.

ChangeLog
dbus/Makefile.am
dbus/dbus-memory.h
dbus/dbus-threads.c [new file with mode: 0644]
dbus/dbus-threads.h [new file with mode: 0644]
dbus/dbus-types.h
dbus/dbus.h

index 7887e8d..a373a6c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2002-12-15  Havoc Pennington  <hp@pobox.com>
+
+       * dbus/dbus-threads.c: add thread stubs that a higher library
+       layer can fill in. e.g. the GLib wrapper might fill them in with
+       GThread stuff. We still need to use this thread API to
+       thread-safe-ize the library.
+
 2002-12-12  Havoc Pennington  <hp@pobox.com>
 
        * dbus/dbus-transport-unix.c, dbus/dbus-server-unix.c: use the
index f74c8be..b5071f0 100644 (file)
@@ -12,6 +12,7 @@ dbusinclude_HEADERS=                          \
        dbus-memory.h                           \
        dbus-message.h                          \
        dbus-server.h                           \
+       dbus-threads.h                          \
        dbus-types.h
 
 libdbus_1_la_SOURCES=                          \
@@ -25,6 +26,7 @@ libdbus_1_la_SOURCES=                         \
        dbus-server-protected.h                 \
        dbus-server-unix.c                      \
        dbus-server-unix.h                      \
+       dbus-threads.c                          \
        dbus-transport.c                        \
        dbus-transport.h                        \
        dbus-transport-protected.h              \
index 7e4d522..e0b5a8b 100644 (file)
@@ -45,4 +45,4 @@ typedef void (* DBusFreeFunction) (void *memory);
 
 DBUS_END_DECLS;
 
-#endif /* DBUS_MESSAGE_H */
+#endif /* DBUS_MEMORY_H */
diff --git a/dbus/dbus-threads.c b/dbus/dbus-threads.c
new file mode 100644 (file)
index 0000000..4a1b021
--- /dev/null
@@ -0,0 +1,148 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-threads.h  D-BUS threads handling
+ *
+ * Copyright (C) 2002  Red Hat Inc.
+ *
+ * Licensed under the Academic Free License version 1.2
+ * 
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+#include "dbus-threads.h"
+#include "dbus-internals.h"
+
+static DBusThreadFunctions thread_functions =
+{
+  0,
+  NULL, NULL, NULL, NULL,
+
+  NULL, NULL, NULL, NULL,
+  NULL, NULL, NULL, NULL
+};
+
+/**
+ * @defgroup DBusThreads Thread functions
+ * @ingroup  DBus
+ * @brief dbus_threads_init(), dbus_mutex_lock(), etc.
+ *
+ * Functions and macros related to threads and thread locks.
+ *
+ * @{
+ */
+
+/**
+ * Creates a new mutex using the function supplied to dbus_threads_init(),
+ * or creates a no-op mutex if threads are not initialized.
+ * May return #NULL even if threads are initialized, indicating
+ * out-of-memory.
+ *
+ * @returns new mutex or #NULL
+ */
+DBusMutex*
+dbus_mutex_new (void)
+{
+  if (thread_functions.mutex_new)
+    return (* thread_functions.mutex_new) ();
+  else
+    return NULL;
+}
+
+/**
+ * Frees a mutex created with dbus_mutex_new(); does
+ * nothing if passed a #NULL pointer.
+ */
+void
+dbus_mutex_free (DBusMutex *mutex)
+{
+  if (mutex && thread_functions.mutex_free)
+    (* thread_functions.mutex_free) (mutex);
+}
+
+/**
+ * Locks a mutex. Does nothing if passed a #NULL pointer.
+ * Locks are not recursive.
+ *
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_mutex_lock (DBusMutex *mutex)
+{
+  if (mutex && thread_functions.mutex_lock)
+    return (* thread_functions.mutex_lock) (mutex);
+  else
+    return TRUE;
+}
+
+/**
+ * Unlocks a mutex. Does nothing if passed a #NULL pointer.
+ *
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_mutex_unlock (DBusMutex *mutex)
+{
+  if (mutex && thread_functions.mutex_unlock)
+    return (* thread_functions.mutex_unlock) (mutex);
+  else
+    return TRUE;
+}
+
+/**
+ * Initializes threads. If this function is not called,
+ * the D-BUS library will not lock any data structures.
+ * If it is called, D-BUS will do locking, at some cost
+ * in efficiency. Note that this function must be called
+ * BEFORE using any other D-BUS functions.
+ *
+ * @param functions functions for using threads
+ */
+void
+dbus_threads_init (const DBusThreadFunctions *functions)
+{
+  _dbus_assert (functions != NULL);
+
+  /* these base functions are required. Future additions to
+   * DBusThreadFunctions may be optional.
+   */
+  _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_NEW_MASK);
+  _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_FREE_MASK);
+  _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_LOCK_MASK);
+  _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_UNLOCK_MASK);
+  _dbus_assert (functions->mutex_new != NULL);
+  _dbus_assert (functions->mutex_free != NULL);
+  _dbus_assert (functions->mutex_lock != NULL);
+  _dbus_assert (functions->mutex_unlock != NULL);
+
+  /* Check that all bits in the mask actually are valid mask bits.
+   * ensures people won't write code that breaks when we add
+   * new bits.
+   */
+  _dbus_assert ((functions->mask & ~DBUS_THREAD_FUNCTIONS_ALL_MASK) == 0);
+  
+  if (thread_functions.mask != 0)
+    {
+      _dbus_warn ("dbus_threads_init() may only be called one time\n");
+      return;
+    }
+  
+  thread_functions.mutex_new = functions->mutex_new;
+  thread_functions.mutex_free = functions->mutex_free;
+  thread_functions.mutex_lock = functions->mutex_lock;
+  thread_functions.mutex_unlock = functions->mutex_unlock;
+  
+  thread_functions.mask = functions->mask;
+}
+
+/** @} */
diff --git a/dbus/dbus-threads.h b/dbus/dbus-threads.h
new file mode 100644 (file)
index 0000000..956fac6
--- /dev/null
@@ -0,0 +1,82 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-threads.h  D-BUS threads handling
+ *
+ * Copyright (C) 2002  Red Hat Inc.
+ *
+ * Licensed under the Academic Free License version 1.2
+ * 
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+#if !defined (DBUS_INSIDE_DBUS_H) && !defined (DBUS_COMPILATION)
+#error "Only <dbus/dbus.h> can be included directly, this file may disappear or change contents."
+#endif
+
+#ifndef DBUS_THREADS_H
+#define DBUS_THREADS_H
+
+#include <dbus/dbus-macros.h>
+#include <dbus/dbus-types.h>
+
+DBUS_BEGIN_DECLS;
+
+typedef struct DBusMutex DBusMutex;
+
+typedef DBusMutex*  (* DBusMutexNewFunction)    (void);
+typedef void        (* DBusMutexFreeFunction)   (DBusMutex *mutex);
+typedef dbus_bool_t (* DBusMutexLockFunction)   (DBusMutex *mutex);
+typedef dbus_bool_t (* DBusMutexUnlockFunction) (DBusMutex *mutex);
+
+typedef enum 
+{
+  DBUS_THREAD_FUNCTIONS_NEW_MASK     = 1 << 0,
+  DBUS_THREAD_FUNCTIONS_FREE_MASK    = 1 << 1,
+  DBUS_THREAD_FUNCTIONS_LOCK_MASK    = 1 << 2,
+  DBUS_THREAD_FUNCTIONS_UNLOCK_MASK  = 1 << 3,
+
+  DBUS_THREAD_FUNCTIONS_ALL_MASK     = 0xf
+} DBusThreadFunctionsMask;
+
+typedef struct
+{
+  unsigned int mask;
+
+  DBusMutexNewFunction mutex_new;
+  DBusMutexFreeFunction mutex_free;
+  DBusMutexLockFunction mutex_lock;
+  DBusMutexUnlockFunction mutex_unlock;
+
+  void (* padding1) (void);
+  void (* padding2) (void);
+  void (* padding3) (void);
+  void (* padding4) (void);
+  void (* padding5) (void);
+  void (* padding6) (void);
+  void (* padding7) (void);
+  void (* padding8) (void);
+  
+} DBusThreadFunctions;
+
+
+DBusMutex*  dbus_mutex_new    (void);
+void        dbus_mutex_free   (DBusMutex *mutex);
+dbus_bool_t dbus_mutex_lock   (DBusMutex *mutex);
+dbus_bool_t dbus_mutex_unlock (DBusMutex *mutex);
+
+void dbus_threads_init (const DBusThreadFunctions *functions);
+
+DBUS_END_DECLS;
+
+#endif /* DBUS_THREADS_H */
index f46b236..a924278 100644 (file)
@@ -27,6 +27,8 @@
 #ifndef DBUS_TYPES_H
 #define DBUS_TYPES_H
 
+#include <stddef.h>
+
 typedef unsigned int  dbus_bool_t;
 typedef unsigned int  dbus_uint32_t;
 typedef int           dbus_int32_t;
index 8bebaf5..6dc14dc 100644 (file)
@@ -31,6 +31,7 @@
 #include <dbus/dbus-macros.h>
 #include <dbus/dbus-message.h>
 #include <dbus/dbus-server.h>
+#include <dbus/dbus-threads.h>
 #include <dbus/dbus-types.h>
 
 #undef DBUS_INSIDE_DBUS_H