1/* GLIB - Library of useful routines for C programming 2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald 3 * 4 * gthread.c: MT safety related functions 5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe 6 * Owen Taylor 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 12 * 13 * This library 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 GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the 20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 21 * Boston, MA 02111-1307, USA. 22 */ 23 24/* 25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS 26 * file for a list of people on the GLib Team. See the ChangeLog 27 * files for a list of changes. These files are distributed with 28 * GLib at ftp://ftp.gtk.org/pub/gtk/. 29 */ 30 31/* 32 * MT safe 33 */ 34 35/* implement gthread.h's inline functions */ 36#define G_IMPLEMENT_INLINES 1 37#define __G_THREAD_C__ 38 39#include "config.h" 40 41#include "glib.h" 42#include "gthreadprivate.h" 43 44#ifdef HAVE_UNISTD_H 45#include <unistd.h> 46#endif 47 48#ifndef G_OS_WIN32 49#include <sys/time.h> 50#include <time.h> 51#else 52#include <windows.h> 53#endif /* G_OS_WIN32 */ 54 55#include <string.h> 56 57#include "galias.h" 58 59GQuark 60g_thread_error_quark (void) 61{ 62 return g_quark_from_static_string ("g_thread_error"); 63} 64 65/* Keep this in sync with GRealThread in gmain.c! */ 66typedef struct _GRealThread GRealThread; 67struct _GRealThread 68{ 69 GThread thread; 70 gpointer private_data; 71 GRealThread *next; 72 gpointer retval; 73 GSystemThread system_thread; 74}; 75 76typedef struct _GStaticPrivateNode GStaticPrivateNode; 77struct _GStaticPrivateNode 78{ 79 gpointer data; 80 GDestroyNotify destroy; 81}; 82 83static void g_thread_cleanup (gpointer data); 84static void g_thread_fail (void); 85static guint64 gettime (void); 86 87guint64 (*g_thread_gettime) (void) = gettime; 88 89/* Global variables */ 90 91static GSystemThread zero_thread; /* This is initialized to all zero */ 92gboolean g_thread_use_default_impl = TRUE; 93gboolean g_threads_got_initialized = FALSE; 94 95GThreadFunctions g_thread_functions_for_glib_use = { 96 (GMutex*(*)())g_thread_fail, /* mutex_new */ 97 NULL, /* mutex_lock */ 98 NULL, /* mutex_trylock */ 99 NULL, /* mutex_unlock */ 100 NULL, /* mutex_free */ 101 (GCond*(*)())g_thread_fail, /* cond_new */ 102 NULL, /* cond_signal */ 103 NULL, /* cond_broadcast */ 104 NULL, /* cond_wait */ 105 NULL, /* cond_timed_wait */ 106 NULL, /* cond_free */ 107 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */ 108 NULL, /* private_get */ 109 NULL, /* private_set */ 110 (void(*)(GThreadFunc, gpointer, gulong, 111 gboolean, gboolean, GThreadPriority, 112 gpointer, GError**))g_thread_fail, /* thread_create */ 113 NULL, /* thread_yield */ 114 NULL, /* thread_join */ 115 NULL, /* thread_exit */ 116 NULL, /* thread_set_priority */ 117 NULL, /* thread_self */ 118 NULL /* thread_equal */ 119}; 120 121/* Local data */ 122 123static GMutex *g_once_mutex = NULL; 124static GCond *g_once_cond = NULL; 125static GPrivate *g_thread_specific_private = NULL; 126static GRealThread *g_thread_all_threads = NULL; 127static GSList *g_thread_free_indeces = NULL; 128static GSList* g_once_init_list = NULL; 129 130G_LOCK_DEFINE_STATIC (g_thread); 131 132#ifdef G_THREADS_ENABLED 133/* This must be called only once, before any threads are created. 134 * It will only be called from g_thread_init() in -lgthread. 135 */ 136void 137g_thread_init_glib (void) 138{ 139 /* We let the main thread (the one that calls g_thread_init) inherit 140 * the static_private data set before calling g_thread_init 141 */ 142 GRealThread* main_thread = (GRealThread*) g_thread_self (); 143 144 /* mutex and cond creation works without g_threads_got_initialized */ 145 g_once_mutex = g_mutex_new (); 146 g_once_cond = g_cond_new (); 147 148 /* we may only create mutex and cond in here */ 149 _g_mem_thread_init_noprivate_nomessage (); 150 151 /* setup the basic threading system */ 152 g_threads_got_initialized = TRUE; 153 g_thread_specific_private = g_private_new (g_thread_cleanup); 154 g_private_set (g_thread_specific_private, main_thread); 155 G_THREAD_UF (thread_self, (&main_thread->system_thread)); 156 157 /* complete memory system initialization, g_private_*() works now */ 158 _g_slice_thread_init_nomessage (); 159 160 /* accomplish log system initialization to enable messaging */ 161 _g_messages_thread_init_nomessage (); 162 163 /* we may run full-fledged initializers from here */ 164 _g_atomic_thread_init (); 165 _g_convert_thread_init (); 166 _g_rand_thread_init (); 167 _g_main_thread_init (); 168 _g_utils_thread_init (); 169#ifdef G_OS_WIN32 170 _g_win32_thread_init (); 171#endif 172} 173#endif /* G_THREADS_ENABLED */ 174 175gpointer 176g_once_impl (GOnce *once, 177 GThreadFunc func, 178 gpointer arg) 179{ 180 g_mutex_lock (g_once_mutex); 181 182 while (once->status == G_ONCE_STATUS_PROGRESS) 183 g_cond_wait (g_once_cond, g_once_mutex); 184 185 if (once->status != G_ONCE_STATUS_READY) 186 { 187 once->status = G_ONCE_STATUS_PROGRESS; 188 g_mutex_unlock (g_once_mutex); 189 190 once->retval = func (arg); 191 192 g_mutex_lock (g_once_mutex); 193 once->status = G_ONCE_STATUS_READY; 194 g_cond_broadcast (g_once_cond); 195 } 196 197 g_mutex_unlock (g_once_mutex); 198 199 return once->retval; 200} 201 202gboolean 203g_once_init_enter_impl (volatile gsize *value_location) 204{ 205 gboolean need_init = FALSE; 206 g_mutex_lock (g_once_mutex); 207 if (g_atomic_pointer_get (value_location) == NULL) 208 { 209 if (!g_slist_find (g_once_init_list, (void*) value_location)) 210 { 211 need_init = TRUE; 212 g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location); 213 } 214 else 215 do 216 g_cond_wait (g_once_cond, g_once_mutex); 217 while (g_slist_find (g_once_init_list, (void*) value_location)); 218 } 219 g_mutex_unlock (g_once_mutex); 220 return need_init; 221} 222 223void 224g_once_init_leave (volatile gsize *value_location, 225 gsize initialization_value) 226{ 227 g_return_if_fail (g_atomic_pointer_get (value_location) == NULL); 228 g_return_if_fail (initialization_value != 0); 229 g_return_if_fail (g_once_init_list != NULL); 230 231 g_atomic_pointer_set ((void**)value_location, (void*) initialization_value); 232 g_mutex_lock (g_once_mutex); 233 g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location); 234 g_cond_broadcast (g_once_cond); 235 g_mutex_unlock (g_once_mutex); 236} 237 238void 239g_static_mutex_init (GStaticMutex *mutex) 240{ 241 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; 242 243 g_return_if_fail (mutex); 244 245 *mutex = init_mutex; 246} 247 248GMutex * 249g_static_mutex_get_mutex_impl (GMutex** mutex) 250{ 251 if (!g_thread_supported ()) 252 return NULL; 253 254 g_assert (g_once_mutex); 255 256 g_mutex_lock (g_once_mutex); 257 258 if (!(*mutex)) 259 g_atomic_pointer_set (mutex, g_mutex_new()); 260 261 g_mutex_unlock (g_once_mutex); 262 263 return *mutex; 264} 265 266void 267g_static_mutex_free (GStaticMutex* mutex) 268{ 269 GMutex **runtime_mutex; 270 271 g_return_if_fail (mutex); 272 273 /* The runtime_mutex is the first (or only) member of GStaticMutex, 274 * see both versions (of glibconfig.h) in configure.in. Note, that 275 * this variable is NULL, if g_thread_init() hasn't been called or 276 * if we're using the default thread implementation and it provides 277 * static mutexes. */ 278 runtime_mutex = ((GMutex**)mutex); 279 280 if (*runtime_mutex) 281 g_mutex_free (*runtime_mutex); 282 283 *runtime_mutex = NULL; 284} 285 286void 287g_static_rec_mutex_init (GStaticRecMutex *mutex) 288{ 289 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT; 290 291 g_return_if_fail (mutex); 292 293 *mutex = init_mutex; 294} 295 296void 297g_static_rec_mutex_lock (GStaticRecMutex* mutex) 298{ 299 GSystemThread self; 300 301 g_return_if_fail (mutex); 302 303 if (!g_thread_supported ()) 304 return; 305 306 G_THREAD_UF (thread_self, (&self)); 307 308 if (g_system_thread_equal (self, mutex->owner)) 309 { 310 mutex->depth++; 311 return; 312 } 313 g_static_mutex_lock (&mutex->mutex); 314 g_system_thread_assign (mutex->owner, self); 315 mutex->depth = 1; 316} 317 318gboolean 319g_static_rec_mutex_trylock (GStaticRecMutex* mutex) 320{ 321 GSystemThread self; 322 323 g_return_val_if_fail (mutex, FALSE); 324 325 if (!g_thread_supported ()) 326 return TRUE; 327 328 G_THREAD_UF (thread_self, (&self)); 329 330 if (g_system_thread_equal (self, mutex->owner)) 331 { 332 mutex->depth++; 333 return TRUE; 334 } 335 336 if (!g_static_mutex_trylock (&mutex->mutex)) 337 return FALSE; 338 339 g_system_thread_assign (mutex->owner, self); 340 mutex->depth = 1; 341 return TRUE; 342} 343 344void 345g_static_rec_mutex_unlock (GStaticRecMutex* mutex) 346{ 347 g_return_if_fail (mutex); 348 349 if (!g_thread_supported ()) 350 return; 351 352 if (mutex->depth > 1) 353 { 354 mutex->depth--; 355 return; 356 } 357 g_system_thread_assign (mutex->owner, zero_thread); 358 g_static_mutex_unlock (&mutex->mutex); 359} 360 361void 362g_static_rec_mutex_lock_full (GStaticRecMutex *mutex, 363 guint depth) 364{ 365 GSystemThread self; 366 g_return_if_fail (mutex); 367 368 if (!g_thread_supported ()) 369 return; 370 371 if (depth == 0) 372 return; 373 374 G_THREAD_UF (thread_self, (&self)); 375 376 if (g_system_thread_equal (self, mutex->owner)) 377 { 378 mutex->depth += depth; 379 return; 380 } 381 g_static_mutex_lock (&mutex->mutex); 382 g_system_thread_assign (mutex->owner, self); 383 mutex->depth = depth; 384} 385 386guint 387g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex) 388{ 389 guint depth; 390 391 g_return_val_if_fail (mutex, 0); 392 393 if (!g_thread_supported ()) 394 return 1; 395 396 depth = mutex->depth; 397 398 g_system_thread_assign (mutex->owner, zero_thread); 399 mutex->depth = 0; 400 g_static_mutex_unlock (&mutex->mutex); 401 402 return depth; 403} 404 405void 406g_static_rec_mutex_free (GStaticRecMutex *mutex) 407{ 408 g_return_if_fail (mutex); 409 410 g_static_mutex_free (&mutex->mutex); 411} 412 413void 414g_static_private_init (GStaticPrivate *private_key) 415{ 416 private_key->index = 0; 417} 418 419gpointer 420g_static_private_get (GStaticPrivate *private_key) 421{ 422 GRealThread *self = (GRealThread*) g_thread_self (); 423 GArray *array; 424 425 array = self->private_data; 426 if (!array) 427 return NULL; 428 429 if (!private_key->index) 430 return NULL; 431 else if (private_key->index <= array->len) 432 return g_array_index (array, GStaticPrivateNode, 433 private_key->index - 1).data; 434 else 435 return NULL; 436} 437 438void 439g_static_private_set (GStaticPrivate *private_key, 440 gpointer data, 441 GDestroyNotify notify) 442{ 443 GRealThread *self = (GRealThread*) g_thread_self (); 444 GArray *array; 445 static guint next_index = 0; 446 GStaticPrivateNode *node; 447 448 array = self->private_data; 449 if (!array) 450 { 451 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode)); 452 self->private_data = array; 453 } 454 455 if (!private_key->index) 456 { 457 G_LOCK (g_thread); 458 459 if (!private_key->index) 460 { 461 if (g_thread_free_indeces) 462 { 463 private_key->index = 464 GPOINTER_TO_UINT (g_thread_free_indeces->data); 465 g_thread_free_indeces = 466 g_slist_delete_link (g_thread_free_indeces, 467 g_thread_free_indeces); 468 } 469 else 470 private_key->index = ++next_index; 471 } 472 473 G_UNLOCK (g_thread); 474 } 475 476 if (private_key->index > array->len) 477 g_array_set_size (array, private_key->index); 478 479 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1); 480 if (node->destroy) 481 { 482 gpointer ddata = node->data; 483 GDestroyNotify ddestroy = node->destroy; 484 485 node->data = data; 486 node->destroy = notify; 487 488 ddestroy (ddata); 489 } 490 else 491 { 492 node->data = data; 493 node->destroy = notify; 494 } 495} 496 497void 498g_static_private_free (GStaticPrivate *private_key) 499{ 500 guint idx = private_key->index; 501 GRealThread *thread; 502 503 if (!idx) 504 return; 505 506 private_key->index = 0; 507 508 G_LOCK (g_thread); 509 510 thread = g_thread_all_threads; 511 while (thread) 512 { 513 GArray *array = thread->private_data; 514 thread = thread->next; 515 516 if (array && idx <= array->len) 517 { 518 GStaticPrivateNode *node = &g_array_index (array, 519 GStaticPrivateNode, 520 idx - 1); 521 gpointer ddata = node->data; 522 GDestroyNotify ddestroy = node->destroy; 523 524 node->data = NULL; 525 node->destroy = NULL; 526 527 if (ddestroy) 528 { 529 G_UNLOCK (g_thread); 530 ddestroy (ddata); 531 G_LOCK (g_thread); 532 } 533 } 534 } 535 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces, 536 GUINT_TO_POINTER (idx)); 537 G_UNLOCK (g_thread); 538} 539 540static void 541g_thread_cleanup (gpointer data) 542{ 543 if (data) 544 { 545 GRealThread* thread = data; 546 if (thread->private_data) 547 { 548 GArray* array = thread->private_data; 549 guint i; 550 551 for (i = 0; i < array->len; i++ ) 552 { 553 GStaticPrivateNode *node = 554 &g_array_index (array, GStaticPrivateNode, i); 555 if (node->destroy) 556 node->destroy (node->data); 557 } 558 g_array_free (array, TRUE); 559 } 560 561 /* We only free the thread structure, if it isn't joinable. If 562 it is, the structure is freed in g_thread_join */ 563 if (!thread->thread.joinable) 564 { 565 GRealThread *t, *p; 566 567 G_LOCK (g_thread); 568 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next) 569 { 570 if (t == thread) 571 { 572 if (p) 573 p->next = t->next; 574 else 575 g_thread_all_threads = t->next; 576 break; 577 } 578 } 579 G_UNLOCK (g_thread); 580 581 /* Just to make sure, this isn't used any more */ 582 g_system_thread_assign (thread->system_thread, zero_thread); 583 g_free (thread); 584 } 585 } 586} 587 588static void 589g_thread_fail (void) 590{ 591 g_error ("The thread system is not yet initialized."); 592} 593 594#define G_NSEC_PER_SEC 1000000000 595 596static guint64 597gettime (void) 598{ 599#ifdef G_OS_WIN32 600 guint64 v; 601 602 /* Returns 100s of nanoseconds since start of 1601 */ 603 GetSystemTimeAsFileTime ((FILETIME *)&v); 604 605 /* Offset to Unix epoch */ 606 v -= G_GINT64_CONSTANT (116444736000000000); 607 /* Convert to nanoseconds */ 608 v *= 100; 609 610 return v; 611#else 612 struct timeval tv; 613 614 gettimeofday (&tv, NULL); 615 616 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC); 617#endif 618} 619 620static gpointer 621g_thread_create_proxy (gpointer data) 622{ 623 GRealThread* thread = data; 624 625 g_assert (data); 626 627 /* This has to happen before G_LOCK, as that might call g_thread_self */ 628 g_private_set (g_thread_specific_private, data); 629 630 /* the lock makes sure, that thread->system_thread is written, 631 before thread->thread.func is called. See g_thread_create. */ 632 G_LOCK (g_thread); 633 G_UNLOCK (g_thread); 634 635 thread->retval = thread->thread.func (thread->thread.data); 636 637 return NULL; 638} 639 640GThread* 641g_thread_create_full (GThreadFunc func, 642 gpointer data, 643 gulong stack_size, 644 gboolean joinable, 645 gboolean bound, 646 GThreadPriority priority, 647 GError **error) 648{ 649 GRealThread* result; 650 GError *local_error = NULL; 651 g_return_val_if_fail (func, NULL); 652 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL); 653 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL); 654 655 result = g_new0 (GRealThread, 1); 656 657 result->thread.joinable = joinable; 658 result->thread.priority = priority; 659 result->thread.func = func; 660 result->thread.data = data; 661 result->private_data = NULL; 662 G_LOCK (g_thread); 663 G_THREAD_UF (thread_create, (g_thread_create_proxy, result, 664 stack_size, joinable, bound, priority, 665 &result->system_thread, &local_error)); 666 if (!local_error) 667 { 668 result->next = g_thread_all_threads; 669 g_thread_all_threads = result; 670 } 671 G_UNLOCK (g_thread); 672 673 if (local_error) 674 { 675 g_propagate_error (error, local_error); 676 g_free (result); 677 return NULL; 678 } 679 680 return (GThread*) result; 681} 682 683void 684g_thread_exit (gpointer retval) 685{ 686 GRealThread* real = (GRealThread*) g_thread_self (); 687 real->retval = retval; 688 G_THREAD_CF (thread_exit, (void)0, ()); 689} 690 691gpointer 692g_thread_join (GThread* thread) 693{ 694 GRealThread* real = (GRealThread*) thread; 695 GRealThread *p, *t; 696 gpointer retval; 697 698 g_return_val_if_fail (thread, NULL); 699 g_return_val_if_fail (thread->joinable, NULL); 700 g_return_val_if_fail (!g_system_thread_equal (real->system_thread, 701 zero_thread), NULL); 702 703 G_THREAD_UF (thread_join, (&real->system_thread)); 704 705 retval = real->retval; 706 707 G_LOCK (g_thread); 708 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next) 709 { 710 if (t == (GRealThread*) thread) 711 { 712 if (p) 713 p->next = t->next; 714 else 715 g_thread_all_threads = t->next; 716 break; 717 } 718 } 719 G_UNLOCK (g_thread); 720 721 /* Just to make sure, this isn't used any more */ 722 thread->joinable = 0; 723 g_system_thread_assign (real->system_thread, zero_thread); 724 725 /* the thread structure for non-joinable threads is freed upon 726 thread end. We free the memory here. This will leave a loose end, 727 if a joinable thread is not joined. */ 728 729 g_free (thread); 730 731 return retval; 732} 733 734void 735g_thread_set_priority (GThread* thread, 736 GThreadPriority priority) 737{ 738 GRealThread* real = (GRealThread*) thread; 739 740 g_return_if_fail (thread); 741 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread)); 742 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW); 743 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT); 744 745 thread->priority = priority; 746 747 G_THREAD_CF (thread_set_priority, (void)0, 748 (&real->system_thread, priority)); 749} 750 751GThread* 752g_thread_self (void) 753{ 754 GRealThread* thread = g_private_get (g_thread_specific_private); 755 756 if (!thread) 757 { 758 /* If no thread data is available, provide and set one. This 759 can happen for the main thread and for threads, that are not 760 created by GLib. */ 761 thread = g_new0 (GRealThread, 1); 762 thread->thread.joinable = FALSE; /* This is a save guess */ 763 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is 764 just a guess */ 765 thread->thread.func = NULL; 766 thread->thread.data = NULL; 767 thread->private_data = NULL; 768 769 if (g_thread_supported ()) 770 G_THREAD_UF (thread_self, (&thread->system_thread)); 771 772 g_private_set (g_thread_specific_private, thread); 773 774 G_LOCK (g_thread); 775 thread->next = g_thread_all_threads; 776 g_thread_all_threads = thread; 777 G_UNLOCK (g_thread); 778 } 779 780 return (GThread*)thread; 781} 782 783void 784g_static_rw_lock_init (GStaticRWLock* lock) 785{ 786 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT; 787 788 g_return_if_fail (lock); 789 790 *lock = init_lock; 791} 792 793inline static void 794g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex) 795{ 796 if (!*cond) 797 *cond = g_cond_new (); 798 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex)); 799} 800 801inline static void 802g_static_rw_lock_signal (GStaticRWLock* lock) 803{ 804 if (lock->want_to_write && lock->write_cond) 805 g_cond_signal (lock->write_cond); 806 else if (lock->want_to_read && lock->read_cond) 807 g_cond_broadcast (lock->read_cond); 808} 809 810void 811g_static_rw_lock_reader_lock (GStaticRWLock* lock) 812{ 813 g_return_if_fail (lock); 814 815 if (!g_threads_got_initialized) 816 return; 817 818 g_static_mutex_lock (&lock->mutex); 819 lock->want_to_read++; 820 while (lock->have_writer || lock->want_to_write) 821 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex); 822 lock->want_to_read--; 823 lock->read_counter++; 824 g_static_mutex_unlock (&lock->mutex); 825} 826 827gboolean 828g_static_rw_lock_reader_trylock (GStaticRWLock* lock) 829{ 830 gboolean ret_val = FALSE; 831 832 g_return_val_if_fail (lock, FALSE); 833 834 if (!g_threads_got_initialized) 835 return TRUE; 836 837 g_static_mutex_lock (&lock->mutex); 838 if (!lock->have_writer && !lock->want_to_write) 839 { 840 lock->read_counter++; 841 ret_val = TRUE; 842 } 843 g_static_mutex_unlock (&lock->mutex); 844 return ret_val; 845} 846 847void 848g_static_rw_lock_reader_unlock (GStaticRWLock* lock) 849{ 850 g_return_if_fail (lock); 851 852 if (!g_threads_got_initialized) 853 return; 854 855 g_static_mutex_lock (&lock->mutex); 856 lock->read_counter--; 857 if (lock->read_counter == 0) 858 g_static_rw_lock_signal (lock); 859 g_static_mutex_unlock (&lock->mutex); 860} 861 862void 863g_static_rw_lock_writer_lock (GStaticRWLock* lock) 864{ 865 g_return_if_fail (lock); 866 867 if (!g_threads_got_initialized) 868 return; 869 870 g_static_mutex_lock (&lock->mutex); 871 lock->want_to_write++; 872 while (lock->have_writer || lock->read_counter) 873 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex); 874 lock->want_to_write--; 875 lock->have_writer = TRUE; 876 g_static_mutex_unlock (&lock->mutex); 877} 878 879gboolean 880g_static_rw_lock_writer_trylock (GStaticRWLock* lock) 881{ 882 gboolean ret_val = FALSE; 883 884 g_return_val_if_fail (lock, FALSE); 885 886 if (!g_threads_got_initialized) 887 return TRUE; 888 889 g_static_mutex_lock (&lock->mutex); 890 if (!lock->have_writer && !lock->read_counter) 891 { 892 lock->have_writer = TRUE; 893 ret_val = TRUE; 894 } 895 g_static_mutex_unlock (&lock->mutex); 896 return ret_val; 897} 898 899void 900g_static_rw_lock_writer_unlock (GStaticRWLock* lock) 901{ 902 g_return_if_fail (lock); 903 904 if (!g_threads_got_initialized) 905 return; 906 907 g_static_mutex_lock (&lock->mutex); 908 lock->have_writer = FALSE; 909 g_static_rw_lock_signal (lock); 910 g_static_mutex_unlock (&lock->mutex); 911} 912 913void 914g_static_rw_lock_free (GStaticRWLock* lock) 915{ 916 g_return_if_fail (lock); 917 918 if (lock->read_cond) 919 { 920 g_cond_free (lock->read_cond); 921 lock->read_cond = NULL; 922 } 923 if (lock->write_cond) 924 { 925 g_cond_free (lock->write_cond); 926 lock->write_cond = NULL; 927 } 928 g_static_mutex_free (&lock->mutex); 929} 930 931/** 932 * g_thread_foreach 933 * @thread_func: function to call for all GThread structures 934 * @user_data: second argument to @thread_func 935 * 936 * Call @thread_func on all existing #GThread structures. Note that 937 * threads may decide to exit while @thread_func is running, so 938 * without intimate knowledge about the lifetime of foreign threads, 939 * @thread_func shouldn't access the GThread* pointer passed in as 940 * first argument. However, @thread_func will not be called for threads 941 * which are known to have exited already. 942 * 943 * Due to thread lifetime checks, this function has an execution complexity 944 * which is quadratic in the number of existing threads. 945 * 946 * Since: 2.10 947 */ 948void 949g_thread_foreach (GFunc thread_func, 950 gpointer user_data) 951{ 952 GSList *slist = NULL; 953 GRealThread *thread; 954 g_return_if_fail (thread_func != NULL); 955 /* snapshot the list of threads for iteration */ 956 G_LOCK (g_thread); 957 for (thread = g_thread_all_threads; thread; thread = thread->next) 958 slist = g_slist_prepend (slist, thread); 959 G_UNLOCK (g_thread); 960 /* walk the list, skipping non-existant threads */ 961 while (slist) 962 { 963 GSList *node = slist; 964 slist = node->next; 965 /* check whether the current thread still exists */ 966 G_LOCK (g_thread); 967 for (thread = g_thread_all_threads; thread; thread = thread->next) 968 if (thread == node->data) 969 break; 970 G_UNLOCK (g_thread); 971 if (thread) 972 thread_func (thread, user_data); 973 g_slist_free_1 (node); 974 } 975} 976 977/** 978 * g_thread_get_initialized 979 * 980 * Indicates if g_thread_init() has been called. 981 * 982 * Returns: %TRUE if threads have been initialized. 983 * 984 * Since: 2.20 985 */ 986gboolean 987g_thread_get_initialized () 988{ 989 return g_thread_supported (); 990} 991 992#define __G_THREAD_C__ 993#include "galiasdef.c" 994