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