1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-pending-call.c Object representing a call in progress.
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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.
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.
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
24 #include "dbus-internals.h"
25 #include "dbus-message-pending.h"
26 #include "dbus-list.h"
27 #include "dbus-threads.h"
28 #include "dbus-test.h"
29 #include "dbus-connection-internal.h"
32 * @defgroup DBusPendingCallInternals DBusPendingCall implementation details
33 * @ingroup DBusInternals
34 * @brief DBusPendingCall private implementation details.
36 * The guts of DBusPendingCall and its methods.
42 * @brief Internals of DBusPendingCall
44 * Object representing a reply message that we're waiting for.
46 struct DBusPendingCall
48 DBusAtomic refcount; /**< reference count */
50 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */
51 void *user_data; /**< user data for function */
52 DBusFreeFunction free_user_data; /**< free the user data */
54 DBusConnection *connection; /**< Connections we're associated with */
55 DBusMessage *reply; /**< Reply (after we've received it) */
56 DBusTimeout *timeout; /**< Timeout */
58 DBusList *timeout_link; /**< Preallocated timeout response */
60 dbus_uint32_t reply_serial; /**< Expected serial of reply */
62 unsigned int completed : 1; /**< TRUE if completed */
63 unsigned int timeout_added : 1; /**< Have added the timeout */
67 * Creates a new pending reply object.
69 * @param connection connection where reply will arrive
70 * @param reply_serial reply serial of the expected reply
71 * @returns a new #DBusPendingCall or #NULL if no memory.
74 _dbus_pending_call_new (DBusConnection *connection,
75 dbus_uint32_t reply_serial)
77 DBusPendingCall *pending;
79 pending = dbus_new (DBusPendingCall, 1);
84 pending->refcount.value = 1;
85 pending->connection = connection;
86 pending->reply_serial = reply_serial;
94 * @defgroup DBusPendingCall DBusPendingCall
96 * @brief Pending reply to a method call message
98 * A DBusPendingCall is an object representing an
99 * expected reply. A #DBusPendingCall can be created
100 * when you send a message that should have a reply.
106 * @typedef DBusPendingCall
108 * Opaque data type representing a message pending.
112 * Increments the reference count on a pending call.
114 * @param pending the pending call object
117 dbus_pending_call_ref (DBusPendingCall *pending)
119 _dbus_return_if_fail (pending != NULL);
121 _dbus_atomic_inc (&pending->refcount);
125 * Decrements the reference count on a pending call,
126 * freeing it if the count reaches 0.
128 * @param pending the pending call object
131 dbus_pending_call_unref (DBusPendingCall *pending)
133 dbus_bool_t last_unref;
135 _dbus_return_if_fail (pending != NULL);
137 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
141 if (pending->free_user_data)
142 (* pending->free_user_data) (pending->user_data);
145 if (pending->connection != NULL)
147 _dbus_connection_pending_destroyed_locked (connection, pending);
148 pending->connection = NULL;
153 dbus_message_unref (pending->reply);
154 pending->reply = NULL;
162 * Sets a notification function to be called when the reply is
163 * received or the pending call times out.
165 * @param pending the pending call
166 * @param function notifier function
167 * @param user_data data to pass to notifier function
168 * @param free_user_data function to free the user data
172 dbus_pending_call_set_notify (DBusPendingCall *pending,
173 DBusPendingCallNotifyFunction function,
175 DBusFreeFunction free_user_data)
177 DBusFreeFunction old_free_func;
180 _dbus_return_if_fail (pending != NULL);
182 _DBUS_LOCK (pending_call);
183 old_free_func = pending->free_user_data;
184 old_user_data = pending->user_data;
186 pending->user_data = user_data;
187 pending->free_user_data = free_user_data;
188 pending->function = function;
189 _DBUS_UNLOCK (pending_call);
192 (* old_free_func) (old_user_data);
197 #ifdef DBUS_BUILD_TESTS
198 static DBusPendingResult
199 test_pending (DBusPendingCall *pending,
200 DBusConnection *connection,
201 DBusMessage *message,
204 return DBUS_PENDING_RESULT_NOT_YET_HANDLED;
208 free_test_data (void *data)
214 * @ingroup DBusPendingCallInternals
215 * Unit test for DBusPendingCall.
217 * @returns #TRUE on success.
220 _dbus_pending_call_test (const char *test_data_dir)
225 #endif /* DBUS_BUILD_TESTS */