mirror of
https://gitlab.freedesktop.org/dbus/dbus.git
synced 2025-12-22 09:00:17 +01:00
dbus_threads_init_default, dbus_threads_init: be safe to call at any time
On Unix, we use a pthreads mutex, which can be allocated and initialized in global memory. On Windows, we use a CRITICAL_SECTION, together with a call to InitializeCriticalSection() from the constructor of a global static C++ object (thanks to Ralf Habacker for suggesting this approach). Bug: https://bugs.freedesktop.org/show_bug.cgi?id=54972 Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk> Reviewed-by: Alban Crequy <alban.crequy@collabora.co.uk> Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
This commit is contained in:
parent
863c989bb6
commit
17a23d08b5
9 changed files with 139 additions and 7 deletions
|
|
@ -186,6 +186,7 @@ set (DBUS_UTIL_HEADERS
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
set (DBUS_SHARED_SOURCES ${DBUS_SHARED_SOURCES}
|
set (DBUS_SHARED_SOURCES ${DBUS_SHARED_SOURCES}
|
||||||
${DBUS_DIR}/dbus-file-win.c
|
${DBUS_DIR}/dbus-file-win.c
|
||||||
|
${DBUS_DIR}/dbus-init-win.cpp
|
||||||
${DBUS_DIR}/dbus-sysdeps-win.c
|
${DBUS_DIR}/dbus-sysdeps-win.c
|
||||||
${DBUS_DIR}/dbus-pipe-win.c
|
${DBUS_DIR}/dbus-pipe-win.c
|
||||||
${DBUS_DIR}/dbus-sysdeps-thread-win.c
|
${DBUS_DIR}/dbus-sysdeps-thread-win.c
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,7 @@ endif
|
||||||
DBUS_SHARED_arch_sources = \
|
DBUS_SHARED_arch_sources = \
|
||||||
$(wince_source) \
|
$(wince_source) \
|
||||||
dbus-file-win.c \
|
dbus-file-win.c \
|
||||||
|
dbus-init-win.cpp \
|
||||||
dbus-pipe-win.c \
|
dbus-pipe-win.c \
|
||||||
dbus-sockets-win.h \
|
dbus-sockets-win.h \
|
||||||
dbus-sysdeps-win.c \
|
dbus-sysdeps-win.c \
|
||||||
|
|
|
||||||
52
dbus/dbus-init-win.cpp
Normal file
52
dbus/dbus-init-win.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
* dbus-init-win.cpp - once-per-process initialization
|
||||||
|
*
|
||||||
|
* Copyright © 2013 Intel Corporation
|
||||||
|
*
|
||||||
|
* Licensed under the Academic Free License version 2.1
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#include "dbus-sysdeps-win.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
class DBusInternalInit
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DBusInternalInit ()
|
||||||
|
{
|
||||||
|
_dbus_threads_windows_init_global ();
|
||||||
|
}
|
||||||
|
|
||||||
|
void must_not_be_omitted ()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static DBusInternalInit init;
|
||||||
|
|
||||||
|
extern "C" void
|
||||||
|
_dbus_threads_windows_ensure_ctor_linked ()
|
||||||
|
{
|
||||||
|
/* Do nothing significant, just ensure that the global initializer gets
|
||||||
|
* linked in. */
|
||||||
|
init.must_not_be_omitted ();
|
||||||
|
}
|
||||||
|
|
@ -26,6 +26,7 @@
|
||||||
#include "dbus-internals.h"
|
#include "dbus-internals.h"
|
||||||
#include "dbus-sysdeps.h"
|
#include "dbus-sysdeps.h"
|
||||||
#include "dbus-list.h"
|
#include "dbus-list.h"
|
||||||
|
#include "dbus-threads.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -890,7 +891,13 @@ dbus_shutdown (void)
|
||||||
dbus_free (c);
|
dbus_free (c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* We wrap this in the thread-initialization lock because
|
||||||
|
* dbus_threads_init() uses the current generation to tell whether
|
||||||
|
* we're initialized, so we need to make sure that un-initializing
|
||||||
|
* propagates into all threads. */
|
||||||
|
_dbus_threads_lock_platform_specific ();
|
||||||
_dbus_current_generation += 1;
|
_dbus_current_generation += 1;
|
||||||
|
_dbus_threads_unlock_platform_specific ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @} */ /** End of public API docs block */
|
/** @} */ /** End of public API docs block */
|
||||||
|
|
|
||||||
|
|
@ -286,3 +286,17 @@ _dbus_threads_init_platform_specific (void)
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
|
void
|
||||||
|
_dbus_threads_lock_platform_specific (void)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock (&init_mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
_dbus_threads_unlock_platform_specific (void)
|
||||||
|
{
|
||||||
|
pthread_mutex_unlock (&init_mutex);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,21 @@
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
|
static dbus_bool_t global_init_done = FALSE;
|
||||||
|
static CRITICAL_SECTION init_lock;
|
||||||
|
|
||||||
|
/* Called from C++ code in dbus-init-win.cpp. */
|
||||||
|
void
|
||||||
|
_dbus_threads_windows_init_global (void)
|
||||||
|
{
|
||||||
|
/* this ensures that the object that acts as our global constructor
|
||||||
|
* actually gets linked in when we're linked statically */
|
||||||
|
_dbus_threads_windows_ensure_ctor_linked ();
|
||||||
|
|
||||||
|
InitializeCriticalSection (&init_lock);
|
||||||
|
global_init_done = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
struct DBusCondVar {
|
struct DBusCondVar {
|
||||||
DBusList *list; /**< list thread-local-stored events waiting on the cond variable */
|
DBusList *list; /**< list thread-local-stored events waiting on the cond variable */
|
||||||
CRITICAL_SECTION lock; /**< lock protecting the list */
|
CRITICAL_SECTION lock; /**< lock protecting the list */
|
||||||
|
|
@ -272,3 +287,16 @@ _dbus_threads_init_platform_specific (void)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
_dbus_threads_lock_platform_specific (void)
|
||||||
|
{
|
||||||
|
_dbus_assert (global_init_done);
|
||||||
|
EnterCriticalSection (&init_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
_dbus_threads_unlock_platform_specific (void)
|
||||||
|
{
|
||||||
|
_dbus_assert (global_init_done);
|
||||||
|
LeaveCriticalSection (&init_lock);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,9 @@ dbus_bool_t _dbus_get_config_file_name(DBusString *config_file,
|
||||||
|
|
||||||
dbus_bool_t _dbus_get_install_root(char *prefix, int len);
|
dbus_bool_t _dbus_get_install_root(char *prefix, int len);
|
||||||
|
|
||||||
|
void _dbus_threads_windows_init_global (void);
|
||||||
|
void _dbus_threads_windows_ensure_ctor_linked (void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** @} end of sysdeps-win.h */
|
/** @} end of sysdeps-win.h */
|
||||||
|
|
|
||||||
|
|
@ -520,6 +520,18 @@ dbus_bool_t _dbus_read_local_machine_uuid (DBusGUID *machine_id,
|
||||||
*/
|
*/
|
||||||
dbus_bool_t _dbus_threads_init_platform_specific (void);
|
dbus_bool_t _dbus_threads_init_platform_specific (void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lock a static mutex used to protect _dbus_threads_init_platform_specific().
|
||||||
|
*
|
||||||
|
* On Windows, this is currently unimplemented and does nothing.
|
||||||
|
*/
|
||||||
|
void _dbus_threads_lock_platform_specific (void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undo _dbus_threads_lock_platform_specific().
|
||||||
|
*/
|
||||||
|
void _dbus_threads_unlock_platform_specific (void);
|
||||||
|
|
||||||
dbus_bool_t _dbus_split_paths_and_append (DBusString *dirs,
|
dbus_bool_t _dbus_split_paths_and_append (DBusString *dirs,
|
||||||
const char *suffix,
|
const char *suffix,
|
||||||
DBusList **dir_list);
|
DBusList **dir_list);
|
||||||
|
|
|
||||||
|
|
@ -581,15 +581,24 @@ init_locks (void)
|
||||||
dbus_bool_t
|
dbus_bool_t
|
||||||
dbus_threads_init (const DBusThreadFunctions *functions)
|
dbus_threads_init (const DBusThreadFunctions *functions)
|
||||||
{
|
{
|
||||||
|
_dbus_threads_lock_platform_specific ();
|
||||||
|
|
||||||
if (thread_init_generation == _dbus_current_generation)
|
if (thread_init_generation == _dbus_current_generation)
|
||||||
return TRUE;
|
{
|
||||||
|
_dbus_threads_unlock_platform_specific ();
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
if (!_dbus_threads_init_platform_specific() ||
|
if (!_dbus_threads_init_platform_specific() ||
|
||||||
!init_locks ())
|
!init_locks ())
|
||||||
return FALSE;
|
{
|
||||||
|
_dbus_threads_unlock_platform_specific ();
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
thread_init_generation = _dbus_current_generation;
|
thread_init_generation = _dbus_current_generation;
|
||||||
|
|
||||||
|
_dbus_threads_unlock_platform_specific ();
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -600,11 +609,16 @@ dbus_threads_init (const DBusThreadFunctions *functions)
|
||||||
/**
|
/**
|
||||||
* Initializes threads. If this function is not called, the D-Bus
|
* Initializes threads. If this function is not called, the D-Bus
|
||||||
* library will not lock any data structures. If it is called, D-Bus
|
* library will not lock any data structures. If it is called, D-Bus
|
||||||
* will do locking, at some cost in efficiency. Note that this
|
* will do locking, at some cost in efficiency.
|
||||||
* function must be called BEFORE the second thread is started.
|
|
||||||
*
|
*
|
||||||
* It's safe to call dbus_threads_init_default() as many times as you
|
* Since D-Bus 1.7 it is safe to call this function from any thread,
|
||||||
* want, but only the first time will have an effect.
|
* any number of times (but it must be called before any other
|
||||||
|
* libdbus API is used).
|
||||||
|
*
|
||||||
|
* In D-Bus 1.6 or older, this function must be called in the main thread
|
||||||
|
* before any other thread starts. As a result, it is not sufficient to
|
||||||
|
* call this function in a library or plugin, unless the library or plugin
|
||||||
|
* imposes a similar requirement on its callers.
|
||||||
*
|
*
|
||||||
* dbus_shutdown() reverses the effects of this function when it
|
* dbus_shutdown() reverses the effects of this function when it
|
||||||
* resets all global state in libdbus.
|
* resets all global state in libdbus.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue