dbus-threads.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
  2. /* dbus-threads.h D-Bus threads handling
  3. *
  4. * Copyright (C) 2002 Red Hat Inc.
  5. *
  6. * Licensed under the Academic Free License version 2.1
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #if !defined (DBUS_INSIDE_DBUS_H) && !defined (DBUS_COMPILATION)
  24. #error "Only <dbus/dbus.h> can be included directly, this file may disappear or change contents."
  25. #endif
  26. #ifndef DBUS_THREADS_H
  27. #define DBUS_THREADS_H
  28. #include <dbus/dbus-macros.h>
  29. #include <dbus/dbus-types.h>
  30. DBUS_BEGIN_DECLS
  31. /**
  32. * @addtogroup DBusThreads
  33. * @{
  34. */
  35. /** An opaque mutex type provided by the #DBusThreadFunctions implementation installed by dbus_threads_init(). */
  36. typedef struct DBusMutex DBusMutex;
  37. /** An opaque condition variable type provided by the #DBusThreadFunctions implementation installed by dbus_threads_init(). */
  38. typedef struct DBusCondVar DBusCondVar;
  39. /** Deprecated, provide DBusRecursiveMutexNewFunction instead. */
  40. typedef DBusMutex* (* DBusMutexNewFunction) (void);
  41. /** Deprecated, provide DBusRecursiveMutexFreeFunction instead. */
  42. typedef void (* DBusMutexFreeFunction) (DBusMutex *mutex);
  43. /** Deprecated, provide DBusRecursiveMutexLockFunction instead. Return value is lock success, but gets ignored in practice. */
  44. typedef dbus_bool_t (* DBusMutexLockFunction) (DBusMutex *mutex);
  45. /** Deprecated, provide DBusRecursiveMutexUnlockFunction instead. Return value is unlock success, but gets ignored in practice. */
  46. typedef dbus_bool_t (* DBusMutexUnlockFunction) (DBusMutex *mutex);
  47. /** Creates a new recursively-lockable mutex, or returns #NULL if not
  48. * enough memory. Can only fail due to lack of memory. Found in
  49. * #DBusThreadFunctions. Do not just use PTHREAD_MUTEX_RECURSIVE for
  50. * this, because it does not save/restore the recursion count when
  51. * waiting on a condition. libdbus requires the Java-style behavior
  52. * where the mutex is fully unlocked to wait on a condition.
  53. */
  54. typedef DBusMutex* (* DBusRecursiveMutexNewFunction) (void);
  55. /** Frees a recursively-lockable mutex. Found in #DBusThreadFunctions.
  56. */
  57. typedef void (* DBusRecursiveMutexFreeFunction) (DBusMutex *mutex);
  58. /** Locks a recursively-lockable mutex. Found in #DBusThreadFunctions.
  59. * Can only fail due to lack of memory.
  60. */
  61. typedef void (* DBusRecursiveMutexLockFunction) (DBusMutex *mutex);
  62. /** Unlocks a recursively-lockable mutex. Found in #DBusThreadFunctions.
  63. * Can only fail due to lack of memory.
  64. */
  65. typedef void (* DBusRecursiveMutexUnlockFunction) (DBusMutex *mutex);
  66. /** Creates a new condition variable. Found in #DBusThreadFunctions.
  67. * Can only fail (returning #NULL) due to lack of memory.
  68. */
  69. typedef DBusCondVar* (* DBusCondVarNewFunction) (void);
  70. /** Frees a condition variable. Found in #DBusThreadFunctions.
  71. */
  72. typedef void (* DBusCondVarFreeFunction) (DBusCondVar *cond);
  73. /** Waits on a condition variable. Found in
  74. * #DBusThreadFunctions. Must work with either a recursive or
  75. * nonrecursive mutex, whichever the thread implementation
  76. * provides. Note that PTHREAD_MUTEX_RECURSIVE does not work with
  77. * condition variables (does not save/restore the recursion count) so
  78. * don't try using simply pthread_cond_wait() and a
  79. * PTHREAD_MUTEX_RECURSIVE to implement this, it won't work right.
  80. *
  81. * Has no error conditions. Must succeed if it returns.
  82. */
  83. typedef void (* DBusCondVarWaitFunction) (DBusCondVar *cond,
  84. DBusMutex *mutex);
  85. /** Waits on a condition variable with a timeout. Found in
  86. * #DBusThreadFunctions. Returns #TRUE if the wait did not
  87. * time out, and #FALSE if it did.
  88. *
  89. * Has no error conditions. Must succeed if it returns.
  90. */
  91. typedef dbus_bool_t (* DBusCondVarWaitTimeoutFunction) (DBusCondVar *cond,
  92. DBusMutex *mutex,
  93. int timeout_milliseconds);
  94. /** Wakes one waiting thread on a condition variable. Found in #DBusThreadFunctions.
  95. *
  96. * Has no error conditions. Must succeed if it returns.
  97. */
  98. typedef void (* DBusCondVarWakeOneFunction) (DBusCondVar *cond);
  99. /** Wakes all waiting threads on a condition variable. Found in #DBusThreadFunctions.
  100. *
  101. * Has no error conditions. Must succeed if it returns.
  102. */
  103. typedef void (* DBusCondVarWakeAllFunction) (DBusCondVar *cond);
  104. /**
  105. * Flags indicating which functions are present in #DBusThreadFunctions. Used to allow
  106. * the library to detect older callers of dbus_threads_init() if new possible functions
  107. * are added to #DBusThreadFunctions.
  108. */
  109. typedef enum
  110. {
  111. DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK = 1 << 0,
  112. DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK = 1 << 1,
  113. DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK = 1 << 2,
  114. DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK = 1 << 3,
  115. DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK = 1 << 4,
  116. DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK = 1 << 5,
  117. DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK = 1 << 6,
  118. DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK = 1 << 7,
  119. DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK = 1 << 8,
  120. DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK = 1 << 9,
  121. DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK = 1 << 10,
  122. DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK = 1 << 11,
  123. DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK = 1 << 12,
  124. DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK = 1 << 13,
  125. DBUS_THREAD_FUNCTIONS_ALL_MASK = (1 << 14) - 1
  126. } DBusThreadFunctionsMask;
  127. /**
  128. * Functions that must be implemented to make the D-Bus library
  129. * thread-aware.
  130. *
  131. * If you supply both recursive and non-recursive mutexes,
  132. * libdbus will use the non-recursive version for condition variables,
  133. * and the recursive version in other contexts.
  134. *
  135. * The condition variable functions have to work with nonrecursive
  136. * mutexes if you provide those, or with recursive mutexes if you
  137. * don't.
  138. */
  139. typedef struct
  140. {
  141. unsigned int mask; /**< Mask indicating which functions are present. */
  142. DBusMutexNewFunction mutex_new; /**< Function to create a mutex; optional and deprecated. */
  143. DBusMutexFreeFunction mutex_free; /**< Function to free a mutex; optional and deprecated. */
  144. DBusMutexLockFunction mutex_lock; /**< Function to lock a mutex; optional and deprecated. */
  145. DBusMutexUnlockFunction mutex_unlock; /**< Function to unlock a mutex; optional and deprecated. */
  146. DBusCondVarNewFunction condvar_new; /**< Function to create a condition variable */
  147. DBusCondVarFreeFunction condvar_free; /**< Function to free a condition variable */
  148. DBusCondVarWaitFunction condvar_wait; /**< Function to wait on a condition */
  149. DBusCondVarWaitTimeoutFunction condvar_wait_timeout; /**< Function to wait on a condition with a timeout */
  150. DBusCondVarWakeOneFunction condvar_wake_one; /**< Function to wake one thread waiting on the condition */
  151. DBusCondVarWakeAllFunction condvar_wake_all; /**< Function to wake all threads waiting on the condition */
  152. DBusRecursiveMutexNewFunction recursive_mutex_new; /**< Function to create a recursive mutex */
  153. DBusRecursiveMutexFreeFunction recursive_mutex_free; /**< Function to free a recursive mutex */
  154. DBusRecursiveMutexLockFunction recursive_mutex_lock; /**< Function to lock a recursive mutex */
  155. DBusRecursiveMutexUnlockFunction recursive_mutex_unlock; /**< Function to unlock a recursive mutex */
  156. void (* padding1) (void); /**< Reserved for future expansion */
  157. void (* padding2) (void); /**< Reserved for future expansion */
  158. void (* padding3) (void); /**< Reserved for future expansion */
  159. void (* padding4) (void); /**< Reserved for future expansion */
  160. } DBusThreadFunctions;
  161. DBUS_EXPORT
  162. dbus_bool_t dbus_threads_init (const DBusThreadFunctions *functions);
  163. DBUS_EXPORT
  164. dbus_bool_t dbus_threads_init_default (void);
  165. /** @} */
  166. DBUS_END_DECLS
  167. #endif /* DBUS_THREADS_H */