gthread.c 19.8 KB
Newer Older
1 2 3
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
4
 * gthread.c: MT safety related functions
5 6 7 8
 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
 *                Owen Taylor
 *
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10 11 12 13 14 15
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19 20 21 22 23 24
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
25
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
26 27 28 29 30 31 32 33 34
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

/* 
 * MT safe
 */

35 36 37
#include "config.h"

#ifdef HAVE_UNISTD_H
38
#include <unistd.h>
39
#endif
40

Havoc Pennington's avatar
Havoc Pennington committed
41 42
#include <string.h>

43
#include "glib.h"
44
#include "gthreadinit.h"
45

46
#if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
47 48
# define g_system_thread_equal_simple(thread1, thread2)			\
   ((thread1).dummy_pointer == (thread2).dummy_pointer)
49
# define g_system_thread_assign(dest, src)				\
50
   ((dest).dummy_pointer = (src).dummy_pointer)
51
#else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
52 53
# define g_system_thread_equal_simple(thread1, thread2)			\
   (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
54
# define g_system_thread_assign(dest, src)				\
55
   (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
56
#endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
57

58 59 60 61 62
#define g_system_thread_equal(thread1, thread2)				\
  (g_thread_functions_for_glib_use.thread_equal ? 			\
   g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
   g_system_thread_equal_simple((thread1), (thread2)))

63
GQuark 
64
g_thread_error_quark (void)
65 66 67
{
  static GQuark quark;
  if (!quark)
68
    quark = g_quark_from_static_string ("g_thread_error");
69 70 71
  return quark;
}

72
/* Keep this in sync with GRealThread in gmain.c! */
73 74 75 76
typedef struct _GRealThread GRealThread;
struct  _GRealThread
{
  GThread thread;
77 78
  gpointer private_data;
  gpointer retval;
79
  GSystemThread system_thread;
80 81 82 83 84 85 86 87 88 89 90 91 92 93
};

typedef struct _GStaticPrivateNode GStaticPrivateNode;
struct _GStaticPrivateNode
{
  gpointer       data;
  GDestroyNotify destroy;
};

static void g_thread_cleanup (gpointer data);
static void g_thread_fail (void);

/* Global variables */

94
static GSystemThread zero_thread; /* This is initialized to all zero */
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
gboolean g_thread_use_default_impl = TRUE;
gboolean g_threads_got_initialized = FALSE;

GThreadFunctions g_thread_functions_for_glib_use = {
  (GMutex*(*)())g_thread_fail,                 /* mutex_new */
  NULL,                                        /* mutex_lock */
  NULL,                                        /* mutex_trylock */
  NULL,                                        /* mutex_unlock */
  NULL,                                        /* mutex_free */
  (GCond*(*)())g_thread_fail,                  /* cond_new */
  NULL,                                        /* cond_signal */
  NULL,                                        /* cond_broadcast */
  NULL,                                        /* cond_wait */
  NULL,                                        /* cond_timed_wait  */
  NULL,                                        /* cond_free */
  (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
  NULL,                                        /* private_get */
  NULL,                                        /* private_set */
113 114
  (void(*)(GThreadFunc, gpointer, gulong, 
	   gboolean, gboolean, GThreadPriority, 
115
	   gpointer, GError**))g_thread_fail,  /* thread_create */
116 117 118 119 120 121 122 123 124
  NULL,                                        /* thread_yield */
  NULL,                                        /* thread_join */
  NULL,                                        /* thread_exit */
  NULL,                                        /* thread_set_priority */
  NULL                                         /* thread_self */
}; 

/* Local data */

125 126
static GMutex   *g_once_mutex = NULL;
static GCond    *g_once_cond = NULL;
127
static GPrivate *g_thread_specific_private = NULL;
128 129 130 131
static GSList   *g_thread_all_threads = NULL;
static GSList   *g_thread_free_indeces = NULL;

G_LOCK_DEFINE_STATIC (g_thread);
132

133
#ifdef G_THREADS_ENABLED
134 135 136
/* This must be called only once, before any threads are created.
 * It will only be called from g_thread_init() in -lgthread.
 */
137 138
void 
g_thread_init_glib (void)
139 140
{
  /* We let the main thread (the one that calls g_thread_init) inherit
141
   * the static_private data set before calling g_thread_init
142
   */
143
  GRealThread* main_thread = (GRealThread*) g_thread_self ();
144

145 146
  g_once_mutex = g_mutex_new ();
  g_once_cond = g_cond_new ();
147

148 149 150 151 152
  _g_convert_thread_init ();
  _g_rand_thread_init ();
  _g_main_thread_init ();
  _g_mem_thread_init ();
  _g_messages_thread_init ();
153 154
  _g_atomic_thread_init ();
 
155 156 157 158 159 160 161 162 163
  g_threads_got_initialized = TRUE;

  g_thread_specific_private = g_private_new (g_thread_cleanup);
  g_private_set (g_thread_specific_private, main_thread);
  G_THREAD_UF (thread_self, (&main_thread->system_thread));

  _g_mem_thread_private_init ();
  _g_messages_thread_private_init ();

164
}
165
#endif /* G_THREADS_ENABLED */
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
gpointer 
g_once_impl (GOnce       *once, 
	     GThreadFunc  func, 
	     gpointer     arg)
{
  g_mutex_lock (g_once_mutex);

  while (once->status == G_ONCE_STATUS_PROGRESS)
    g_cond_wait (g_once_cond, g_once_mutex);
  
  if (once->status != G_ONCE_STATUS_READY)
    {
      once->status = G_ONCE_STATUS_PROGRESS;
      g_mutex_unlock (g_once_mutex);
  
      once->retval = func (arg);

      g_mutex_lock (g_once_mutex);
      once->status = G_ONCE_STATUS_READY;
      g_cond_broadcast (g_once_cond);
    }
  
  g_mutex_unlock (g_once_mutex);
  
  return once->retval;
}

194 195 196 197 198 199 200
void 
g_static_mutex_init (GStaticMutex *mutex)
{
  static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;

  g_return_if_fail (mutex);

201
  *mutex = init_mutex;
202 203 204 205 206 207 208 209
}

GMutex *
g_static_mutex_get_mutex_impl (GMutex** mutex)
{
  if (!g_thread_supported ())
    return NULL;

210
  g_assert (g_once_mutex);
211

212
  g_mutex_lock (g_once_mutex);
213 214

  if (!(*mutex)) 
215 216 217 218 219 220 221 222 223 224 225 226
    {
      GMutex *new_mutex = g_mutex_new (); 
      
      /* The following is a memory barrier to avoid the write 
       * to *new_mutex being reordered to after writing *mutex */
      g_mutex_lock (new_mutex);
      g_mutex_unlock (new_mutex);
      
      *mutex = new_mutex;
    }

  g_mutex_unlock (g_once_mutex);
227 228 229 230
  
  return *mutex;
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
void
g_static_mutex_free (GStaticMutex* mutex)
{
  GMutex **runtime_mutex;
  
  g_return_if_fail (mutex);

  /* The runtime_mutex is the first (or only) member of GStaticMutex,
   * see both versions (of glibconfig.h) in configure.in */
  runtime_mutex = ((GMutex**)mutex);
  
  if (*runtime_mutex)
    g_mutex_free (*runtime_mutex);

  *runtime_mutex = NULL;
}

248 249 250 251 252 253 254
void     
g_static_rec_mutex_init (GStaticRecMutex *mutex)
{
  static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
  
  g_return_if_fail (mutex);

255
  *mutex = init_mutex;
256 257
}

258 259 260
void
g_static_rec_mutex_lock (GStaticRecMutex* mutex)
{
261 262 263 264
  GSystemThread self;

  g_return_if_fail (mutex);

265 266 267
  if (!g_thread_supported ())
    return;

268 269 270
  G_THREAD_UF (thread_self, (&self));

  if (g_system_thread_equal (self, mutex->owner))
271
    {
272 273
      mutex->depth++;
      return;
274
    }
275 276 277
  g_static_mutex_lock (&mutex->mutex);
  g_system_thread_assign (mutex->owner, self);
  mutex->depth = 1;
278 279 280 281 282
}

gboolean
g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
{
283 284 285 286
  GSystemThread self;

  g_return_val_if_fail (mutex, FALSE);

287 288 289
  if (!g_thread_supported ())
    return TRUE;

290 291 292
  G_THREAD_UF (thread_self, (&self));

  if (g_system_thread_equal (self, mutex->owner))
293
    {
294 295
      mutex->depth++;
      return TRUE;
296
    }
297 298 299 300 301 302

  if (!g_static_mutex_trylock (&mutex->mutex))
    return FALSE;

  g_system_thread_assign (mutex->owner, self);
  mutex->depth = 1;
303 304 305 306 307 308
  return TRUE;
}

void
g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
{
309 310
  g_return_if_fail (mutex);

311 312 313
  if (!g_thread_supported ())
    return;

314
  if (mutex->depth > 1)
315
    {
316 317
      mutex->depth--;
      return;
318
    }
319 320 321 322 323 324 325 326
  g_system_thread_assign (mutex->owner, zero_thread);
  g_static_mutex_unlock (&mutex->mutex);  
}

void
g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
				guint            depth)
{
327
  GSystemThread self;
328 329
  g_return_if_fail (mutex);

330 331 332
  if (!g_thread_supported ())
    return;

333 334 335 336 337 338 339
  G_THREAD_UF (thread_self, (&self));

  if (g_system_thread_equal (self, mutex->owner))
    {
      mutex->depth += depth;
      return;
    }
340
  g_static_mutex_lock (&mutex->mutex);
341
  g_system_thread_assign (mutex->owner, self);
342 343 344 345 346 347
  mutex->depth = depth;
}

guint    
g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
{
348
  guint depth;
349 350 351

  g_return_val_if_fail (mutex, 0);

352 353 354 355 356
  if (!g_thread_supported ())
    return 1;

  depth = mutex->depth;

357 358 359 360 361
  g_system_thread_assign (mutex->owner, zero_thread);
  mutex->depth = 0;
  g_static_mutex_unlock (&mutex->mutex);

  return depth;
362
}
363

364 365 366 367 368 369 370 371 372 373 374 375 376
void
g_static_rec_mutex_free (GStaticRecMutex *mutex)
{
  g_return_if_fail (mutex);

  g_static_mutex_free (&mutex->mutex);
}

void     
g_static_private_init (GStaticPrivate *private_key)
{
  private_key->index = 0;
}
377 378 379 380

gpointer
g_static_private_get (GStaticPrivate *private_key)
{
381
  GRealThread *self = (GRealThread*) g_thread_self ();
382 383 384 385 386 387 388 389 390
  GArray *array;

  array = self->private_data;
  if (!array)
    return NULL;

  if (!private_key->index)
    return NULL;
  else if (private_key->index <= array->len)
391 392
    return g_array_index (array, GStaticPrivateNode, 
			  private_key->index - 1).data;
393 394 395 396 397 398 399 400 401
  else
    return NULL;
}

void
g_static_private_set (GStaticPrivate *private_key, 
		      gpointer        data,
		      GDestroyNotify  notify)
{
402
  GRealThread *self = (GRealThread*) g_thread_self ();
403 404 405 406 407 408 409 410 411 412 413 414 415
  GArray *array;
  static guint next_index = 0;
  GStaticPrivateNode *node;

  array = self->private_data;
  if (!array)
    {
      array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
      self->private_data = array;
    }

  if (!private_key->index)
    {
416
      G_LOCK (g_thread);
417 418

      if (!private_key->index)
419 420 421 422 423 424 425 426 427 428 429 430
	{
	  if (g_thread_free_indeces)
	    {
	      private_key->index = 
		GPOINTER_TO_UINT (g_thread_free_indeces->data);
	      g_thread_free_indeces = 
		g_slist_delete_link (g_thread_free_indeces,
				     g_thread_free_indeces);
	    }
	  else
	    private_key->index = ++next_index;
	}
431

432
      G_UNLOCK (g_thread);
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    }

  if (private_key->index > array->len)
    g_array_set_size (array, private_key->index);

  node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
  if (node->destroy)
    {
      gpointer ddata = node->data;
      GDestroyNotify ddestroy = node->destroy;

      node->data = data;
      node->destroy = notify;

      ddestroy (ddata);
    }
  else
    {
      node->data = data;
      node->destroy = notify;
    }
}

456 457 458
void     
g_static_private_free (GStaticPrivate *private_key)
{
459
  guint index = private_key->index;
460 461
  GSList *list;

462
  if (!index)
463
    return;
464 465
  
  private_key->index = 0;
466 467 468 469 470

  G_LOCK (g_thread);
  list =  g_thread_all_threads;
  while (list)
    {
471 472
      GRealThread *thread = list->data;
      GArray *array = thread->private_data;
473
      list = list->next;
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

      if (array && index <= array->len)
	{
	  GStaticPrivateNode *node = &g_array_index (array, 
						     GStaticPrivateNode, 
						     index - 1);
	  gpointer ddata = node->data;
	  GDestroyNotify ddestroy = node->destroy;

	  node->data = NULL;
	  node->destroy = NULL;

	  if (ddestroy) 
	    {
	      G_UNLOCK (g_thread);
	      ddestroy (ddata);
	      G_LOCK (g_thread);
	      }
	}
493
    }
494 495
  g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces, 
					   GUINT_TO_POINTER (index));
496 497 498
  G_UNLOCK (g_thread);
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
static void
g_thread_cleanup (gpointer data)
{
  if (data)
    {
      GRealThread* thread = data;
      if (thread->private_data)
	{
	  GArray* array = thread->private_data;
	  guint i;
	  
	  for (i = 0; i < array->len; i++ )
	    {
	      GStaticPrivateNode *node = 
		&g_array_index (array, GStaticPrivateNode, i);
	      if (node->destroy)
		node->destroy (node->data);
	    }
	  g_array_free (array, TRUE);
	}
519

520 521 522 523
      /* We only free the thread structure, if it isn't joinable. If
         it is, the structure is freed in g_thread_join */
      if (!thread->thread.joinable)
	{
524 525 526 527
	  G_LOCK (g_thread);
	  g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
	  G_UNLOCK (g_thread);
	  
528
	  /* Just to make sure, this isn't used any more */
529
	  g_system_thread_assign (thread->system_thread, zero_thread);
530 531 532 533 534 535 536 537 538 539 540
	  g_free (thread);
	}
    }
}

static void
g_thread_fail (void)
{
  g_error ("The thread system is not yet initialized.");
}

541
static gpointer
542 543 544 545 546 547
g_thread_create_proxy (gpointer data)
{
  GRealThread* thread = data;

  g_assert (data);

548 549
  /* This has to happen before G_LOCK, as that might call g_thread_self */
  g_private_set (g_thread_specific_private, data);
550

551
  /* the lock makes sure, that thread->system_thread is written,
552
     before thread->thread.func is called. See g_thread_create. */
553 554
  G_LOCK (g_thread);
  G_UNLOCK (g_thread);
555
 
556
  thread->retval = thread->thread.func (thread->thread.data);
557 558

  return NULL;
559 560 561
}

GThread* 
562 563 564 565 566 567 568
g_thread_create_full (GThreadFunc 		 func,
		      gpointer 		 data,
		      gulong 		 stack_size,
		      gboolean 		 joinable,
		      gboolean 		 bound,
		      GThreadPriority 	 priority,
		      GError                **error)
569
{
570
  GRealThread* result;
571
  GError *local_error = NULL;
572
  g_return_val_if_fail (func, NULL);
573 574
  g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
  g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
575
  
576 577
  result = g_new (GRealThread, 1);

578 579
  result->thread.joinable = joinable;
  result->thread.priority = priority;
580 581
  result->thread.func = func;
  result->thread.data = data;
582
  result->private_data = NULL; 
583
  G_LOCK (g_thread);
584 585 586
  G_THREAD_UF (thread_create, (g_thread_create_proxy, result, 
			       stack_size, joinable, bound, priority,
			       &result->system_thread, &local_error));
587 588
  g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
  G_UNLOCK (g_thread);
589 590 591 592 593 594 595 596

  if (local_error)
    {
      g_propagate_error (error, local_error);
      g_free (result);
      return NULL;
    }

597 598 599
  return (GThread*) result;
}

600 601 602 603 604 605 606 607 608
void
g_thread_exit (gpointer retval)
{
  GRealThread* real = (GRealThread*) g_thread_self ();
  real->retval = retval;
  G_THREAD_CF (thread_exit, (void)0, ());
}

gpointer
609 610 611
g_thread_join (GThread* thread)
{
  GRealThread* real = (GRealThread*) thread;
612
  gpointer retval;
613

614 615 616 617
  g_return_val_if_fail (thread, NULL);
  g_return_val_if_fail (thread->joinable, NULL);
  g_return_val_if_fail (!g_system_thread_equal (real->system_thread, 
						zero_thread), NULL);
618

619
  G_THREAD_UF (thread_join, (&real->system_thread));
620

621 622
  retval = real->retval;

623 624 625 626
  G_LOCK (g_thread);
  g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
  G_UNLOCK (g_thread);

627 628
  /* Just to make sure, this isn't used any more */
  thread->joinable = 0;
629
  g_system_thread_assign (real->system_thread, zero_thread);
630 631

  /* the thread structure for non-joinable threads is freed upon
632
     thread end. We free the memory here. This will leave a loose end,
633 634 635
     if a joinable thread is not joined. */

  g_free (thread);
636 637

  return retval;
638 639 640 641 642 643 644 645 646
}

void
g_thread_set_priority (GThread* thread, 
		       GThreadPriority priority)
{
  GRealThread* real = (GRealThread*) thread;

  g_return_if_fail (thread);
647
  g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
648 649
  g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
  g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
650 651

  thread->priority = priority;
652

653 654
  G_THREAD_CF (thread_set_priority, (void)0, 
	       (&real->system_thread, priority));
655 656 657
}

GThread*
658
g_thread_self (void)
659 660 661 662 663 664 665
{
  GRealThread* thread = g_private_get (g_thread_specific_private);

  if (!thread)
    {  
      /* If no thread data is available, provide and set one.  This
         can happen for the main thread and for threads, that are not
666 667
         created by GLib. */
      thread = g_new (GRealThread, 1);
668 669 670
      thread->thread.joinable = FALSE; /* This is a save guess */
      thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
							     just a guess */
671
      thread->thread.func = NULL;
672
      thread->thread.data = NULL;
673
      thread->private_data = NULL;
674 675 676 677

      if (g_thread_supported ())
	G_THREAD_UF (thread_self, (&thread->system_thread));

678 679 680 681 682
      g_private_set (g_thread_specific_private, thread); 
      
      G_LOCK (g_thread);
      g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
      G_UNLOCK (g_thread);
683
    }
684
  
685 686 687
  return (GThread*)thread;
}

688 689 690 691 692 693 694
void
g_static_rw_lock_init (GStaticRWLock* lock)
{
  static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;

  g_return_if_fail (lock);

695
  *lock = init_lock;
696 697 698 699
}

static void inline 
g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
700 701 702 703 704 705
{
  if (!*cond)
      *cond = g_cond_new ();
  g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
}

706 707
static void inline 
g_static_rw_lock_signal (GStaticRWLock* lock)
708 709 710
{
  if (lock->want_to_write && lock->write_cond)
    g_cond_signal (lock->write_cond);
711
  else if (lock->want_to_read && lock->read_cond)
712
    g_cond_broadcast (lock->read_cond);
713 714
}

715 716
void 
g_static_rw_lock_reader_lock (GStaticRWLock* lock)
717 718 719 720 721 722 723
{
  g_return_if_fail (lock);

  if (!g_threads_got_initialized)
    return;

  g_static_mutex_lock (&lock->mutex);
724
  lock->want_to_read++;
725
  while (lock->have_writer || lock->want_to_write) 
726
    g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
727
  lock->want_to_read--;
728 729 730 731
  lock->read_counter++;
  g_static_mutex_unlock (&lock->mutex);
}

732 733
gboolean 
g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
734 735 736 737 738 739 740 741 742
{
  gboolean ret_val = FALSE;

  g_return_val_if_fail (lock, FALSE);

  if (!g_threads_got_initialized)
    return TRUE;

  g_static_mutex_lock (&lock->mutex);
743
  if (!lock->have_writer && !lock->want_to_write)
744 745 746 747 748 749 750 751
    {
      lock->read_counter++;
      ret_val = TRUE;
    }
  g_static_mutex_unlock (&lock->mutex);
  return ret_val;
}

752 753
void 
g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
754 755 756 757 758 759 760 761
{
  g_return_if_fail (lock);

  if (!g_threads_got_initialized)
    return;

  g_static_mutex_lock (&lock->mutex);
  lock->read_counter--;
762 763
  if (lock->read_counter == 0)
    g_static_rw_lock_signal (lock);
764 765 766
  g_static_mutex_unlock (&lock->mutex);
}

767 768
void 
g_static_rw_lock_writer_lock (GStaticRWLock* lock)
769 770 771 772 773 774 775 776
{
  g_return_if_fail (lock);

  if (!g_threads_got_initialized)
    return;

  g_static_mutex_lock (&lock->mutex);
  lock->want_to_write++;
777
  while (lock->have_writer || lock->read_counter)
778 779
    g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
  lock->want_to_write--;
780
  lock->have_writer = TRUE;
781 782 783
  g_static_mutex_unlock (&lock->mutex);
}

784 785
gboolean 
g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
786 787 788 789 790 791 792 793 794
{
  gboolean ret_val = FALSE;

  g_return_val_if_fail (lock, FALSE);
  
  if (!g_threads_got_initialized)
    return TRUE;

  g_static_mutex_lock (&lock->mutex);
795
  if (!lock->have_writer && !lock->read_counter)
796
    {
797
      lock->have_writer = TRUE;
798 799 800 801 802 803
      ret_val = TRUE;
    }
  g_static_mutex_unlock (&lock->mutex);
  return ret_val;
}

804 805
void 
g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
806 807 808 809 810 811 812
{
  g_return_if_fail (lock);
  
  if (!g_threads_got_initialized)
    return;

  g_static_mutex_lock (&lock->mutex);
813
  lock->have_writer = FALSE; 
814 815 816 817
  g_static_rw_lock_signal (lock);
  g_static_mutex_unlock (&lock->mutex);
}

818 819
void 
g_static_rw_lock_free (GStaticRWLock* lock)
820 821 822 823
{
  g_return_if_fail (lock);
  
  if (lock->read_cond)
824 825 826 827
    {
      g_cond_free (lock->read_cond);
      lock->read_cond = NULL;
    }
828
  if (lock->write_cond)
829 830 831 832 833
    {
      g_cond_free (lock->write_cond);
      lock->write_cond = NULL;
    }
  g_static_mutex_free (&lock->mutex);
834
}