1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-threads.h D-BUS threads handling
4 * Copyright (C) 2002 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
23 #include "dbus-threads.h"
24 #include "dbus-internals.h"
26 static DBusThreadFunctions thread_functions =
29 NULL, NULL, NULL, NULL,
31 NULL, NULL, NULL, NULL,
32 NULL, NULL, NULL, NULL
36 * @defgroup DBusThreads Thread functions
38 * @brief dbus_threads_init(), dbus_mutex_lock(), etc.
40 * Functions and macros related to threads and thread locks.
46 * Creates a new mutex using the function supplied to dbus_threads_init(),
47 * or creates a no-op mutex if threads are not initialized.
48 * May return #NULL even if threads are initialized, indicating
51 * @returns new mutex or #NULL
56 if (thread_functions.mutex_new)
57 return (* thread_functions.mutex_new) ();
63 * Frees a mutex created with dbus_mutex_new(); does
64 * nothing if passed a #NULL pointer.
67 dbus_mutex_free (DBusMutex *mutex)
69 if (mutex && thread_functions.mutex_free)
70 (* thread_functions.mutex_free) (mutex);
74 * Locks a mutex. Does nothing if passed a #NULL pointer.
75 * Locks are not recursive.
77 * @returns #TRUE on success
80 dbus_mutex_lock (DBusMutex *mutex)
82 if (mutex && thread_functions.mutex_lock)
83 return (* thread_functions.mutex_lock) (mutex);
89 * Unlocks a mutex. Does nothing if passed a #NULL pointer.
91 * @returns #TRUE on success
94 dbus_mutex_unlock (DBusMutex *mutex)
96 if (mutex && thread_functions.mutex_unlock)
97 return (* thread_functions.mutex_unlock) (mutex);
103 * Initializes threads. If this function is not called,
104 * the D-BUS library will not lock any data structures.
105 * If it is called, D-BUS will do locking, at some cost
106 * in efficiency. Note that this function must be called
107 * BEFORE using any other D-BUS functions.
109 * @param functions functions for using threads
112 dbus_threads_init (const DBusThreadFunctions *functions)
114 _dbus_assert (functions != NULL);
116 /* these base functions are required. Future additions to
117 * DBusThreadFunctions may be optional.
119 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_NEW_MASK);
120 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_FREE_MASK);
121 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_LOCK_MASK);
122 _dbus_assert (functions->mask & DBUS_THREAD_FUNCTIONS_UNLOCK_MASK);
123 _dbus_assert (functions->mutex_new != NULL);
124 _dbus_assert (functions->mutex_free != NULL);
125 _dbus_assert (functions->mutex_lock != NULL);
126 _dbus_assert (functions->mutex_unlock != NULL);
128 /* Check that all bits in the mask actually are valid mask bits.
129 * ensures people won't write code that breaks when we add
132 _dbus_assert ((functions->mask & ~DBUS_THREAD_FUNCTIONS_ALL_MASK) == 0);
134 if (thread_functions.mask != 0)
136 _dbus_warn ("dbus_threads_init() may only be called one time\n");
140 thread_functions.mutex_new = functions->mutex_new;
141 thread_functions.mutex_free = functions->mutex_free;
142 thread_functions.mutex_lock = functions->mutex_lock;
143 thread_functions.mutex_unlock = functions->mutex_unlock;
145 thread_functions.mask = functions->mask;