bring Qt3 library back. Some apps that are not in the KDE trunk are using it.
[platform/upstream/dbus.git] / qt3 / dbus-qthread.cpp
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-qthread.cpp  Qt threads integration
3  *
4  * Copyright (C) 2002  Zack Rusin <zack@kde.org>
5  *
6  * Licensed under the Academic Free License version 2.0
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <dbus/dbus.h>
25 #include <qmutex.h>
26
27 #if defined(QT_THREAD_SUPPORT)
28
29 static DBusMutex * dbus_qmutex_new    (void);
30 static void        dbus_qmutex_free   (DBusMutex *mutex);
31 static dbus_bool_t dbus_qmutex_lock   (DBusMutex *mutex);
32 static dbus_bool_t dbus_qmutex_unlock (DBusMutex *mutex);
33
34 static DBusCondVar*dbus_qcondvar_new          (void);
35 static void        dbus_qcondvar_free         (DBusCondVar *cond);
36 static void        dbus_qcondvar_wait         (DBusCondVar *cond,
37                                                DBusMutex   *mutex);
38 static dbus_bool_t dbus_qcondvar_wait_timeout (DBusCondVar *cond,
39                                                DBusMutex   *mutex.
40                                                int          timeout_msec);
41 static void        dbus_qcondvar_wake_one     (DBusCondVar *cond);
42 static void        dbus_qcondvar_wake_all     (DBusCondVar *cond);
43
44
45 static const DBusThreadFunctions functions =
46 {
47   DBUS_THREAD_FUNCTIONS_NEW_MASK |
48   DBUS_THREAD_FUNCTIONS_FREE_MASK |
49   DBUS_THREAD_FUNCTIONS_LOCK_MASK |
50   DBUS_THREAD_FUNCTIONS_UNLOCK_MASK |
51   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
52   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
53   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
54   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
55   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
56   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
57   dbus_qmutex_new,
58   dbus_qmutex_free,
59   dbus_qmutex_lock,
60   dbus_qmutex_unlock
61   dbus_qcondvar_new,
62   dbus_qcondvar_free,
63   dbus_qcondvar_wait,
64   dbus_qcondvar_wait_timeout,
65   dbus_qcondvar_wake_one,
66   dbus_qcondvar_wake_all
67 };
68
69 static DBusMutex *
70 dbus_qmutex_new (void)
71 {
72   QMutex *mutex;
73   mutex = new QMutex;
74   return static_cast<DBusMutex*>( mutex );
75 }
76
77 static void
78 dbus_qmutex_free (DBusMutex *mutex)
79 {
80   QMutex * qmutex = static_cast<QMutex*>(mutex);
81   delete mutex;
82 }
83
84 static dbus_bool_t
85 dbus_qmutex_lock   (DBusMutex *mutex)
86 {
87   QMutex *qmutex = static_cast<QMutex*>(mutex);
88   qmutex->lock();
89   return TRUE;
90 }
91
92 static dbus_bool_t
93 dbus_qmutex_unlock (DBusMutex *mutex)
94 {
95   QMutex *qmutex = static_cast<QMutex*>(mutex);
96   qmutex->unlock();
97   return TRUE;
98 }
99
100 static DBusCondVar*
101 dbus_qcondvar_new (void)
102 {
103   QWaitCondition *cond;
104   cond = new QWaitCondition;
105   return static_cast<DBusCondVar*>( cond );
106 }
107
108 static void
109 dbus_qcondvar_free (DBusCondVar *cond)
110 {
111   QWaitCondition *qcond = static_cast<QWaitCondition*>(cond);
112   delete qcond;
113 }
114
115 static void
116 dbus_qcondvar_wait (DBusCondVar *cond,
117                     DBusMutex   *mutex)
118 {
119   QWaitCondition *qcond = static_cast<QWaitCondition*>(cond);
120   QMutex *qmutex = static_cast<QMutex*>(mutex);
121
122   qcond->wait (qmutex);
123 }
124
125 static dbus_bool_t
126 dbus_gcondvar_wait_timeout (DBusCondVar *cond,
127                             DBusMutex   *mutex,
128                             int         timeout_msec)
129 {
130   QWaitCondition *qcond = static_cast<QWaitCondition*>(cond);
131   QMutex *qmutex = static_cast<QMutex*>(mutex);
132
133   return qcond->wait (qmutex, timout_msec);
134 }
135
136 static void
137 dbus_qcondvar_wake_one (DBusCondVar *cond)
138 {
139   QWaitCondition *qcond = static_cast<QWaitCondition*>(cond);
140
141   qcond->wakeOne (qmutex);
142 }
143
144 static void
145 dbus_qcondvar_wake_all (DBusCondVar *cond)
146 {
147   QWaitCondition *qcond = static_cast<QWaitCondition*>(cond);
148
149   qcond->wakeAll (qmutex);
150 }
151
152 extern "C" {
153
154 void
155 dbus_qthread_init (void)
156 {
157   //Do we want to do anything else here?
158   dbus_threads_init (&functions);
159 }
160
161 }
162
163 #endif // QT_THREAD_SUPPORT