| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtDBus module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtCore/qmutex.h> | 
|---|
| 43 | #include <QtCore/qwaitcondition.h> | 
|---|
| 44 |  | 
|---|
| 45 | #include <stdlib.h> | 
|---|
| 46 | #include <qdbus_symbols_p.h> | 
|---|
| 47 |  | 
|---|
| 48 | QT_USE_NAMESPACE | 
|---|
| 49 |  | 
|---|
| 50 | static DBusMutex* mutex_new() | 
|---|
| 51 | { | 
|---|
| 52 | return reinterpret_cast<DBusMutex *>(new QMutex(QMutex::NonRecursive)); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | #if 0 | 
|---|
| 56 | static DBusMutex* recursive_mutex_new() | 
|---|
| 57 | { | 
|---|
| 58 | return reinterpret_cast<DBusMutex *>(new QMutex(QMutex::Recursive)); | 
|---|
| 59 | } | 
|---|
| 60 | #endif | 
|---|
| 61 |  | 
|---|
| 62 | static void mutex_free(DBusMutex *mutex) | 
|---|
| 63 | { | 
|---|
| 64 | delete reinterpret_cast<QMutex *>(mutex); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | static dbus_bool_t mutex_lock(DBusMutex *mutex) | 
|---|
| 68 | { | 
|---|
| 69 | reinterpret_cast<QMutex *>(mutex)->lock(); | 
|---|
| 70 | return true; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | static dbus_bool_t mutex_unlock(DBusMutex *mutex) | 
|---|
| 74 | { | 
|---|
| 75 | reinterpret_cast<QMutex *>(mutex)->unlock(); | 
|---|
| 76 | return true; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | static DBusCondVar* condvar_new() | 
|---|
| 80 | { | 
|---|
| 81 | return reinterpret_cast<DBusCondVar *>(new QWaitCondition); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | static void condvar_free(DBusCondVar *cond) | 
|---|
| 85 | { | 
|---|
| 86 | delete reinterpret_cast<QWaitCondition *>(cond); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | static void condvar_wait(DBusCondVar *cond, DBusMutex *mutex) | 
|---|
| 90 | { | 
|---|
| 91 | reinterpret_cast<QWaitCondition *>(cond)->wait(reinterpret_cast<QMutex *>(mutex)); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | static dbus_bool_t condvar_wait_timeout(DBusCondVar *cond, DBusMutex *mutex, int msec) | 
|---|
| 95 | { | 
|---|
| 96 | return reinterpret_cast<QWaitCondition *>(cond)->wait(reinterpret_cast<QMutex *>(mutex), msec); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | static void condvar_wake_one(DBusCondVar *cond) | 
|---|
| 100 | { | 
|---|
| 101 | reinterpret_cast<QWaitCondition *>(cond)->wakeOne(); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | static void condvar_wake_all(DBusCondVar *cond) | 
|---|
| 105 | { | 
|---|
| 106 | reinterpret_cast<QWaitCondition *>(cond)->wakeAll(); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | QT_BEGIN_NAMESPACE | 
|---|
| 110 |  | 
|---|
| 111 | bool qDBusInitThreads() | 
|---|
| 112 | { | 
|---|
| 113 | // ### Disable the recursive mutex functions. | 
|---|
| 114 | static DBusThreadFunctions fcn = { | 
|---|
| 115 | DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK | | 
|---|
| 116 | DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK | | 
|---|
| 117 | DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK | | 
|---|
| 118 | DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK | | 
|---|
| 119 | DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK | | 
|---|
| 120 | DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK | | 
|---|
| 121 | DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK | | 
|---|
| 122 | DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK | | 
|---|
| 123 | DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK | | 
|---|
| 124 | DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK, | 
|---|
| 125 | #if 0 | 
|---|
| 126 | DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK | | 
|---|
| 127 | DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK | | 
|---|
| 128 | DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK | | 
|---|
| 129 | DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK, | 
|---|
| 130 | #endif | 
|---|
| 131 | mutex_new, | 
|---|
| 132 | mutex_free, | 
|---|
| 133 | mutex_lock, | 
|---|
| 134 | mutex_unlock, | 
|---|
| 135 | condvar_new, | 
|---|
| 136 | condvar_free, | 
|---|
| 137 | condvar_wait, | 
|---|
| 138 | condvar_wait_timeout, | 
|---|
| 139 | condvar_wake_one, | 
|---|
| 140 | condvar_wake_all, | 
|---|
| 141 | #if 0 | 
|---|
| 142 | recursive_mutex_new, | 
|---|
| 143 | mutex_free, | 
|---|
| 144 | mutex_lock, | 
|---|
| 145 | mutex_unlock, | 
|---|
| 146 | #else | 
|---|
| 147 | 0, 0, 0, 0, | 
|---|
| 148 | #endif | 
|---|
| 149 | 0, 0, 0, 0 | 
|---|
| 150 | }; | 
|---|
| 151 |  | 
|---|
| 152 | #if !defined QT_LINKED_LIBDBUS | 
|---|
| 153 | void (*threads_init_default)() = (void (*)())qdbus_resolve_conditionally("dbus_threads_init_default"); | 
|---|
| 154 | void (*threads_init)(DBusThreadFunctions *) = (void (*)(DBusThreadFunctions*))qdbus_resolve_conditionally("dbus_threads_init"); | 
|---|
| 155 |  | 
|---|
| 156 | if (threads_init_default) | 
|---|
| 157 | threads_init_default(); | 
|---|
| 158 | else if (threads_init) | 
|---|
| 159 | threads_init(&fcn); | 
|---|
| 160 | else | 
|---|
| 161 | return false; | 
|---|
| 162 |  | 
|---|
| 163 | return true; | 
|---|
| 164 | #else | 
|---|
| 165 | dbus_threads_init(&fcn); | 
|---|
| 166 |  | 
|---|
| 167 | return true; | 
|---|
| 168 | #endif | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | QT_END_NAMESPACE | 
|---|