sync0sync.c 36.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/******************************************************
Mutex, the basic synchronization primitive

(c) 1995 Innobase Oy

Created 9/5/1995 Heikki Tuuri
*******************************************************/

#include "sync0sync.h"
#ifdef UNIV_NONINL
#include "sync0sync.ic"
#endif

#include "sync0rw.h"
#include "buf0buf.h"
#include "srv0srv.h"
#include "buf0types.h"

/*
	REASONS FOR IMPLEMENTING THE SPIN LOCK MUTEX
	============================================

Semaphore operations in operating systems are slow: Solaris on a 1993 Sparc
takes 3 microseconds (us) for a lock-unlock pair and Windows NT on a 1995
Pentium takes 20 microseconds for a lock-unlock pair. Therefore, we have to
implement our own efficient spin lock mutex. Future operating systems may
provide efficient spin locks, but we cannot count on that.

Another reason for implementing a spin lock is that on multiprocessor systems
30
it can be more efficient for a processor to run a loop waiting for the
31 32 33 34 35 36 37
semaphore to be released than to switch to a different thread. A thread switch
takes 25 us on both platforms mentioned above. See Gray and Reuter's book
Transaction processing for background.

How long should the spin loop last before suspending the thread? On a
uniprocessor, spinning does not help at all, because if the thread owning the
mutex is not executing, it cannot be released. Spinning actually wastes
38
resources.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

On a multiprocessor, we do not know if the thread owning the mutex is
executing or not. Thus it would make sense to spin as long as the operation
guarded by the mutex would typically last assuming that the thread is
executing. If the mutex is not released by that time, we may assume that the
thread owning the mutex is not executing and suspend the waiting thread.

A typical operation (where no i/o involved) guarded by a mutex or a read-write
lock may last 1 - 20 us on the current Pentium platform. The longest
operations are the binary searches on an index node.

We conclude that the best choice is to set the spin time at 20 us. Then the
system should work well on a multiprocessor. On a uniprocessor we have to
make sure that thread swithches due to mutex collisions are not frequent,
i.e., they do not happen every 100 us or so, because that wastes too much
resources. If the thread switches are not frequent, the 20 us wasted in spin
55
loop is not too much.
56 57 58 59

Empirical studies on the effect of spin time should be done for different
platforms.

60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	IMPLEMENTATION OF THE MUTEX
	===========================

For background, see Curt Schimmel's book on Unix implementation on modern
architectures. The key points in the implementation are atomicity and
serialization of memory accesses. The test-and-set instruction (XCHG in
Pentium) must be atomic. As new processors may have weak memory models, also
serialization of memory references may be necessary. The successor of Pentium,
P6, has at least one mode where the memory model is weak. As far as we know,
in Pentium all memory accesses are serialized in the program order and we do
not have to worry about the memory model. On other processors there are
special machine instructions called a fence, memory barrier, or storage
barrier (STBAR in Sparc), which can be used to serialize the memory accesses
to happen in program order relative to the fence instruction.

Leslie Lamport has devised a "bakery algorithm" to implement a mutex without
the atomic test-and-set, but his algorithm should be modified for weak memory
models. We do not use Lamport's algorithm, because we guess it is slower than
the atomic test-and-set.

Our mutex implementation works as follows: After that we perform the atomic
test-and-set instruction on the memory word. If the test returns zero, we
know we got the lock first. If the test returns not zero, some other thread
was quicker and got the lock: then we spin in a loop reading the memory word,
waiting it to become zero. It is wise to just read the word in the loop, not
perform numerous test-and-set instructions, because they generate memory
traffic between the cache and the main memory. The read loop can just access
the cache, saving bus bandwidth.

If we cannot acquire the mutex lock in the specified time, we reserve a cell
in the wait array, set the waiters byte in the mutex to 1. To avoid a race
condition, after setting the waiters byte and before suspending the waiting
thread, we still have to check that the mutex is reserved, because it may
have happened that the thread which was holding the mutex has just released
it and did not see the waiters byte set to 1, a case which would lead the
other thread to an infinite wait.

LEMMA 1: After a thread resets the event of the cell it reserves for waiting
========
for a mutex, some thread will eventually call sync_array_signal_object with
the mutex as an argument. Thus no infinite wait is possible.

Proof:	After making the reservation the thread sets the waiters field in the
mutex to 1. Then it checks that the mutex is still reserved by some thread,
or it reserves the mutex for itself. In any case, some thread (which may be
also some earlier thread, not necessarily the one currently holding the mutex)
will set the waiters field to 0 in mutex_exit, and then call
108
sync_array_signal_object with the mutex as an argument.
109 110 111 112 113 114 115 116 117 118 119
Q.E.D. */

/* The number of system calls made in this module. Intended for performance
monitoring. */

ulint	mutex_system_call_count		= 0;

/* Number of spin waits on mutexes: for performance monitoring */

ulint	mutex_spin_round_count		= 0;
ulint	mutex_spin_wait_count		= 0;
120
ulint	mutex_os_wait_count		= 0;
121 122 123 124 125 126 127 128 129 130 131 132 133
ulint	mutex_exit_count		= 0;

/* The global array of wait cells for implementation of the database's own
mutexes and read-write locks */
sync_array_t*	sync_primary_wait_array;

/* This variable is set to TRUE when sync_init is called */
ibool	sync_initialized	= FALSE;


typedef struct sync_level_struct	sync_level_t;
typedef struct sync_thread_struct	sync_thread_t;

134
#ifdef UNIV_SYNC_DEBUG
135 136 137 138 139 140 141
/* The latch levels currently owned by threads are stored in this data
structure; the size of this array is OS_THREAD_MAX_N */

sync_thread_t*	sync_thread_level_arrays;

/* Mutex protecting sync_thread_level_arrays */
mutex_t	sync_thread_mutex;
142
#endif /* UNIV_SYNC_DEBUG */
143

kent@mysql.com's avatar
kent@mysql.com committed
144 145 146 147 148 149
/* Global list of database mutexes (not OS mutexes) created. */
ut_list_base_node_t  mutex_list;

/* Mutex protecting the mutex_list variable */
mutex_t mutex_list_mutex;

150
#ifdef UNIV_SYNC_DEBUG
151 152
/* Latching order checks start when this is set TRUE */
ibool	sync_order_checks_on	= FALSE;
153
#endif /* UNIV_SYNC_DEBUG */
154 155 156 157 158 159 160 161

struct sync_thread_struct{
	os_thread_id_t	id;	/* OS thread id */
	sync_level_t*	levels;	/* level array for this thread; if this is NULL
				this slot is unused */
};

/* Number of slots reserved for each OS thread in the sync level array */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
162
#define SYNC_THREAD_N_LEVELS	10000
163 164 165 166 167 168 169

struct sync_level_struct{
	void*	latch;	/* pointer to a mutex or an rw-lock; NULL means that
			the slot is empty */
	ulint	level;	/* level of the latch in the latching order */
};

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/**********************************************************************
A noninlined function that reserves a mutex. In ha_innodb.cc we have disabled
inlining of InnoDB functions, and no inlined functions should be called from
there. That is why we need to duplicate the inlined function here. */

void
mutex_enter_noninline(
/*==================*/
	mutex_t*	mutex)	/* in: mutex */
{
	mutex_enter(mutex);
}

/**********************************************************************
Releases a mutex. */

void
mutex_exit_noninline(
/*=================*/
	mutex_t*	mutex)	/* in: mutex */
{
	mutex_exit(mutex);
}

194 195 196 197 198 199 200 201 202 203
/**********************************************************************
Creates, or rather, initializes a mutex object in a specified memory
location (which must be appropriately aligned). The mutex is initialized
in the reset state. Explicit freeing of the mutex with mutex_free is
necessary only if the memory block containing it is freed. */

void
mutex_create_func(
/*==============*/
	mutex_t*	mutex,		/* in: pointer to memory */
204 205 206
#ifdef UNIV_DEBUG
	const char*	cmutex_name,	/* in: mutex name */
# ifdef UNIV_SYNC_DEBUG
207
	ulint		level,		/* in: level */
208 209
# endif /* UNIV_SYNC_DEBUG */
#endif /* UNIV_DEBUG */
210
	const char*	cfile_name,	/* in: file name where created */
211
	ulint		cline)		/* in: file line where created */
212
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
213
#if defined(_WIN32) && defined(UNIV_CAN_USE_X86_ASSEMBLER)
214
	mutex_reset_lock_word(mutex);
215
#else
216 217 218 219
	os_fast_mutex_init(&(mutex->os_fast_mutex));
	mutex->lock_word = 0;
#endif
	mutex_set_waiters(mutex, 0);
220
#ifdef UNIV_DEBUG
221
	mutex->magic_n = MUTEX_MAGIC_N;
222
#endif /* UNIV_DEBUG */
223
#ifdef UNIV_SYNC_DEBUG
224
	mutex->line = 0;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
225
	mutex->file_name = "not yet reserved";
226
	mutex->level = level;
227
#endif /* UNIV_SYNC_DEBUG */
228
	mutex->cfile_name = cfile_name;
229
	mutex->cline = cline;
230
#ifndef UNIV_HOTBACKUP
231 232
	mutex->count_os_wait = 0;
# ifdef UNIV_DEBUG
233 234 235 236 237 238 239 240
	mutex->cmutex_name=	  cmutex_name;
	mutex->count_using=	  0;
	mutex->mutex_type=	  0;
	mutex->lspent_time=	  0;
	mutex->lmax_spent_time=     0;
	mutex->count_spin_loop= 0;
	mutex->count_spin_rounds=   0;
	mutex->count_os_yield=  0;
241
# endif /* UNIV_DEBUG */
242
#endif /* !UNIV_HOTBACKUP */
243

244
	/* Check that lock_word is aligned; this is important on Intel */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
245
	ut_ad(((ulint)(&(mutex->lock_word))) % 4 == 0);
246 247 248

	/* NOTE! The very first mutexes are not put to the mutex list */

249 250 251 252 253
	if ((mutex == &mutex_list_mutex)
#ifdef UNIV_SYNC_DEBUG
	    || (mutex == &sync_thread_mutex)
#endif /* UNIV_SYNC_DEBUG */
	    ) {
254

255
		return;
256
	}
257

258 259
	mutex_enter(&mutex_list_mutex);

260 261
	ut_ad(UT_LIST_GET_LEN(mutex_list) == 0
	      || UT_LIST_GET_FIRST(mutex_list)->magic_n == MUTEX_MAGIC_N);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	UT_LIST_ADD_FIRST(list, mutex_list, mutex);

	mutex_exit(&mutex_list_mutex);
}

/**********************************************************************
Calling this function is obligatory only if the memory buffer containing
the mutex is freed. Removes a mutex object from the mutex list. The mutex
is checked to be in the reset state. */

void
mutex_free(
/*=======*/
	mutex_t*	mutex)	/* in: mutex */
{
278
	ut_ad(mutex_validate(mutex));
279 280
	ut_a(mutex_get_lock_word(mutex) == 0);
	ut_a(mutex_get_waiters(mutex) == 0);
281

282 283 284 285 286
	if (mutex != &mutex_list_mutex
#ifdef UNIV_SYNC_DEBUG
	    && mutex != &sync_thread_mutex
#endif /* UNIV_SYNC_DEBUG */
	    ) {
287

288
		mutex_enter(&mutex_list_mutex);
289

290 291 292 293 294 295
		ut_ad(!UT_LIST_GET_PREV(list, mutex)
		      || UT_LIST_GET_PREV(list, mutex)->magic_n
		      == MUTEX_MAGIC_N);
		ut_ad(!UT_LIST_GET_NEXT(list, mutex)
		      || UT_LIST_GET_NEXT(list, mutex)->magic_n
		      == MUTEX_MAGIC_N);
296 297

		UT_LIST_REMOVE(list, mutex_list, mutex);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
298 299 300

		mutex_exit(&mutex_list_mutex);
	}
301

302
#if !defined(_WIN32) || !defined(UNIV_CAN_USE_X86_ASSEMBLER)
303 304 305 306 307
	os_fast_mutex_free(&(mutex->os_fast_mutex));
#endif
	/* If we free the mutex protecting the mutex list (freeing is
	not necessary), we have to reset the magic number AFTER removing
	it from the list. */
308
#ifdef UNIV_DEBUG
309
	mutex->magic_n = 0;
310
#endif /* UNIV_DEBUG */
311 312 313 314 315 316 317 318 319
}

/************************************************************************
Tries to lock the mutex for the current thread. If the lock is not acquired
immediately, returns with return value 1. */

ulint
mutex_enter_nowait(
/*===============*/
320 321
					/* out: 0 if succeed, 1 if not */
	mutex_t*	mutex,		/* in: pointer to mutex */
322
	const char*	file_name __attribute__((unused)),
323
					/* in: file name where mutex
324
					requested */
325
	ulint		line __attribute__((unused)))
326
					/* in: line where requested */
327 328 329 330 331
{
	ut_ad(mutex_validate(mutex));

	if (!mutex_test_and_set(mutex)) {

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
332
#ifdef UNIV_SYNC_DEBUG
333
		mutex_set_debug_info(mutex, file_name, line);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
334
#endif
335

336 337 338 339 340 341
		return(0);	/* Succeeded! */
	}

	return(1);
}

342
#ifdef UNIV_DEBUG
343 344 345 346 347 348 349 350 351 352 353 354 355
/**********************************************************************
Checks that the mutex has been initialized. */

ibool
mutex_validate(
/*===========*/
	mutex_t*	mutex)
{
	ut_a(mutex);
	ut_a(mutex->magic_n == MUTEX_MAGIC_N);

	return(TRUE);
}
356
#endif /* UNIV_DEBUG */
357 358 359 360 361 362 363 364

/**********************************************************************
Sets the waiters field in a mutex. */

void
mutex_set_waiters(
/*==============*/
	mutex_t*	mutex,	/* in: mutex */
365
	ulint		n)	/* in: value to set */
366
{
367 368
	volatile ulint*	ptr;		/* declared volatile to ensure that
					the value is stored to memory */
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	ut_ad(mutex);

	ptr = &(mutex->waiters);

	*ptr = n;		/* Here we assume that the write of a single
				word in memory is atomic */
}

/**********************************************************************
Reserves a mutex for the current thread. If the mutex is reserved, the
function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting
for the mutex before suspending the thread. */

void
mutex_spin_wait(
/*============*/
385 386 387 388
	mutex_t*	mutex,		/* in: pointer to mutex */
	const char*	file_name,	/* in: file name where mutex
					requested */
	ulint		line)		/* in: line where requested */
389
{
390 391
	ulint	   index; /* index of the reserved wait cell */
	ulint	   i;	  /* spin round count */
392
#if defined UNIV_DEBUG && !defined UNIV_HOTBACKUP
393 394 395 396 397
	ib_longlong lstart_time = 0, lfinish_time; /* for timing os_wait */
	ulint ltime_diff;
	ulint sec;
	ulint ms;
	uint timer_started = 0;
398
#endif /* UNIV_DEBUG && !UNIV_HOTBACKUP */
399
	ut_ad(mutex);
400

vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
401
mutex_loop:
402

403
	i = 0;
404

405 406 407 408 409
	/* Spin waiting for the lock word to become zero. Note that we do
	not have to assume that the read access to the lock word is atomic,
	as the actual locking is always committed with atomic test-and-set.
	In reality, however, all processors probably have an atomic read of
	a memory word. */
410

vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
411
spin_loop:
412
#if defined UNIV_DEBUG && !defined UNIV_HOTBACKUP
413 414
	mutex_spin_wait_count++;
	mutex->count_spin_loop++;
415
#endif /* UNIV_DEBUG && !UNIV_HOTBACKUP */
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
416

417 418 419 420
	while (mutex_get_lock_word(mutex) != 0 && i < SYNC_SPIN_ROUNDS) {
		if (srv_spin_wait_delay) {
			ut_delay(ut_rnd_interval(0, srv_spin_wait_delay));
		}
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
421

422 423
		i++;
	}
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
424

425
	if (i == SYNC_SPIN_ROUNDS) {
426
#if defined UNIV_DEBUG && !defined UNIV_HOTBACKUP
427 428 429 430 431 432
		mutex->count_os_yield++;
		if (timed_mutexes == 1 && timer_started==0) {
			ut_usectime(&sec, &ms);
			lstart_time= (ib_longlong)sec * 1000000 + ms;
			timer_started = 1;
		}
433
#endif /* UNIV_DEBUG && !UNIV_HOTBACKUP */
434 435
		os_thread_yield();
	}
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
436 437

#ifdef UNIV_SRV_PRINT_LATCH_WAITS
438
	fprintf(stderr,
439 440
		"Thread %lu spin wait mutex at %p"
		" cfile %s cline %lu rnds %lu\n",
441
		(ulong) os_thread_pf(os_thread_get_curr_id()), (void*) mutex,
442
		mutex->cfile_name, (ulong) mutex->cline, (ulong) i);
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
443
#endif
444

445
	mutex_spin_round_count += i;
446

447
#if defined UNIV_DEBUG && !defined UNIV_HOTBACKUP
448
	mutex->count_spin_rounds += i;
449
#endif /* UNIV_DEBUG && !UNIV_HOTBACKUP */
450

451 452
	if (mutex_test_and_set(mutex) == 0) {
		/* Succeeded! */
453

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
454
#ifdef UNIV_SYNC_DEBUG
455
		mutex_set_debug_info(mutex, file_name, line);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
456
#endif
457

458 459
		goto finish_timing;
	}
460

461 462 463 464 465 466
	/* We may end up with a situation where lock_word is 0 but the OS
	fast mutex is still reserved. On FreeBSD the OS does not seem to
	schedule a thread which is constantly calling pthread_mutex_trylock
	(in mutex_test_and_set implementation). Then we could end up
	spinning here indefinitely. The following 'i++' stops this infinite
	spin. */
467

468
	i++;
469

470 471 472
	if (i < SYNC_SPIN_ROUNDS) {
		goto spin_loop;
	}
473

474
	sync_array_reserve_cell(sync_primary_wait_array, mutex,
475
				SYNC_MUTEX, file_name, line, &index);
476

477
	mutex_system_call_count++;
478

479 480 481 482 483
	/* The memory order of the array reservation and the change in the
	waiters field is important: when we suspend a thread, we first
	reserve the cell and then set waiters field to 1. When threads are
	released in mutex_exit, the waiters field is first set to zero and
	then the event is set to the signaled state. */
484

485
	mutex_set_waiters(mutex, 1);
486

487 488 489 490
	/* Try to reserve still a few times */
	for (i = 0; i < 4; i++) {
		if (mutex_test_and_set(mutex) == 0) {
			/* Succeeded! Free the reserved wait cell */
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
491

492
			sync_array_free_cell_protected(sync_primary_wait_array,
493
						       index);
494

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
495
#ifdef UNIV_SYNC_DEBUG
496
			mutex_set_debug_info(mutex, file_name, line);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
497
#endif
498

vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
499
#ifdef UNIV_SRV_PRINT_LATCH_WAITS
500 501 502
			fprintf(stderr, "Thread %lu spin wait succeeds at 2:"
				" mutex at %p\n",
				(ulong) os_thread_pf(os_thread_get_curr_id()),
503
				(void*) mutex);
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
504
#endif
505

506
			goto finish_timing;
507

508 509 510 511 512
			/* Note that in this case we leave the waiters field
			set to 1. We cannot reset it to zero, as we do not
			know if there are other waiters. */
		}
	}
513

514 515 516
	/* Now we know that there has been some thread holding the mutex
	after the change in the wait array and the waiters field was made.
	Now there is no risk of infinite wait on the event. */
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
517 518

#ifdef UNIV_SRV_PRINT_LATCH_WAITS
519 520
	fprintf(stderr,
		"Thread %lu OS wait mutex at %p cfile %s cline %lu rnds %lu\n",
521
		(ulong) os_thread_pf(os_thread_get_curr_id()), (void*) mutex,
522
		mutex->cfile_name, (ulong) mutex->cline, (ulong) i);
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
523 524
#endif

525 526
	mutex_system_call_count++;
	mutex_os_wait_count++;
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
527

528
#ifndef UNIV_HOTBACKUP
529
	mutex->count_os_wait++;
530
# ifdef UNIV_DEBUG
531 532 533 534 535 536 537
	/* !!!!! Sometimes os_wait can be called without os_thread_yield */

	if (timed_mutexes == 1 && timer_started==0) {
		ut_usectime(&sec, &ms);
		lstart_time= (ib_longlong)sec * 1000000 + ms;
		timer_started = 1;
	}
538
# endif /* UNIV_DEBUG */
539
#endif /* !UNIV_HOTBACKUP */
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
540

541 542
	sync_array_wait_event(sync_primary_wait_array, index);
	goto mutex_loop;
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
543 544

finish_timing:
545
#if defined UNIV_DEBUG && !defined UNIV_HOTBACKUP
546 547 548 549 550 551 552 553 554 555 556
	if (timed_mutexes == 1 && timer_started==1) {
		ut_usectime(&sec, &ms);
		lfinish_time= (ib_longlong)sec * 1000000 + ms;

		ltime_diff= (ulint) (lfinish_time - lstart_time);
		mutex->lspent_time += ltime_diff;

		if (mutex->lmax_spent_time < ltime_diff) {
			mutex->lmax_spent_time= ltime_diff;
		}
	}
557
#endif /* UNIV_DEBUG && !UNIV_HOTBACKUP */
558
	return;
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
}

/**********************************************************************
Releases the threads waiting in the primary wait array for this mutex. */

void
mutex_signal_object(
/*================*/
	mutex_t*	mutex)	/* in: mutex */
{
	mutex_set_waiters(mutex, 0);

	/* The memory order of resetting the waiters field and
	signaling the object is important. See LEMMA 1 above. */

	sync_array_signal_object(sync_primary_wait_array, mutex);
}

577
#ifdef UNIV_SYNC_DEBUG
578 579 580 581 582 583 584
/**********************************************************************
Sets the debug information for a reserved mutex. */

void
mutex_set_debug_info(
/*=================*/
	mutex_t*	mutex,		/* in: mutex */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
585
	const char*	file_name,	/* in: file where requested */
586 587 588 589 590 591 592 593
	ulint		line)		/* in: line where requested */
{
	ut_ad(mutex);
	ut_ad(file_name);

	sync_thread_add_level(mutex, mutex->level);

	mutex->file_name = file_name;
594
	mutex->line	 = line;
595
	mutex->thread_id = os_thread_get_curr_id();
596
}
597 598 599 600 601 602 603 604

/**********************************************************************
Gets the debug information for a reserved mutex. */

void
mutex_get_debug_info(
/*=================*/
	mutex_t*	mutex,		/* in: mutex */
605
	const char**	file_name,	/* out: file where requested */
606 607 608 609 610 611 612 613 614
	ulint*		line,		/* out: line where requested */
	os_thread_id_t* thread_id)	/* out: id of the thread which owns
					the mutex */
{
	ut_ad(mutex);

	*file_name = mutex->file_name;
	*line	   = mutex->line;
	*thread_id = mutex->thread_id;
615
}
616 617 618 619 620 621 622 623 624 625 626

/**********************************************************************
Checks that the current thread owns the mutex. Works only in the debug
version. */

ibool
mutex_own(
/*======*/
				/* out: TRUE if owns */
	mutex_t*	mutex)	/* in: mutex */
{
627
	ut_ad(mutex_validate(mutex));
628 629 630 631 632

	if (mutex_get_lock_word(mutex) != 1) {

		return(FALSE);
	}
633

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
634
	if (!os_thread_eq(mutex->thread_id, os_thread_get_curr_id())) {
635 636 637 638 639 640 641 642 643

		return(FALSE);
	}

	return(TRUE);
}

/**********************************************************************
Prints debug info of currently reserved mutexes. */
644
static
645
void
646 647 648
mutex_list_print_info(
/*==================*/
	FILE*	file)		/* in: file where to print */
649 650
{
	mutex_t*	mutex;
651
	const char*	file_name;
652 653 654 655
	ulint		line;
	os_thread_id_t	thread_id;
	ulint		count		= 0;

656
	fputs("----------\n"
657
	      "MUTEX INFO\n"
658
	      "----------\n", file);
659 660 661 662 663 664 665 666 667

	mutex_enter(&mutex_list_mutex);

	mutex = UT_LIST_GET_FIRST(mutex_list);

	while (mutex != NULL) {
		count++;

		if (mutex_get_lock_word(mutex) != 0) {
668
			mutex_get_debug_info(mutex, &file_name, &line,
669
					     &thread_id);
670
			fprintf(file,
671 672
				"Locked mutex: addr %p thread %ld"
				" file %s line %ld\n",
673
				(void*) mutex, os_thread_pf(thread_id),
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
674
				file_name, line);
675 676 677 678 679
		}

		mutex = UT_LIST_GET_NEXT(list, mutex);
	}

680
	fprintf(file, "Total number of mutexes %ld\n", count);
681

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
	mutex_exit(&mutex_list_mutex);
}

/**********************************************************************
Counts currently reserved mutexes. Works only in the debug version. */

ulint
mutex_n_reserved(void)
/*==================*/
{
	mutex_t*	mutex;
	ulint		count		= 0;

	mutex_enter(&mutex_list_mutex);

	mutex = UT_LIST_GET_FIRST(mutex_list);

	while (mutex != NULL) {
		if (mutex_get_lock_word(mutex) != 0) {

			count++;
		}

		mutex = UT_LIST_GET_NEXT(list, mutex);
	}

	mutex_exit(&mutex_list_mutex);

	ut_a(count >= 1);

	return(count - 1); /* Subtract one, because this function itself
			   was holding one mutex (mutex_list_mutex) */
}

/**********************************************************************
Returns TRUE if no mutex or rw-lock is currently locked. Works only in
the debug version. */

ibool
sync_all_freed(void)
/*================*/
{
724
	return(mutex_n_reserved() + rw_lock_n_locked() == 0);
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
}

/**********************************************************************
Gets the value in the nth slot in the thread level arrays. */
static
sync_thread_t*
sync_thread_level_arrays_get_nth(
/*=============================*/
			/* out: pointer to thread slot */
	ulint	n)	/* in: slot number */
{
	ut_ad(n < OS_THREAD_MAX_N);

	return(sync_thread_level_arrays + n);
}

/**********************************************************************
Looks for the thread slot for the calling thread. */
static
sync_thread_t*
sync_thread_level_arrays_find_slot(void)
/*====================================*/
			/* out: pointer to thread slot, NULL if not found */
748

749 750 751 752 753 754 755 756 757 758 759
{
	sync_thread_t*	slot;
	os_thread_id_t	id;
	ulint		i;

	id = os_thread_get_curr_id();

	for (i = 0; i < OS_THREAD_MAX_N; i++) {

		slot = sync_thread_level_arrays_get_nth(i);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
760
		if (slot->levels && os_thread_eq(slot->id, id)) {
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

			return(slot);
		}
	}

	return(NULL);
}

/**********************************************************************
Looks for an unused thread slot. */
static
sync_thread_t*
sync_thread_level_arrays_find_free(void)
/*====================================*/
			/* out: pointer to thread slot */
776

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
{
	sync_thread_t*	slot;
	ulint		i;

	for (i = 0; i < OS_THREAD_MAX_N; i++) {

		slot = sync_thread_level_arrays_get_nth(i);

		if (slot->levels == NULL) {

			return(slot);
		}
	}

	return(NULL);
}

/**********************************************************************
Gets the value in the nth slot in the thread level array. */
static
sync_level_t*
sync_thread_levels_get_nth(
/*=======================*/
				/* out: pointer to level slot */
	sync_level_t*	arr,	/* in: pointer to level array for an OS
				thread */
	ulint		n)	/* in: slot number */
{
	ut_ad(n < SYNC_THREAD_N_LEVELS);

	return(arr + n);
}

/**********************************************************************
Checks if all the level values stored in the level array are greater than
the given limit. */
static
ibool
sync_thread_levels_g(
/*=================*/
				/* out: TRUE if all greater */
	sync_level_t*	arr,	/* in: pointer to level array for an OS
				thread */
	ulint		limit)	/* in: level limit */
{
	sync_level_t*	slot;
	rw_lock_t*	lock;
	mutex_t*	mutex;
	ulint		i;

	for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {

		slot = sync_thread_levels_get_nth(arr, i);

		if (slot->latch != NULL) {
			if (slot->level <= limit) {

				lock = slot->latch;
				mutex = slot->latch;

837
				fprintf(stderr,
838 839 840
					"InnoDB error: sync levels should be"
					" > %lu but a level is %lu\n",
					(ulong) limit, (ulong) slot->level);
841 842

				if (mutex->magic_n == MUTEX_MAGIC_N) {
843 844 845
					fprintf(stderr,
						"Mutex created at %s %lu\n",
						mutex->cfile_name,
846
						(ulong) mutex->cline);
847 848

					if (mutex_get_lock_word(mutex) != 0) {
849
						const char*	file_name;
850 851
						ulint		line;
						os_thread_id_t	thread_id;
852

853 854 855
						mutex_get_debug_info(
							mutex, &file_name,
							&line, &thread_id);
856

857
						fprintf(stderr,
858 859 860 861
							"InnoDB: Locked mutex:"
							" addr %p thread %ld"
							" file %s line %ld\n",
							(void*) mutex,
862 863
							os_thread_pf(
								thread_id),
864 865
							file_name,
							(ulong) line);
866
					} else {
867
						fputs("Not locked\n", stderr);
868
					}
869 870 871
				} else {
					rw_lock_print(lock);
				}
872

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
				return(FALSE);
			}
		}
	}

	return(TRUE);
}

/**********************************************************************
Checks if the level value is stored in the level array. */
static
ibool
sync_thread_levels_contain(
/*=======================*/
				/* out: TRUE if stored */
	sync_level_t*	arr,	/* in: pointer to level array for an OS
				thread */
	ulint		level)	/* in: level */
{
	sync_level_t*	slot;
	ulint		i;

	for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {

		slot = sync_thread_levels_get_nth(arr, i);

		if (slot->latch != NULL) {
			if (slot->level == level) {

				return(TRUE);
			}
		}
	}

	return(FALSE);
}

/**********************************************************************
Checks that the level array for the current thread is empty. */

ibool
sync_thread_levels_empty_gen(
/*=========================*/
					/* out: TRUE if empty except the
					exceptions specified below */
	ibool	dict_mutex_allowed)	/* in: TRUE if dictionary mutex is
					allowed to be owned by the thread,
					also purge_is_running mutex is
					allowed */
{
	sync_level_t*	arr;
	sync_thread_t*	thread_slot;
	sync_level_t*	slot;
	ulint		i;

	if (!sync_order_checks_on) {

		return(TRUE);
	}

	mutex_enter(&sync_thread_mutex);

	thread_slot = sync_thread_level_arrays_find_slot();

	if (thread_slot == NULL) {

		mutex_exit(&sync_thread_mutex);

		return(TRUE);
	}

	arr = thread_slot->levels;

	for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {

		slot = sync_thread_levels_get_nth(arr, i);

950 951 952 953
		if (slot->latch != NULL
		    && (!dict_mutex_allowed
			|| (slot->level != SYNC_DICT
			    && slot->level != SYNC_DICT_OPERATION))) {
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

			mutex_exit(&sync_thread_mutex);
			ut_error;

			return(FALSE);
		}
	}

	mutex_exit(&sync_thread_mutex);

	return(TRUE);
}

/**********************************************************************
Checks that the level array for the current thread is empty. */

ibool
sync_thread_levels_empty(void)
/*==========================*/
			/* out: TRUE if empty */
{
	return(sync_thread_levels_empty_gen(FALSE));
}

/**********************************************************************
Adds a latch and its level in the thread level array. Allocates the memory
for the array if called first time for this OS thread. Makes the checks
against other latch levels stored in the array for this thread. */

void
sync_thread_add_level(
/*==================*/
	void*	latch,	/* in: pointer to a mutex or an rw-lock */
987 988
	ulint	level)	/* in: level in the latching order; if
			SYNC_LEVEL_VARYING, nothing is done */
989 990 991 992 993
{
	sync_level_t*	array;
	sync_level_t*	slot;
	sync_thread_t*	thread_slot;
	ulint		i;
994

995 996 997 998 999 1000
	if (!sync_order_checks_on) {

		return;
	}

	if ((latch == (void*)&sync_thread_mutex)
1001 1002 1003
	    || (latch == (void*)&mutex_list_mutex)
	    || (latch == (void*)&rw_lock_debug_mutex)
	    || (latch == (void*)&rw_lock_list_mutex)) {
1004 1005 1006 1007

		return;
	}

1008
	if (level == SYNC_LEVEL_VARYING) {
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

		return;
	}

	mutex_enter(&sync_thread_mutex);

	thread_slot = sync_thread_level_arrays_find_slot();

	if (thread_slot == NULL) {
		/* We have to allocate the level array for a new thread */
		array = ut_malloc(sizeof(sync_level_t) * SYNC_THREAD_N_LEVELS);
1020

1021
		thread_slot = sync_thread_level_arrays_find_free();
1022 1023

		thread_slot->id = os_thread_get_curr_id();
1024
		thread_slot->levels = array;
1025

1026 1027 1028 1029 1030 1031 1032 1033 1034
		for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {

			slot = sync_thread_levels_get_nth(array, i);

			slot->latch = NULL;
		}
	}

	array = thread_slot->levels;
1035

1036 1037 1038 1039 1040
	/* NOTE that there is a problem with _NODE and _LEAF levels: if the
	B-tree height changes, then a leaf can change to an internal node
	or the other way around. We do not know at present if this can cause
	unnecessary assertion failures below. */

1041 1042 1043 1044
	switch (level) {
	case SYNC_NO_ORDER_CHECK:
	case SYNC_EXTERN_STORAGE:
	case SYNC_TREE_NODE_FROM_HASH:
1045
		/* Do no order checking */
1046 1047
		break;
	case SYNC_MEM_POOL:
1048
		ut_a(sync_thread_levels_g(array, SYNC_MEM_POOL));
1049 1050
		break;
	case SYNC_MEM_HASH:
1051
		ut_a(sync_thread_levels_g(array, SYNC_MEM_HASH));
1052 1053
		break;
	case SYNC_RECV:
1054
		ut_a(sync_thread_levels_g(array, SYNC_RECV));
1055
		break;
1056 1057 1058
	case SYNC_WORK_QUEUE:
		ut_a(sync_thread_levels_g(array, SYNC_WORK_QUEUE));
		break;
1059
	case SYNC_LOG:
1060
		ut_a(sync_thread_levels_g(array, SYNC_LOG));
1061 1062
		break;
	case SYNC_THR_LOCAL:
1063
		ut_a(sync_thread_levels_g(array, SYNC_THR_LOCAL));
1064 1065
		break;
	case SYNC_ANY_LATCH:
1066
		ut_a(sync_thread_levels_g(array, SYNC_ANY_LATCH));
1067 1068
		break;
	case SYNC_TRX_SYS_HEADER:
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1069
		ut_a(sync_thread_levels_g(array, SYNC_TRX_SYS_HEADER));
1070 1071
		break;
	case SYNC_DOUBLEWRITE:
1072
		ut_a(sync_thread_levels_g(array, SYNC_DOUBLEWRITE));
1073 1074
		break;
	case SYNC_BUF_BLOCK:
1075
		ut_a((sync_thread_levels_contain(array, SYNC_BUF_POOL)
1076 1077
		      && sync_thread_levels_g(array, SYNC_BUF_BLOCK - 1))
		     || sync_thread_levels_g(array, SYNC_BUF_BLOCK));
1078 1079
		break;
	case SYNC_BUF_POOL:
1080
		ut_a(sync_thread_levels_g(array, SYNC_BUF_POOL));
1081 1082
		break;
	case SYNC_SEARCH_SYS:
1083
		ut_a(sync_thread_levels_g(array, SYNC_SEARCH_SYS));
1084 1085
		break;
	case SYNC_TRX_LOCK_HEAP:
1086
		ut_a(sync_thread_levels_g(array, SYNC_TRX_LOCK_HEAP));
1087 1088
		break;
	case SYNC_REC_LOCK:
1089
		ut_a((sync_thread_levels_contain(array, SYNC_KERNEL)
1090 1091
		      && sync_thread_levels_g(array, SYNC_REC_LOCK - 1))
		     || sync_thread_levels_g(array, SYNC_REC_LOCK));
1092 1093
		break;
	case SYNC_KERNEL:
1094
		ut_a(sync_thread_levels_g(array, SYNC_KERNEL));
1095 1096
		break;
	case SYNC_IBUF_BITMAP:
1097
		ut_a((sync_thread_levels_contain(array, SYNC_IBUF_BITMAP_MUTEX)
1098 1099
		      && sync_thread_levels_g(array, SYNC_IBUF_BITMAP - 1))
		     || sync_thread_levels_g(array, SYNC_IBUF_BITMAP));
1100 1101
		break;
	case SYNC_IBUF_BITMAP_MUTEX:
1102
		ut_a(sync_thread_levels_g(array, SYNC_IBUF_BITMAP_MUTEX));
1103 1104
		break;
	case SYNC_FSP_PAGE:
1105
		ut_a(sync_thread_levels_contain(array, SYNC_FSP));
1106 1107
		break;
	case SYNC_FSP:
1108
		ut_a(sync_thread_levels_contain(array, SYNC_FSP)
1109
		     || sync_thread_levels_g(array, SYNC_FSP));
1110 1111
		break;
	case SYNC_TRX_UNDO_PAGE:
1112
		ut_a(sync_thread_levels_contain(array, SYNC_TRX_UNDO)
1113 1114 1115
		     || sync_thread_levels_contain(array, SYNC_RSEG)
		     || sync_thread_levels_contain(array, SYNC_PURGE_SYS)
		     || sync_thread_levels_g(array, SYNC_TRX_UNDO_PAGE));
1116 1117
		break;
	case SYNC_RSEG_HEADER:
1118
		ut_a(sync_thread_levels_contain(array, SYNC_RSEG));
1119 1120
		break;
	case SYNC_RSEG_HEADER_NEW:
1121
		ut_a(sync_thread_levels_contain(array, SYNC_KERNEL)
1122
		     && sync_thread_levels_contain(array, SYNC_FSP_PAGE));
1123 1124
		break;
	case SYNC_RSEG:
1125
		ut_a(sync_thread_levels_g(array, SYNC_RSEG));
1126 1127
		break;
	case SYNC_TRX_UNDO:
1128
		ut_a(sync_thread_levels_g(array, SYNC_TRX_UNDO));
1129 1130
		break;
	case SYNC_PURGE_LATCH:
1131
		ut_a(sync_thread_levels_g(array, SYNC_PURGE_LATCH));
1132 1133
		break;
	case SYNC_PURGE_SYS:
1134
		ut_a(sync_thread_levels_g(array, SYNC_PURGE_SYS));
1135 1136
		break;
	case SYNC_TREE_NODE:
1137
		ut_a(sync_thread_levels_contain(array, SYNC_INDEX_TREE)
1138
		     || sync_thread_levels_g(array, SYNC_TREE_NODE - 1));
1139 1140
		break;
	case SYNC_TREE_NODE_NEW:
1141
		ut_a(sync_thread_levels_contain(array, SYNC_FSP_PAGE)
1142
		     || sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
1143 1144
		break;
	case SYNC_INDEX_TREE:
1145
		ut_a((sync_thread_levels_contain(array, SYNC_IBUF_MUTEX)
1146 1147 1148
		      && sync_thread_levels_contain(array, SYNC_FSP)
		      && sync_thread_levels_g(array, SYNC_FSP_PAGE - 1))
		     || sync_thread_levels_g(array, SYNC_TREE_NODE - 1));
1149 1150
		break;
	case SYNC_IBUF_MUTEX:
1151
		ut_a(sync_thread_levels_g(array, SYNC_FSP_PAGE - 1));
1152 1153
		break;
	case SYNC_IBUF_PESS_INSERT_MUTEX:
1154
		ut_a(sync_thread_levels_g(array, SYNC_FSP - 1)
1155
		     && !sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
1156 1157
		break;
	case SYNC_IBUF_HEADER:
1158
		ut_a(sync_thread_levels_g(array, SYNC_FSP - 1)
1159
		     && !sync_thread_levels_contain(array, SYNC_IBUF_MUTEX)
1160 1161
		     && !sync_thread_levels_contain(
			     array, SYNC_IBUF_PESS_INSERT_MUTEX));
1162 1163
		break;
	case SYNC_DICT_AUTOINC_MUTEX:
1164
		ut_a(sync_thread_levels_g(array, SYNC_DICT_AUTOINC_MUTEX));
1165 1166
		break;
	case SYNC_DICT_OPERATION:
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1167
		ut_a(sync_thread_levels_g(array, SYNC_DICT_OPERATION));
1168 1169
		break;
	case SYNC_DICT_HEADER:
1170
		ut_a(sync_thread_levels_g(array, SYNC_DICT_HEADER));
1171 1172
		break;
	case SYNC_DICT:
1173
#ifdef UNIV_DEBUG
1174
		ut_a(buf_debug_prints
1175
		     || sync_thread_levels_g(array, SYNC_DICT));
1176 1177 1178
#else /* UNIV_DEBUG */
		ut_a(sync_thread_levels_g(array, SYNC_DICT));
#endif /* UNIV_DEBUG */
1179 1180
		break;
	default:
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
		ut_error;
	}

	for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {

		slot = sync_thread_levels_get_nth(array, i);

		if (slot->latch == NULL) {
			slot->latch = latch;
			slot->level = level;

			break;
		}
	}

	ut_a(i < SYNC_THREAD_N_LEVELS);

	mutex_exit(&sync_thread_mutex);
}
1200

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
/**********************************************************************
Removes a latch from the thread level array if it is found there. */

ibool
sync_thread_reset_level(
/*====================*/
			/* out: TRUE if found from the array; it is an error
			if the latch is not found */
	void*	latch)	/* in: pointer to a mutex or an rw-lock */
{
	sync_level_t*	array;
	sync_level_t*	slot;
	sync_thread_t*	thread_slot;
	ulint		i;
1215

1216 1217 1218 1219 1220 1221
	if (!sync_order_checks_on) {

		return(FALSE);
	}

	if ((latch == (void*)&sync_thread_mutex)
1222 1223 1224
	    || (latch == (void*)&mutex_list_mutex)
	    || (latch == (void*)&rw_lock_debug_mutex)
	    || (latch == (void*)&rw_lock_list_mutex)) {
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241

		return(FALSE);
	}

	mutex_enter(&sync_thread_mutex);

	thread_slot = sync_thread_level_arrays_find_slot();

	if (thread_slot == NULL) {

		ut_error;

		mutex_exit(&sync_thread_mutex);
		return(FALSE);
	}

	array = thread_slot->levels;
1242

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
	for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {

		slot = sync_thread_levels_get_nth(array, i);

		if (slot->latch == latch) {
			slot->latch = NULL;

			mutex_exit(&sync_thread_mutex);

			return(TRUE);
		}
	}

	ut_error;

	mutex_exit(&sync_thread_mutex);

	return(FALSE);
}
1262
#endif /* UNIV_SYNC_DEBUG */
1263

1264 1265 1266 1267 1268 1269 1270
/**********************************************************************
Initializes the synchronization data structures. */

void
sync_init(void)
/*===========*/
{
1271
#ifdef UNIV_SYNC_DEBUG
1272 1273
	sync_thread_t*	thread_slot;
	ulint		i;
1274
#endif /* UNIV_SYNC_DEBUG */
1275

1276 1277 1278 1279 1280 1281 1282 1283
	ut_a(sync_initialized == FALSE);

	sync_initialized = TRUE;

	/* Create the primary system wait array which is protected by an OS
	mutex */

	sync_primary_wait_array = sync_array_create(OS_THREAD_MAX_N,
1284
						    SYNC_ARRAY_OS_MUTEX);
1285
#ifdef UNIV_SYNC_DEBUG
1286 1287 1288 1289
	/* Create the thread latch level array where the latch levels
	are stored for each OS thread */

	sync_thread_level_arrays = ut_malloc(OS_THREAD_MAX_N
1290
					     * sizeof(sync_thread_t));
1291 1292 1293 1294 1295
	for (i = 0; i < OS_THREAD_MAX_N; i++) {

		thread_slot = sync_thread_level_arrays_get_nth(i);
		thread_slot->levels = NULL;
	}
1296
#endif /* UNIV_SYNC_DEBUG */
1297
	/* Init the mutex list and create the mutex to protect it. */
1298 1299

	UT_LIST_INIT(mutex_list);
1300
	mutex_create(&mutex_list_mutex, SYNC_NO_ORDER_CHECK);
1301
#ifdef UNIV_SYNC_DEBUG
1302
	mutex_create(&sync_thread_mutex, SYNC_NO_ORDER_CHECK);
1303
#endif /* UNIV_SYNC_DEBUG */
1304 1305 1306 1307

	/* Init the rw-lock list and create the mutex to protect it. */

	UT_LIST_INIT(rw_lock_list);
1308
	mutex_create(&rw_lock_list_mutex, SYNC_NO_ORDER_CHECK);
1309

1310
#ifdef UNIV_SYNC_DEBUG
1311
	mutex_create(&rw_lock_debug_mutex, SYNC_NO_ORDER_CHECK);
1312 1313 1314

	rw_lock_debug_event = os_event_create(NULL);
	rw_lock_debug_waiters = FALSE;
1315
#endif /* UNIV_SYNC_DEBUG */
1316 1317 1318
}

/**********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1319 1320
Frees the resources in InnoDB's own synchronization data structures. Use
os_sync_free() after calling this. */
1321 1322 1323 1324 1325

void
sync_close(void)
/*===========*/
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1326 1327
	mutex_t*	mutex;

1328
	sync_array_free(sync_primary_wait_array);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1329 1330 1331 1332

	mutex = UT_LIST_GET_FIRST(mutex_list);

	while (mutex) {
1333
		mutex_free(mutex);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1334 1335 1336 1337
		mutex = UT_LIST_GET_FIRST(mutex_list);
	}

	mutex_free(&mutex_list_mutex);
1338
#ifdef UNIV_SYNC_DEBUG
1339
	mutex_free(&sync_thread_mutex);
1340
#endif /* UNIV_SYNC_DEBUG */
1341 1342 1343 1344 1345 1346
}

/***********************************************************************
Prints wait info of the sync system. */

void
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1347 1348
sync_print_wait_info(
/*=================*/
1349
	FILE*	file)		/* in: file where to print */
1350
{
1351
#ifdef UNIV_SYNC_DEBUG
1352
	fprintf(file, "Mutex exits %lu, rws exits %lu, rwx exits %lu\n",
1353 1354
		mutex_exit_count, rw_s_exit_count, rw_x_exit_count);
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1355

1356
	fprintf(file,
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
		"Mutex spin waits %lu, rounds %lu, OS waits %lu\n"
		"RW-shared spins %lu, OS waits %lu;"
		" RW-excl spins %lu, OS waits %lu\n",
		(ulong) mutex_spin_wait_count,
		(ulong) mutex_spin_round_count,
		(ulong) mutex_os_wait_count,
		(ulong) rw_s_spin_wait_count,
		(ulong) rw_s_os_wait_count,
		(ulong) rw_x_spin_wait_count,
		(ulong) rw_x_os_wait_count);
1367 1368 1369 1370 1371 1372
}

/***********************************************************************
Prints info of the sync system. */

void
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1373 1374
sync_print(
/*=======*/
1375
	FILE*	file)		/* in: file where to print */
1376
{
1377
#ifdef UNIV_SYNC_DEBUG
1378
	mutex_list_print_info(file);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1379

1380
	rw_lock_list_print_info(file);
1381
#endif /* UNIV_SYNC_DEBUG */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1382

1383
	sync_array_print_info(file, sync_primary_wait_array);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1384

1385
	sync_print_wait_info(file);
1386
}