From 5434105fcb432bd2549359e6e4d641d57ea6f632 Mon Sep 17 00:00:00 2001 From: Sebastian Wilhelmi Date: Thu, 1 Feb 2001 09:37:28 +0000 Subject: [PATCH] Added g_static_mutex_init to allow initialization of a GStaticMutex, that 2001-02-01 Sebastian Wilhelmi * gthread.c, gthread.h: Added g_static_mutex_init to allow initialization of a GStaticMutex, that can not be initialized with G_STATIC_MUTEX_INIT, for example in allocated structures. * glib/tmpl/threads.sgml, glib/glib-sections.txt: Added documentation for g_static_mutex_init(). WCVS: ---------------------------------------------------------------------- --- ChangeLog | 6 ++++++ ChangeLog.pre-2-0 | 6 ++++++ ChangeLog.pre-2-10 | 6 ++++++ ChangeLog.pre-2-12 | 6 ++++++ ChangeLog.pre-2-2 | 6 ++++++ ChangeLog.pre-2-4 | 6 ++++++ ChangeLog.pre-2-6 | 6 ++++++ ChangeLog.pre-2-8 | 6 ++++++ docs/reference/ChangeLog | 5 +++++ docs/reference/glib/glib-sections.txt | 1 + docs/reference/glib/tmpl/threads.sgml | 30 +++++++++++++++++++++------ glib/gthread.c | 10 +++++++++ glib/gthread.h | 1 + gthread.c | 10 +++++++++ gthread.h | 1 + 15 files changed, 100 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 14de7e9f..ad1a0040 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2001-02-01 Sebastian Wilhelmi + + * gthread.c, gthread.h: Added g_static_mutex_init to allow + initialization of a GStaticMutex, that can not be initialized with + G_STATIC_MUTEX_INIT, for example in allocated structures. + Wed Jan 31 13:46:58 2001 Owen Taylor * acinclude.m4 glib-gettext.m4: Fix problem with --disable-nls. diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 14de7e9f..ad1a0040 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,9 @@ +2001-02-01 Sebastian Wilhelmi + + * gthread.c, gthread.h: Added g_static_mutex_init to allow + initialization of a GStaticMutex, that can not be initialized with + G_STATIC_MUTEX_INIT, for example in allocated structures. + Wed Jan 31 13:46:58 2001 Owen Taylor * acinclude.m4 glib-gettext.m4: Fix problem with --disable-nls. diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 14de7e9f..ad1a0040 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,9 @@ +2001-02-01 Sebastian Wilhelmi + + * gthread.c, gthread.h: Added g_static_mutex_init to allow + initialization of a GStaticMutex, that can not be initialized with + G_STATIC_MUTEX_INIT, for example in allocated structures. + Wed Jan 31 13:46:58 2001 Owen Taylor * acinclude.m4 glib-gettext.m4: Fix problem with --disable-nls. diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 14de7e9f..ad1a0040 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,9 @@ +2001-02-01 Sebastian Wilhelmi + + * gthread.c, gthread.h: Added g_static_mutex_init to allow + initialization of a GStaticMutex, that can not be initialized with + G_STATIC_MUTEX_INIT, for example in allocated structures. + Wed Jan 31 13:46:58 2001 Owen Taylor * acinclude.m4 glib-gettext.m4: Fix problem with --disable-nls. diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 14de7e9f..ad1a0040 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,9 @@ +2001-02-01 Sebastian Wilhelmi + + * gthread.c, gthread.h: Added g_static_mutex_init to allow + initialization of a GStaticMutex, that can not be initialized with + G_STATIC_MUTEX_INIT, for example in allocated structures. + Wed Jan 31 13:46:58 2001 Owen Taylor * acinclude.m4 glib-gettext.m4: Fix problem with --disable-nls. diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 14de7e9f..ad1a0040 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,9 @@ +2001-02-01 Sebastian Wilhelmi + + * gthread.c, gthread.h: Added g_static_mutex_init to allow + initialization of a GStaticMutex, that can not be initialized with + G_STATIC_MUTEX_INIT, for example in allocated structures. + Wed Jan 31 13:46:58 2001 Owen Taylor * acinclude.m4 glib-gettext.m4: Fix problem with --disable-nls. diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 14de7e9f..ad1a0040 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,9 @@ +2001-02-01 Sebastian Wilhelmi + + * gthread.c, gthread.h: Added g_static_mutex_init to allow + initialization of a GStaticMutex, that can not be initialized with + G_STATIC_MUTEX_INIT, for example in allocated structures. + Wed Jan 31 13:46:58 2001 Owen Taylor * acinclude.m4 glib-gettext.m4: Fix problem with --disable-nls. diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 14de7e9f..ad1a0040 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,9 @@ +2001-02-01 Sebastian Wilhelmi + + * gthread.c, gthread.h: Added g_static_mutex_init to allow + initialization of a GStaticMutex, that can not be initialized with + G_STATIC_MUTEX_INIT, for example in allocated structures. + Wed Jan 31 13:46:58 2001 Owen Taylor * acinclude.m4 glib-gettext.m4: Fix problem with --disable-nls. diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index a3858899..57f908d1 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,8 @@ +2001-02-01 Sebastian Wilhelmi + + * glib/tmpl/threads.sgml, glib/glib-sections.txt: Added + documentation for g_static_mutex_init(). + Wed Jan 31 07:14:22 2001 Tim Janik * gobject/Makefile.am: adapt to work with new CVS gtk-doc, leaving the old diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 4fdbf904..53c3c3f7 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -461,6 +461,7 @@ g_mutex_free GStaticMutex G_STATIC_MUTEX_INIT +g_static_mutex_init g_static_mutex_lock g_static_mutex_trylock g_static_mutex_unlock diff --git a/docs/reference/glib/tmpl/threads.sgml b/docs/reference/glib/tmpl/threads.sgml index 10fbee49..9a7cb143 100644 --- a/docs/reference/glib/tmpl/threads.sgml +++ b/docs/reference/glib/tmpl/threads.sgml @@ -370,6 +370,12 @@ not use such constructs in your own programs. One working solution is: #GStaticMutex provides a simpler and safer way of doing this. + +If you want to use a mutex, but your code should also work without +calling g_thread_init() first, you can not use a #GMutex, as +g_mutex_new() requires that. Use a #GStaticMutex instead. + + A #GMutex should only be accessed via the following functions. @@ -473,10 +479,10 @@ Sometimes you would like to dynamically create a mutex. If you don't want to require prior calling to g_thread_init(), because your code should also be usable in non-threaded programs, you are not able to use g_mutex_new() and thus #GMutex, as that requires a prior call to -g_thread_init(). In theses cases you can also use a #GStaticMutex, but -you should remember to free the #GStaticMutex with -g_static_mutex_free() when not needed anymore to free up any -allocated recourses. +g_thread_init(). In theses cases you can also use a #GStaticMutex. It +must be initialized with g_static_mutex_init() before using it and +freed with with g_static_mutex_free() when not needed anymore to free +up any allocated recourses. @@ -520,8 +526,10 @@ functions. -Every #GStaticMutex must be initialized with this macro, before it can -be used. +A #GStaticMutex must be initialized with this macro, before it can be +used. This macro can used be to initialize a variable, but it cannot +be assigned to a variable. In that case you have to use +g_static_mutex_init(). @@ -535,6 +543,16 @@ GStaticMutex my_mutex = G_STATIC_MUTEX_INIT; + + +A #GStaticMutex must be initialized with this function, before it can +be used. Alternatively you can initialize it with +#G_STATIC_MUTEX_INIT. + + +@mutex: a #GStaticMutex to be initialized. + + works like g_mutex_lock(), but for a #GStaticMutex. diff --git a/glib/gthread.c b/glib/gthread.c index 4e94474f..71b56271 100644 --- a/glib/gthread.c +++ b/glib/gthread.c @@ -159,6 +159,16 @@ g_static_mutex_get_mutex_impl (GMutex** mutex) return *mutex; } +void +g_static_mutex_init (GStaticMutex *mutex) +{ + static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; + + g_return_if_fail (mutex); + + memcpy (mutex, &init_mutex, sizeof (GStaticMutex)); +} + void g_static_mutex_free (GStaticMutex* mutex) { diff --git a/glib/gthread.h b/glib/gthread.h index 303c4419..1f86418f 100644 --- a/glib/gthread.h +++ b/glib/gthread.h @@ -227,6 +227,7 @@ void g_thread_set_priority (GThread *thread, g_mutex_trylock (g_static_mutex_get_mutex (mutex)) #define g_static_mutex_unlock(mutex) \ g_mutex_unlock (g_static_mutex_get_mutex (mutex)) +void g_static_mutex_init (GStaticMutex *mutex); void g_static_mutex_free (GStaticMutex *mutex); struct _GStaticPrivate diff --git a/gthread.c b/gthread.c index 4e94474f..71b56271 100644 --- a/gthread.c +++ b/gthread.c @@ -159,6 +159,16 @@ g_static_mutex_get_mutex_impl (GMutex** mutex) return *mutex; } +void +g_static_mutex_init (GStaticMutex *mutex) +{ + static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; + + g_return_if_fail (mutex); + + memcpy (mutex, &init_mutex, sizeof (GStaticMutex)); +} + void g_static_mutex_free (GStaticMutex* mutex) { diff --git a/gthread.h b/gthread.h index 303c4419..1f86418f 100644 --- a/gthread.h +++ b/gthread.h @@ -227,6 +227,7 @@ void g_thread_set_priority (GThread *thread, g_mutex_trylock (g_static_mutex_get_mutex (mutex)) #define g_static_mutex_unlock(mutex) \ g_mutex_unlock (g_static_mutex_get_mutex (mutex)) +void g_static_mutex_init (GStaticMutex *mutex); void g_static_mutex_free (GStaticMutex *mutex); struct _GStaticPrivate -- 2.34.1