Bug #37425 make dist is missing config/ac-macros/libevent_configure.m4
Submitted: 16 Jun 2008 9:16 Modified: 19 Jun 2008 1:36
Reporter: Hakan Küçükyılmaz Email Updates:
Status: Closed
Category:Server: Compiling Severity:S3 (Non-critical)
Version:6.0, 6.0-falcon OS:Any (with libevent)
Assigned to: Hakan Küçükyılmaz Target Version:
Triage: D3 (Medium)

[16 Jun 2008 9:16] Hakan Küçükyılmaz
Description:
make dist is missing config/ac-macros/libevent_configure.m4

How to repeat:
BUILD/compile-dist
make dist
gunzip -c mysql-6.0.6-alpha.tar.gz | tar xvf -
BUILD/compile-amd64-debug-max-no-ndb

++++ IFS='
'
++++ aclocal
/usr/bin/m4:configure.in:2385: cannot open `config/ac-macros/libevent_configure.m4': No
such file or directory
autom4te: /usr/bin/m4 failed with exit status: 1
[16 Jun 2008 12:57] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/47905

2704 Hakan Kuecuekyilmaz	2008-06-16
      Fix for Bug#37425 make dist is missing config/ac-macros/libevent_configure.m4.
[16 Jun 2008 19:28] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/47939

2666 Joerg Bruehe	2008-06-16
      File "config/ac-macros/libevent_configure.m4" was missing in the source tarball
      (bug#37425).
      
      Fix that in top-level "Makefile.am",
      also improve comments about this file in "config/ac-macros/libevent.m4"
[16 Jun 2008 19:51] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/47941

2666 Joerg Bruehe	2008-06-16
      File "config/ac-macros/libevent_configure.m4" was missing in the source tarball
      (bug#37425).
      
      Fix that in top-level "Makefile.am",
      also improve comments about this file in "config/ac-macros/libevent.m4"
[16 Jun 2008 19:56] Joerg Bruehe
Hakan's and my push would both solve it.

However, the "sinclude" approach used by Hakan has the side effect of immediately running
the macros of the file (4* AC_CHECK_LIB, AC_CHECK_STDC),
and in my local tests this seemed to have undesirable consequences.
[16 Jun 2008 22:01] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/47944

2704 Hakan Kuecuekyilmaz	2008-06-17
      Applied fix of Joerg Bruehe.
      
      File "config/ac-macros/libevent_configure.m4" was missing in the source tarball
      (bug#37425).
      
      Fix that in top-level "Makefile.am",
      also improve comments about this file in "config/ac-macros/libevent.m4"
[16 Jun 2008 22:46] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/47945

2704 Hakan Kuecuekyilmaz	2008-06-17
      Applied fix of Joerg Bruehe.
      
      File "config/ac-macros/libevent_configure.m4" was missing in the source tarball
      (bug#37425).
      
      Fix that in top-level "Makefile.am",
      also improve comments about this file in "config/ac-macros/libevent.m4"
[16 Jun 2008 22:47] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/47946

2704 Hakan Kuecuekyilmaz	2008-06-17
      Applied fix of Joerg Bruehe.
      
      File "config/ac-macros/libevent_configure.m4" was missing in the source tarball
      (bug#37425).
      
      Fix that in top-level "Makefile.am",
      also improve comments about this file in "config/ac-macros/libevent.m4"
[18 Jun 2008 18:16] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/48116

2667 Joerg Bruehe	2008-06-18 [merge]
      Merge the main tree into the build team tree.
      
      Changes in the main tree are these:
         Timothy Smith 2008-06-17 Merge up changes from mysql-5.1
[18 Jun 2008 23:51] Timothy Smith
Pushed to mysql-6.0 (6.0.6).  Probably nothing to document?
[19 Jun 2008 1:36] Paul DuBois
No changelog entry needed.
[21 Jun 2008 0:14] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/48276

0 and the type is not unsigned then we reset the value to 0. */
 static
-ib_longlong
+ib_ulonglong
 row_search_autoinc_read_column(
 /*===========================*/
 					/* out: value read from the column */
@@ -4593,10 +4614,8 @@
 {
 	ulint		len;
 	const byte*	data;
-	ib_longlong	value;
+	ib_ulonglong	value;
 	mem_heap_t*	heap = NULL;
-	/* Our requirement is that dest should be word aligned. */
-	byte		dest[sizeof(value)];
 	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
 	ulint*		offsets	= offsets_;
 
@@ -4614,41 +4633,14 @@
 	ut_a(len != UNIV_SQL_NULL);
 	ut_a(len <= sizeof value);
 
-	mach_read_int_type(dest, data, len, unsigned_type);
-
-	/* The assumption here is that the AUTOINC value can't be negative
-	and that dest is word aligned. */
-	switch (len) {
-	case 8:
-		value = *(ib_longlong*) dest;
-		break;
-
-	case 4:
-		value = *(ib_uint32_t*) dest;
-		break;
-
-	case 3:
-		value = *(ib_uint32_t*) dest;
-		value &= 0xFFFFFF;
-		break;
-
-	case 2:
-		value = *(uint16 *) dest;
-		break;
-
-	case 1:
-		value = *dest;
-		break;
-
-	default:
-		ut_error;
-	}
+	value = mach_read_int_type(data, len, unsigned_type);
 
 	if (UNIV_LIKELY_NULL(heap)) {
 		mem_heap_free(heap);
 	}
 
-	if (!unsigned_type && value < 0) {
+	/* We assume that the autoinc counter can't be negative. */
+	if (!unsigned_type && (ib_longlong) value < 0) {
 		value = 0;
 	}
 
@@ -4687,7 +4679,7 @@
 					column name can't be found in index */
 	dict_index_t*	index,		/* in: index to search */
 	const char*	col_name,	/* in: name of autoinc column */
-	ib_longlong*	value)		/* out: AUTOINC value read */
+	ib_ulonglong*	value)		/* out: AUTOINC value read */
 {
 	ulint		i;
 	ulint		n_cols;

=== modified file 'storage/innobase/srv/srv0srv.c'
--- a/storage/innobase/srv/srv0srv.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/srv/srv0srv.c	2008-06-12 00:08:07 +0000
@@ -192,7 +192,7 @@
 ulong	srv_insert_buffer_batch_size = 20;
 
 char*	srv_file_flush_method_str = NULL;
-ulint	srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;
+ulint	srv_unix_file_flush_method = SRV_UNIX_FSYNC;
 ulint	srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
 
 ulint	srv_max_n_open_files	  = 300;
@@ -1453,8 +1453,11 @@
 		srv_n_lock_wait_count++;
 		srv_n_lock_wait_current_count++;
 
-		ut_usectime(&sec, &ms);
-		start_time = (ib_longlong)sec * 1000000 + ms;
+		if (ut_usectime(&sec, &ms) == -1) {
+			start_time = -1;
+		} else {
+			start_time = (ib_longlong)sec * 1000000 + ms;
+		}
 	}
 	/* Wake the lock timeout monitor thread, if it is suspended */
 
@@ -1508,14 +1511,20 @@
 	wait_time = ut_difftime(ut_time(), slot->suspend_time);
 
 	if (thr->lock_state == QUE_THR_LOCK_ROW) {
-		ut_usectime(&sec, &ms);
-		finish_time = (ib_longlong)sec * 1000000 + ms;
+		if (ut_usectime(&sec, &ms) == -1) {
+			finish_time = -1;
+		} else {
+			finish_time = (ib_longlong)sec * 1000000 + ms;
+		}
 
 		diff_time = (ulint) (finish_time - start_time);
 
 		srv_n_lock_wait_current_count--;
 		srv_n_lock_wait_time = srv_n_lock_wait_time + diff_time;
-		if (diff_time > srv_n_lock_max_wait_time) {
+		if (diff_time > srv_n_lock_max_wait_time &&
+		    /* only update the variable if we successfully
+		    retrieved the start and finish times. See Bug#36819. */
+		    start_time != -1 && finish_time != -1) {
 			srv_n_lock_max_wait_time = diff_time;
 		}
 	}
@@ -1825,8 +1834,10 @@
 		= UT_LIST_GET_LEN(buf_pool->flush_list);
 	export_vars.innodb_buffer_pool_pages_free
 		= UT_LIST_GET_LEN(buf_pool->free);
+#ifdef UNIV_DEBUG
 	export_vars.innodb_buffer_pool_pages_latched
 		= buf_get_latched_pages_number();
+#endif /* UNIV_DEBUG */
 	export_vars.innodb_buffer_pool_pages_total = buf_pool->curr_size;
 
 	export_vars.innodb_buffer_pool_pages_misc = buf_pool->max_size
@@ -1904,12 +1915,6 @@
 
 	os_thread_sleep(1000000);
 
-	/* In case mutex_exit is not a memory barrier, it is
-	theoretically possible some threads are left waiting though
-	the semaphore is already released. Wake up those threads: */
-
-	sync_arr_wake_threads_if_sema_free();
-
 	current_time = time(NULL);
 
 	time_elapsed = difftime(current_time, last_monitor_time);
@@ -2106,9 +2111,15 @@
 		srv_refresh_innodb_monitor_stats();
 	}
 
+	/* In case mutex_exit is not a memory barrier, it is
+	theoretically possible some threads are left waiting though
+	the semaphore is already released. Wake up those threads: */
+	
+	sync_arr_wake_threads_if_sema_free();
+
 	if (sync_array_print_long_waits()) {
 		fatal_cnt++;
-		if (fatal_cnt > 5) {
+		if (fatal_cnt > 10) {
 
 			fprintf(stderr,
 				"InnoDB: Error: semaphore wait has lasted"
@@ -2128,7 +2139,7 @@
 
 	fflush(stderr);
 
-	os_thread_sleep(2000000);
+	os_thread_sleep(1000000);
 
 	if (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP) {
 

=== modified file 'storage/innobase/srv/srv0start.c'
--- a/storage/innobase/srv/srv0start.c	2008-03-31 09:37:03 +0000
+++ b/storage/innobase/srv/srv0start.c	2008-06-12 00:08:07 +0000
@@ -202,13 +202,13 @@
 
 		str = srv_parse_megabytes(str, &size);
 
-		if (0 == memcmp(str, ":autoextend",
-				(sizeof ":autoextend") - 1)) {
+		if (0 == strncmp(str, ":autoextend",
+				 (sizeof ":autoextend") - 1)) {
 
 			str += (sizeof ":autoextend") - 1;
 
-			if (0 == memcmp(str, ":max:",
-					(sizeof ":max:") - 1)) {
+			if (0 == strncmp(str, ":max:",
+					 (sizeof ":max:") - 1)) {
 
 				str += (sizeof ":max:") - 1;
 
@@ -290,14 +290,15 @@
 		(*data_file_names)[i] = path;
 		(*data_file_sizes)[i] = size;
 
-		if (0 == memcmp(str, ":autoextend",
-				(sizeof ":autoextend") - 1)) {
+		if (0 == strncmp(str, ":autoextend",
+				 (sizeof ":autoextend") - 1)) {
 
 			*is_auto_extending = TRUE;
 
 			str += (sizeof ":autoextend") - 1;
 
-			if (0 == memcmp(str, ":max:", (sizeof ":max:") - 1)) {
+			if (0 == strncmp(str, ":max:",
+					 (sizeof ":max:") - 1)) {
 
 				str += (sizeof ":max:") - 1;
 
@@ -1122,12 +1123,12 @@
 	if (srv_file_flush_method_str == NULL) {
 		/* These are the default options */
 
-		srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;
+		srv_unix_file_flush_method = SRV_UNIX_FSYNC;
 
 		srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
 #ifndef __WIN__
-	} else if (0 == ut_strcmp(srv_file_flush_method_str, "fdatasync")) {
-		srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;
+	} else if (0 == ut_strcmp(srv_file_flush_method_str, "fsync")) {
+		srv_unix_file_flush_method = SRV_UNIX_FSYNC;
 
 	} else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) {
 		srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;

=== modified file 'storage/innobase/sync/sync0arr.c'
--- a/storage/innobase/sync/sync0arr.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/sync/sync0arr.c	2008-06-12 00:08:07 +0000
@@ -40,24 +40,23 @@
 say 200. In NT 3.51, allocating events seems to be a quadratic
 algorithm, because 10 000 events are created fast, but
 100 000 events takes a couple of minutes to create.
-*/
+
+As of 5.0.30 the above mentioned design is changed. Since now
+OS can handle millions of wait events efficiently, we no longer
+have this concept of each cell of wait array having one event.
+Instead, now the event that a thread wants to wait on is embedded
+in the wait object (mutex or rw_lock). We still keep the global
+wait array for the sake of diagnostics and also to avoid infinite
+wait The error_monitor thread scans the global wait array to signal
+any waiting threads who have missed the signal. */
 
 /* A cell where an individual thread may wait suspended
 until a resource is released. The suspending is implemented
 using an operating system event semaphore. */
 struct sync_cell_struct {
-	/* State of the cell. SC_WAKING_UP means
-	sync_array_struct->n_reserved has been decremented, but the thread
-	in this cell has not waken up yet. When it does, it will set the
-	state to SC_FREE. Note that this is done without the protection of
-	any mutex. */
-	enum { SC_FREE, SC_RESERVED, SC_WAKING_UP } state;
-
 	void*		wait_object;	/* pointer to the object the
-					thread is waiting for; this is not
-					reseted to NULL when a cell is
-					freed. */
-
+					thread is waiting for; if NULL
+					the cell is free for use */
 	mutex_t*	old_wait_mutex;	/* the latest wait mutex in cell */
 	rw_lock_t*	old_wait_rw_lock;/* the latest wait rw-lock in cell */
 	ulint		request_type;	/* lock type requested on the
@@ -71,13 +70,23 @@
 	ibool		waiting;	/* TRUE if the thread has already
 					called sync_array_event_wait
 					on this cell */
-	ibool		event_set;	/* TRUE if the event is set */
-	os_event_t	event;		/* operating system event
-					semaphore handle */
+	ib_longlong	signal_count;	/* We capture the signal_count
+					of the wait_object when we
+					reset the event. This value is
+					then passed on to os_event_wait
+					and we wait only if the event
+					has not been signalled in the
+					period between the reset and
+					wait call. */
 	time_t		reservation_time;/* time when the thread reserved
 					the wait cell */
 };
 
+/* NOTE: It is allowed for a thread to wait
+for an event allocated for the array without owning the
+protecting mutex (depending on the case: OS or database mutex), but
+all changes (set or reset) to the state of the event must be made
+while owning the mutex. */
 struct sync_array_struct {
 	ulint		n_reserved;	/* number of currently reserved
 					cells in the wait array */
@@ -220,12 +229,9 @@
 
 	for (i = 0; i < n_cells; i++) {
 		cell = sync_array_get_nth_cell(arr, i);
-		cell->state = SC_FREE;
-		cell->wait_object = NULL;
-
-		/* Create an operating system event semaphore with no name */
-		cell->event = os_event_create(NULL);
-		cell->event_set = FALSE; /* it is created in reset state */
+	cell->wait_object = NULL;
+		cell->waiting = FALSE;
+		cell->signal_count = 0;
 	}
 
 	return(arr);
@@ -239,19 +245,12 @@
 /*============*/
 	sync_array_t*	arr)	/* in, own: sync wait array */
 {
-	ulint		i;
-	sync_cell_t*	cell;
 	ulint		protection;
 
 	ut_a(arr->n_reserved == 0);
 
 	sync_array_validate(arr);
 
-	for (i = 0; i < arr->n_cells; i++) {
-		cell = sync_array_get_nth_cell(arr, i);
-		os_event_free(cell->event);
-	}
-
 	protection = arr->protection;
 
 	/* Release the mutex protecting the wait array complex */
@@ -285,8 +284,7 @@
 
 	for (i = 0; i < arr->n_cells; i++) {
 		cell = sync_array_get_nth_cell(arr, i);
-
-		if (cell->state == SC_RESERVED) {
+		if (cell->wait_object != NULL) {
 			count++;
 		}
 	}
@@ -296,6 +294,29 @@
 	sync_array_exit(arr);
 }
 
+/***********************************************************************
+Puts the cell event in reset state. */
+static
+ib_longlong
+sync_cell_event_reset(
+/*==================*/
+				/* out: value of signal_count
+				at the time of reset. */
+	ulint		type,	/* in: lock type mutex/rw_lock */
+	void*		object) /* in: the rw_lock/mutex object */
+{
+	if (type == SYNC_MUTEX) {
+		return(os_event_reset(((mutex_t *) object)->event));
+#ifdef __WIN__
+	} else if (type == RW_LOCK_WAIT_EX) {
+		return(os_event_reset(
+		       ((rw_lock_t *) object)->wait_ex_event));
+#endif
+	} else {
+		return(os_event_reset(((rw_lock_t *) object)->event));
+	}
+}
+
 /**********************************************************************
 Reserves a wait array cell for waiting for an object.
 The event of the cell is reset to nonsignalled state. */
@@ -324,21 +345,9 @@
 	for (i = 0; i < arr->n_cells; i++) {
 		cell = sync_array_get_nth_cell(arr, i);
 
-		if (cell->state == SC_FREE) {
-
-			/* We do not check cell->event_set because it is
-			set outside the protection of the sync array mutex
-			and we had a bug regarding it, and since resetting
-			an event when it is not needed does no harm it is
-			safer always to do it. */
-
-			cell->event_set = FALSE;
-			os_event_reset(cell->event);
-
-			cell->state = SC_RESERVED;
-			cell->reservation_time = time(NULL);
-			cell->thread = os_thread_get_curr_id();
-
+		if (cell->wait_object == NULL) {
+
+			cell->waiting = FALSE;
 			cell->wait_object = object;
 
 			if (type == SYNC_MUTEX) {
@@ -348,7 +357,6 @@
 			}
 
 			cell->request_type = type;
-			cell->waiting = FALSE;
 
 			cell->file = file;
 			cell->line = line;
@@ -359,6 +367,16 @@
 
 			sync_array_exit(arr);
 
+			/* Make sure the event is reset and also store
+			the value of signal_count at which the event
+			was reset. */
+			cell->signal_count = sync_cell_event_reset(type,
+								object);
+
+			cell->reservation_time = time(NULL);
+
+			cell->thread = os_thread_get_curr_id();
+
 			return;
 		}
 	}
@@ -369,68 +387,6 @@
 }
 
 /**********************************************************************
-Frees the cell. Note that we don't have any mutex reserved when calling
-this. */
-static
-void
-sync_array_free_cell(
-/*=================*/
-	sync_array_t*	arr,	/* in: wait array */
-	ulint		index)	/* in: index of the cell in array */
-{
-	sync_cell_t*	cell;
-
-	cell = sync_array_get_nth_cell(arr, index);
-
-	ut_a(cell->state == SC_WAKING_UP);
-	ut_a(cell->wait_object != NULL);
-
-	cell->state = SC_FREE;
-}
-
-/**********************************************************************
-Frees the cell safely by reserving the sync array mutex and decrementing
-n_reserved if necessary. Should only be called from mutex_spin_wait. */
-
-void
-sync_array_free_cell_protected(
-/*===========================*/
-	sync_array_t*	arr,	/* in: wait array */
-	ulint		index)	/* in: index of the cell in array */
-{
-	sync_cell_t*	cell;
-
-	sync_array_enter(arr);
-
-	cell = sync_array_get_nth_cell(arr, index);
-
-	ut_a(cell->state != SC_FREE);
-	ut_a(cell->wait_object != NULL);
-
-	/* We only need to decrement n_reserved if it has not already been
-	done by sync_array_signal_object. */
-	if (cell->state == SC_RESERVED) {
-		ut_a(arr->n_reserved > 0);
-		arr->n_reserved--;
-	} else if (cell->state == SC_WAKING_UP) {
-		/* This is tricky; if we don't wait for the event to be
-		signaled, signal_object can set the state of a cell to
-		SC_WAKING_UP, mutex_spin_wait can call this and set the
-		state to SC_FREE, and then signal_object gets around to
-		calling os_set_event for the cell but since it's already
-		been freed things break horribly. */
-
-		sync_array_exit(arr);
-		os_event_wait(cell->event);
-		sync_array_enter(arr);
-	}
-
-	cell->state = SC_FREE;
-
-	sync_array_exit(arr);
-}
-
-/**********************************************************************
 This function should be called when a thread starts to wait on
 a wait array cell. In the debug version this function checks
 if the wait for a semaphore will result in a deadlock, in which
@@ -447,15 +403,28 @@
 
 	ut_a(arr);
 
+	sync_array_enter(arr);
+
 	cell = sync_array_get_nth_cell(arr, index);
 
-	ut_a((cell->state == SC_RESERVED) || (cell->state == SC_WAKING_UP));
 	ut_a(cell->wait_object);
 	ut_a(!cell->waiting);
 	ut_ad(os_thread_get_curr_id() == cell->thread);
 
-	event = cell->event;
-	cell->waiting = TRUE;
+	if (cell->request_type == SYNC_MUTEX) {
+		event = ((mutex_t*) cell->wait_object)->event;
+#ifdef __WIN__
+	/* On windows if the thread about to wait is the one which
+	has set the state of the rw_lock to RW_LOCK_WAIT_EX, then
+	it waits on a special event i.e.: wait_ex_event. */
+	} else if (cell->request_type == RW_LOCK_WAIT_EX) {
+		event = ((rw_lock_t*) cell->wait_object)->wait_ex_event;
+#endif
+	} else {
+		event = ((rw_lock_t*) cell->wait_object)->event;
+	}
+
+		cell->waiting = TRUE;
 
 #ifdef UNIV_SYNC_DEBUG
 
@@ -464,7 +433,6 @@
 	recursively sync_array routines, leading to trouble.
 	rw_lock_debug_mutex freezes the debug lists. */
 
-	sync_array_enter(arr);
 	rw_lock_debug_mutex_enter();
 
 	if (TRUE == sync_array_detect_deadlock(arr, cell, cell, 0)) {
@@ -474,16 +442,16 @@
 	}
 
 	rw_lock_debug_mutex_exit();
+#endif
 	sync_array_exit(arr);
-#endif
-	os_event_wait(event);
+
+	os_event_wait_low(event, cell->signal_count);
 
 	sync_array_free_cell(arr, index);
 }
 
 /**********************************************************************
-Reports info of a wait array cell. Note: sync_array_print_long_waits()
-calls this without mutex protection. */
+Reports info of a wait array cell. */
 static
 void
 sync_array_cell_print(
@@ -503,17 +471,8 @@
 		(ulong) os_thread_pf(cell->thread), cell->file,
 		(ulong) cell->line,
 		difftime(time(NULL), cell->reservation_time));
-	fprintf(file, "Wait array cell state %lu\n", (ulong)cell->state);
-
-	/* If the memory area pointed to by old_wait_mutex /
-	old_wait_rw_lock has been freed, this can crash. */
-
-	if (cell->state != SC_RESERVED) {
-		/* If cell has this state, then even if we are holding the sync
-		array mutex, the wait object may get freed meanwhile. Do not
-		print the wait object then. */
-
-	} else if (type == SYNC_MUTEX) {
+
+	if (type == SYNC_MUTEX) {
 		/* We use old_wait_mutex in case the cell has already
 		been freed meanwhile */
 		mutex = cell->old_wait_mutex;
@@ -531,7 +490,11 @@
 #endif /* UNIV_SYNC_DEBUG */
 			(ulong) mutex->waiters);
 
-	} else if (type == RW_LOCK_EX || type == RW_LOCK_SHARED) {
+	} else if (type == RW_LOCK_EX
+#ifdef __WIN__
+		   || type == RW_LOCK_WAIT_EX
+#endif
+		   || type == RW_LOCK_SHARED) {
 
 		fputs(type == RW_LOCK_EX ? "X-lock on" : "S-lock on", file);
 
@@ -565,8 +528,8 @@
 		ut_error;
 	}
 
-	if (cell->event_set) {
-		fputs("wait is ending\n", file);
+	if (!cell->waiting) {
+		fputs("wait has ended\n", file);
 	}
 }
 
@@ -589,7 +552,7 @@
 
 		cell = sync_array_get_nth_cell(arr, i);
 
-		if ((cell->state == SC_RESERVED)
+		if (cell->wait_object != NULL
 		    && os_thread_eq(cell->thread, thread)) {
 
 			return(cell);	/* Found */
@@ -679,7 +642,7 @@
 
 	depth++;
 
-	if (cell->event_set || !cell->waiting) {
+	if (!cell->waiting) {
 
 		return(FALSE); /* No deadlock here */
 	}
@@ -704,10 +667,8 @@
 						       depth);
 			if (ret) {
 				fprintf(stderr,
-					"Mutex %p owned by thread %lu"
-					" file %s line %lu\n",
-					(void*) mutex,
-					(ulong) os_thread_pf(mutex->thread_id),
+			"Mutex %p owned by thread %lu file %s line %lu\n",
+					mutex, (ulong) os_thread_pf(mutex->thread_id),
 					mutex->file_name, (ulong) mutex->line);
 				sync_array_cell_print(stderr, cell);
 
@@ -717,7 +678,8 @@
 
 		return(FALSE); /* No deadlock */
 
-	} else if (cell->request_type == RW_LOCK_EX) {
+	} else if (cell->request_type == RW_LOCK_EX
+		   || cell->request_type == RW_LOCK_WAIT_EX) {
 
 		lock = cell->wait_object;
 
@@ -816,7 +778,8 @@
 			return(TRUE);
 		}
 
-	} else if (cell->request_type == RW_LOCK_EX) {
+	} else if (cell->request_type == RW_LOCK_EX
+		   || cell->request_type == RW_LOCK_WAIT_EX) {
 
 		lock = cell->wait_object;
 
@@ -845,101 +808,47 @@
 	return(FALSE);
 }
 
-/**************************************************************************
-Looks for the cells in the wait array which refer to the wait object
-specified, and sets their corresponding events to the signaled state. In this
-way releases the threads waiting for the object to contend for the object.
-It is possible that no such cell is found, in which case does nothing. */
+/**********************************************************************
+Frees the cell. NOTE! sync_array_wait_event frees the cell
+automatically! */
 
 void
-sync_array_signal_object(
-/*=====================*/
+sync_array_free_cell(
+/*=================*/
 	sync_array_t*	arr,	/* in: wait array */
-	void*		object)	/* in: wait object */
+	ulint		index)  /* in: index of the cell in array */
 {
 	sync_cell_t*	cell;
-	ulint		count;
-	ulint		i;
-	ulint		res_count;
-
-	/* We store the addresses of cells we need to signal and signal
-	them only after we have released the sync array's mutex (for
-	performance reasons). cell_count is the number of such cells, and
-	cell_ptr points to the first one. If there are less than
-	UT_ARR_SIZE(cells) of them, cell_ptr == &cells[0], otherwise
-	cell_ptr points to malloc'd memory that we must free. */
-
-	sync_cell_t*	cells[100];
-	sync_cell_t**	cell_ptr = &cells[0];
-	ulint		cell_count = 0;
-	ulint		cell_max_count = UT_ARR_SIZE(cells);
-
-	ut_a(100 == cell_max_count);
-
+
+	sync_array_enter(arr);
+
+	cell = sync_array_get_nth_cell(arr, index);
+
+	ut_a(cell->wait_object != NULL);
+
+	cell->waiting = FALSE;
+	cell->wait_object =  NULL;
+	cell->signal_count = 0;
+
+	ut_a(arr->n_reserved > 0);
+	arr->n_reserved--;
+
+	sync_array_exit(arr);
+}
+
+/**************************************************************************
+Increments the signalled count. */
+
+void
+sync_array_object_signalled(
+/*========================*/
+	sync_array_t*	arr)	/* in: wait array */
+{
 	sync_array_enter(arr);
 
 	arr->sg_count++;
 
-	i = 0;
-	count = 0;
-
-	/* We need to store this to a local variable because it is modified
-	inside the loop */
-	res_count = arr->n_reserved;
-
-	while (count < res_count) {
-
-		cell = sync_array_get_nth_cell(arr, i);
-
-		if (cell->state == SC_RESERVED) {
-
-			count++;
-			if (cell->wait_object == object) {
-				cell->state = SC_WAKING_UP;
-
-				ut_a(arr->n_reserved > 0);
-				arr->n_reserved--;
-
-				if (cell_count == cell_max_count) {
-					sync_cell_t** old_cell_ptr = cell_ptr;
-					size_t old_size, new_size;
-
-					old_size = cell_max_count
-						* sizeof(sync_cell_t*);
-					cell_max_count *= 2;
-					new_size = cell_max_count
-						* sizeof(sync_cell_t*);
-
-					cell_ptr = malloc(new_size);
-					ut_a(cell_ptr);
-					memcpy(cell_ptr, old_cell_ptr,
-					       old_size);
-
-					if (old_cell_ptr != &cells[0]) {
-						free(old_cell_ptr);
-					}
-				}
-
-				cell_ptr[cell_count] = cell;
-				cell_count++;
-			}
-		}
-
-		i++;
-	}
-
 	sync_array_exit(arr);
-
-	for (i = 0; i < cell_count; i++) {
-		cell = cell_ptr[i];
-
-		cell->event_set = TRUE;
-		os_event_set(cell->event);
-	}
-
-	if (cell_ptr != &cells[0]) {
-		free(cell_ptr);
-	}
 }
 
 /**************************************************************************
@@ -959,33 +868,41 @@
 	sync_cell_t*	cell;
 	ulint		count;
 	ulint		i;
-	ulint		res_count;
 
 	sync_array_enter(arr);
 
 	i = 0;
 	count = 0;
 
-	/* We need to store this to a local variable because it is modified
-	inside the loop */
-
-	res_count = arr->n_reserved;
-
-	while (count < res_count) {
+	while (count < arr->n_reserved) {
 
 		cell = sync_array_get_nth_cell(arr, i);
 
-		if (cell->state == SC_RESERVED) {
+		if (cell->wait_object != NULL) {
 
 			count++;
 
 			if (sync_arr_cell_can_wake_up(cell)) {
-				cell->state = SC_WAKING_UP;
-				cell->event_set = TRUE;
-				os_event_set(cell->event);
-
-				ut_a(arr->n_reserved > 0);
-				arr->n_reserved--;
+
+				if (cell->request_type == SYNC_MUTEX) {
+					mutex_t*	mutex;
+
+					mutex = cell->wait_object;
+					os_event_set(mutex->event);
+#ifdef __WIN__
+				} else if (cell->request_type
+					   == RW_LOCK_WAIT_EX) {
+					rw_lock_t*	lock;
+
+					lock = cell->wait_object;
+					os_event_set(lock->wait_ex_event);
+#endif
+				} else {
+					rw_lock_t*	lock;
+
+					lock = cell->wait_object;
+					os_event_set(lock->event);
+				}
 			}
 		}
 
@@ -1015,7 +932,7 @@
 
 		cell = sync_array_get_nth_cell(sync_primary_wait_array, i);
 
-		if ((cell->state != SC_FREE)
+		if (cell->wait_object != NULL && cell->waiting
 		    && difftime(time(NULL), cell->reservation_time) > 240) {
 			fputs("InnoDB: Warning: a long semaphore wait:\n",
 			      stderr);
@@ -1023,7 +940,7 @@
 			noticed = TRUE;
 		}
 
-		if ((cell->state != SC_FREE)
+		if (cell->wait_object != NULL && cell->waiting
 		    && difftime(time(NULL), cell->reservation_time)
 		    > fatal_timeout) {
 			fatal = TRUE;
@@ -1072,20 +989,25 @@
 				mutex */
 {
 	sync_cell_t*	cell;
+	ulint		count;
 	ulint		i;
 
 	fprintf(file,
-		"OS WAIT ARRAY INFO: reservation count %ld,"
-		" signal count %ld\n",
-		(long) arr->res_count,
-		(long) arr->sg_count);
-	for (i = 0; i < arr->n_cells; i++) {
+		"OS WAIT ARRAY INFO: reservation count %ld, signal count %ld\n",
+						(long) arr->res_count, (long) arr->sg_count);
+	i = 0;
+	count = 0;
+
+	while (count < arr->n_reserved) {
 
 		cell = sync_array_get_nth_cell(arr, i);
 
-		if (cell->state != SC_FREE) {
+	if (cell->wait_object != NULL) {
+		count++;
 			sync_array_cell_print(file, cell);
 		}
+
+		i++;
 	}
 }
 

=== modified file 'storage/innobase/sync/sync0rw.c'
--- a/storage/innobase/sync/sync0rw.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/sync/sync0rw.c	2008-06-12 00:08:07 +0000
@@ -151,6 +151,11 @@
 	lock->last_x_file_name = "not yet reserved";
 	lock->last_s_line = 0;
 	lock->last_x_line = 0;
+	lock->event = os_event_create(NULL);
+
+#ifdef __WIN__
+	lock->wait_ex_event = os_event_create(NULL);
+#endif
 
 	mutex_enter(&rw_lock_list_mutex);
 
@@ -184,6 +189,11 @@
 	mutex_free(rw_lock_get_mutex(lock));
 
 	mutex_enter(&rw_lock_list_mutex);
+	os_event_free(lock->event);
+
+#ifdef __WIN__
+	os_event_free(lock->wait_ex_event);
+#endif
 
 	if (UT_LIST_GET_PREV(list, lock)) {
 		ut_a(UT_LIST_GET_PREV(list, lock)->magic_n == RW_LOCK_MAGIC_N);
@@ -544,7 +554,15 @@
 	rw_x_system_call_count++;
 
 	sync_array_reserve_cell(sync_primary_wait_array,
-				lock, RW_LOCK_EX,
+				lock,
+#ifdef __WIN__
+				/* On windows RW_LOCK_WAIT_EX signifies
+				that this thread should wait on the
+				special wait_ex_event. */
+				(state == RW_LOCK_WAIT_EX)
+				 ? RW_LOCK_WAIT_EX :
+#endif
+				RW_LOCK_EX,
 				file_name, line,
 				&index);
 

=== modified file 'storage/innobase/sync/sync0sync.c'
--- a/storage/innobase/sync/sync0sync.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/sync/sync0sync.c	2008-06-12 00:08:07 +0000
@@ -95,17 +95,47 @@
 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.
+LEMMA 1: After a thread resets the event of a mutex (or rw_lock), some
+=======
+thread will eventually call os_event_set() on that particular event.
+Thus no infinite wait is possible in this case.
 
 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
-sync_array_signal_object with the mutex as an argument.
+os_event_set() with the mutex as an argument.
+Q.E.D.
+
+LEMMA 2: If an os_event_set() call is made after some thread has called
+=======
+the os_event_reset() and before it starts wait on that event, the call
+will not be lost to the second thread. This is true even if there is an
+intervening call to os_event_reset() by another thread.
+Thus no infinite wait is possible in this case.
+
+Proof (non-windows platforms): os_event_reset() returns a monotonically
+increasing value of signal_count. This value is increased at every
+call of os_event_set() If thread A has called os_event_reset() followed
+by thread B calling os_event_set() and then some other thread C calling
+os_event_reset(), the is_set flag of the event will be set to FALSE;
+but now if thread A calls os_event_wait_low() with the signal_count
+value returned from the earlier call of os_event_reset(), it will
+return immediately without waiting.
+Q.E.D.
+
+Proof (windows): If there is a writer thread which is forced to wait for
+the lock, it may be able to set the state of rw_lock to RW_LOCK_WAIT_EX
+The design of rw_lock ensures that there is one and only one thread
+that is able to change the state to RW_LOCK_WAIT_EX and this thread is
+guaranteed to acquire the lock after it is released by the current
+holders and before any other waiter gets the lock.
+On windows this thread waits on a separate event i.e.: wait_ex_event.
+Since only one thread can wait on this event there is no chance
+of this event getting reset before the writer starts wait on it.
+Therefore, this thread is guaranteed to catch the os_set_event()
+signalled unconditionally at the release of the lock.
 Q.E.D. */
 
 /* The number of system calls made in this module. Intended for performance
@@ -217,6 +247,7 @@
 	os_fast_mutex_init(&(mutex->os_fast_mutex));
 	mutex->lock_word = 0;
 #endif
+	mutex->event = os_event_create(NULL);
 	mutex_set_waiters(mutex, 0);
 #ifdef UNIV_DEBUG
 	mutex->magic_n = MUTEX_MAGIC_N;
@@ -300,6 +331,8 @@
 		mutex_exit(&mutex_list_mutex);
 	}
 
+	os_event_free(mutex->event);
+
 #if !defined(_WIN32) || !defined(UNIV_CAN_USE_X86_ASSEMBLER)
 	os_fast_mutex_free(&(mutex->os_fast_mutex));
 #endif
@@ -509,8 +542,7 @@
 		if (mutex_test_and_set(mutex) == 0) {
 			/* Succeeded! Free the reserved wait cell */
 
-			sync_array_free_cell_protected(sync_primary_wait_array,
-						       index);
+			sync_array_free_cell(sync_primary_wait_array, index);
 
 			ut_d(mutex->thread_id = os_thread_get_curr_id());
 #ifdef UNIV_SYNC_DEBUG
@@ -591,8 +623,8 @@
 
 	/* 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);
+	os_event_set(mutex->event);
+	sync_array_object_signalled(sync_primary_wait_array);
 }
 
 #ifdef UNIV_SYNC_DEBUG
@@ -1130,6 +1162,7 @@
 		break;
 	case SYNC_TREE_NODE:
 		ut_a(sync_thread_levels_contain(array, SYNC_INDEX_TREE)
+		     || sync_thread_levels_contain(array, SYNC_DICT_OPERATION)
 		     || sync_thread_levels_g(array, SYNC_TREE_NODE - 1));
 		break;
 	case SYNC_TREE_NODE_NEW:

=== modified file 'storage/innobase/trx/trx0trx.c'
--- a/storage/innobase/trx/trx0trx.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/trx/trx0trx.c	2008-06-12 00:08:07 +0000
@@ -757,8 +757,8 @@
 		mutex_enter(&(rseg->mutex));
 
 		if (trx->insert_undo != NULL) {
-			trx_undo_set_state_at_finish(trx, trx->insert_undo,
-						     &mtr);
+			trx_undo_set_state_at_finish(
+				rseg, trx, trx->insert_undo, &mtr);
 		}
 
 		undo = trx->update_undo;
@@ -774,7 +774,7 @@
 			transaction commit for this transaction. */
 
 			update_hdr_page = trx_undo_set_state_at_finish(
-				trx, undo, &mtr);
+				rseg, trx, undo, &mtr);
 
 			/* We have to do the cleanup for the update log while
 			holding the rseg mutex because update log headers
@@ -933,6 +933,7 @@
 	trx->rseg = NULL;
 	trx->undo_no = ut_dulint_zero;
 	trx->last_sql_stat_start.least_undo_no = ut_dulint_zero;
+	trx->mysql_query_str = NULL;
 
 	ut_ad(UT_LIST_GET_LEN(trx->wait_thrs) == 0);
 	ut_ad(UT_LIST_GET_LEN(trx->trx_locks) == 0);

=== modified file 'storage/innobase/trx/trx0undo.c'
--- a/storage/innobase/trx/trx0undo.c	2008-03-31 09:37:03 +0000
+++ b/storage/innobase/trx/trx0undo.c	2008-06-12 00:08:07 +0000
@@ -1725,6 +1725,7 @@
 /*=========================*/
 				/* out: undo log segment header page,
 				x-latched */
+	trx_rseg_t*	rseg,	/* in: rollback segment memory object */
 	trx_t*		trx __attribute__((unused)), /* in: transaction */
 	trx_undo_t*	undo,	/* in: undo log memory copy */
 	mtr_t*		mtr)	/* in: mtr */
@@ -1734,7 +1735,10 @@
 	page_t*		undo_page;
 	ulint		state;
 
-	ut_ad(trx && undo && mtr);
+	ut_ad(trx);
+	ut_ad(undo);
+	ut_ad(mtr);
+	ut_ad(mutex_own(&rseg->mutex));
 
 	if (undo->id >= TRX_RSEG_N_SLOTS) {
 		fprintf(stderr, "InnoDB: Error: undo->id is %lu\n",
@@ -1748,9 +1752,23 @@
 	seg_hdr = undo_page + TRX_UNDO_SEG_HDR;
 	page_hdr = undo_page + TRX_UNDO_PAGE_HDR;
 
-	if (undo->size == 1 && mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE)
-	    < TRX_UNDO_PAGE_REUSE_LIMIT) {
-		state = TRX_UNDO_CACHED;
+	if (undo->size == 1
+	    && mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE)
+	       < TRX_UNDO_PAGE_REUSE_LIMIT) {
+
+		/* This is a heuristic to avoid the problem of all UNDO
+		slots ending up in one of the UNDO lists. Previously if
+		the server crashed with all the slots in one of the lists,
+		transactions that required the slots of a different type
+		would fail for lack of slots. */
+
+		if (UT_LIST_GET_LEN(rseg->update_undo_list) < 500
+		    && UT_LIST_GET_LEN(rseg->insert_undo_list) < 500) {
+
+			state = TRX_UNDO_CACHED;
+		} else {
+			state = TRX_UNDO_TO_FREE;
+		}
 
 	} else if (undo->type == TRX_UNDO_INSERT) {
 

=== modified file 'storage/innobase/ut/ut0ut.c'
--- a/storage/innobase/ut/ut0ut.c	2007-11-06 22:42:58 +0000
+++ b/storage/innobase/ut/ut0ut.c	2008-06-12 00:08:07 +0000
@@ -112,19 +112,45 @@
 }
 
 /**************************************************************
-Returns system time. */
+Returns system time.
+Upon successful completion, the value 0 is returned; otherwise the
+value -1 is returned and the global variable errno is set to indicate the
+error. */
 
-void
+int
 ut_usectime(
 /*========*/
+			/* out: 0 on success, -1 otherwise */
 	ulint*	sec,	/* out: seconds since the Epoch */
 	ulint*	ms)	/* out: microseconds since the Epoch+*sec */
 {
 	struct timeval	tv;
-
-	ut_gettimeofday(&tv, NULL);
-	*sec = (ulint) tv.tv_sec;
-	*ms  = (ulint) tv.tv_usec;
+	int		ret;
+	int		errno_gettimeofday;
+	int		i;
+
+	for (i = 0; i < 10; i++) {
+
+		ret = ut_gettimeofday(&tv, NULL);
+
+		if (ret == -1) {
+			errno_gettimeofday = errno;
+			ut_print_timestamp(stderr);
+			fprintf(stderr, "  InnoDB: gettimeofday(): %s\n",
+				strerror(errno_gettimeofday));
+			os_thread_sleep(100000);  /* 0.1 sec */
+			errno = errno_gettimeofday;
+		} else {
+			break;
+		}
+	}
+
+	if (ret != -1) {
+		*sec = (ulint) tv.tv_sec;
+		*ms  = (ulint) tv.tv_usec;
+	}
+
+	return(ret);
 }
 
 /**************************************************************

--===============3764427964322729948==
Content-Type: text/plain; charset=us-ascii

-- 
MySQL Code Commits Mailing List
For list archives: http://lists.mysql.com/commits
To unsubscribe:    http://lists.mysql.com/commits?unsub=commits@bugs.mysql.com
--===============3764427964322729948==--
[21 Jun 2008 0:15] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/48277

0 and the type is not unsigned then we reset the value to 0. */
 static
-ib_longlong
+ib_ulonglong
 row_search_autoinc_read_column(
 /*===========================*/
 					/* out: value read from the column */
@@ -4593,10 +4614,8 @@
 {
 	ulint		len;
 	const byte*	data;
-	ib_longlong	value;
+	ib_ulonglong	value;
 	mem_heap_t*	heap = NULL;
-	/* Our requirement is that dest should be word aligned. */
-	byte		dest[sizeof(value)];
 	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
 	ulint*		offsets	= offsets_;
 
@@ -4614,41 +4633,14 @@
 	ut_a(len != UNIV_SQL_NULL);
 	ut_a(len <= sizeof value);
 
-	mach_read_int_type(dest, data, len, unsigned_type);
-
-	/* The assumption here is that the AUTOINC value can't be negative
-	and that dest is word aligned. */
-	switch (len) {
-	case 8:
-		value = *(ib_longlong*) dest;
-		break;
-
-	case 4:
-		value = *(ib_uint32_t*) dest;
-		break;
-
-	case 3:
-		value = *(ib_uint32_t*) dest;
-		value &= 0xFFFFFF;
-		break;
-
-	case 2:
-		value = *(uint16 *) dest;
-		break;
-
-	case 1:
-		value = *dest;
-		break;
-
-	default:
-		ut_error;
-	}
+	value = mach_read_int_type(data, len, unsigned_type);
 
 	if (UNIV_LIKELY_NULL(heap)) {
 		mem_heap_free(heap);
 	}
 
-	if (!unsigned_type && value < 0) {
+	/* We assume that the autoinc counter can't be negative. */
+	if (!unsigned_type && (ib_longlong) value < 0) {
 		value = 0;
 	}
 
@@ -4687,7 +4679,7 @@
 					column name can't be found in index */
 	dict_index_t*	index,		/* in: index to search */
 	const char*	col_name,	/* in: name of autoinc column */
-	ib_longlong*	value)		/* out: AUTOINC value read */
+	ib_ulonglong*	value)		/* out: AUTOINC value read */
 {
 	ulint		i;
 	ulint		n_cols;

=== modified file 'storage/innobase/srv/srv0srv.c'
--- a/storage/innobase/srv/srv0srv.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/srv/srv0srv.c	2008-06-12 00:08:07 +0000
@@ -192,7 +192,7 @@
 ulong	srv_insert_buffer_batch_size = 20;
 
 char*	srv_file_flush_method_str = NULL;
-ulint	srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;
+ulint	srv_unix_file_flush_method = SRV_UNIX_FSYNC;
 ulint	srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
 
 ulint	srv_max_n_open_files	  = 300;
@@ -1453,8 +1453,11 @@
 		srv_n_lock_wait_count++;
 		srv_n_lock_wait_current_count++;
 
-		ut_usectime(&sec, &ms);
-		start_time = (ib_longlong)sec * 1000000 + ms;
+		if (ut_usectime(&sec, &ms) == -1) {
+			start_time = -1;
+		} else {
+			start_time = (ib_longlong)sec * 1000000 + ms;
+		}
 	}
 	/* Wake the lock timeout monitor thread, if it is suspended */
 
@@ -1508,14 +1511,20 @@
 	wait_time = ut_difftime(ut_time(), slot->suspend_time);
 
 	if (thr->lock_state == QUE_THR_LOCK_ROW) {
-		ut_usectime(&sec, &ms);
-		finish_time = (ib_longlong)sec * 1000000 + ms;
+		if (ut_usectime(&sec, &ms) == -1) {
+			finish_time = -1;
+		} else {
+			finish_time = (ib_longlong)sec * 1000000 + ms;
+		}
 
 		diff_time = (ulint) (finish_time - start_time);
 
 		srv_n_lock_wait_current_count--;
 		srv_n_lock_wait_time = srv_n_lock_wait_time + diff_time;
-		if (diff_time > srv_n_lock_max_wait_time) {
+		if (diff_time > srv_n_lock_max_wait_time &&
+		    /* only update the variable if we successfully
+		    retrieved the start and finish times. See Bug#36819. */
+		    start_time != -1 && finish_time != -1) {
 			srv_n_lock_max_wait_time = diff_time;
 		}
 	}
@@ -1825,8 +1834,10 @@
 		= UT_LIST_GET_LEN(buf_pool->flush_list);
 	export_vars.innodb_buffer_pool_pages_free
 		= UT_LIST_GET_LEN(buf_pool->free);
+#ifdef UNIV_DEBUG
 	export_vars.innodb_buffer_pool_pages_latched
 		= buf_get_latched_pages_number();
+#endif /* UNIV_DEBUG */
 	export_vars.innodb_buffer_pool_pages_total = buf_pool->curr_size;
 
 	export_vars.innodb_buffer_pool_pages_misc = buf_pool->max_size
@@ -1904,12 +1915,6 @@
 
 	os_thread_sleep(1000000);
 
-	/* In case mutex_exit is not a memory barrier, it is
-	theoretically possible some threads are left waiting though
-	the semaphore is already released. Wake up those threads: */
-
-	sync_arr_wake_threads_if_sema_free();
-
 	current_time = time(NULL);
 
 	time_elapsed = difftime(current_time, last_monitor_time);
@@ -2106,9 +2111,15 @@
 		srv_refresh_innodb_monitor_stats();
 	}
 
+	/* In case mutex_exit is not a memory barrier, it is
+	theoretically possible some threads are left waiting though
+	the semaphore is already released. Wake up those threads: */
+	
+	sync_arr_wake_threads_if_sema_free();
+
 	if (sync_array_print_long_waits()) {
 		fatal_cnt++;
-		if (fatal_cnt > 5) {
+		if (fatal_cnt > 10) {
 
 			fprintf(stderr,
 				"InnoDB: Error: semaphore wait has lasted"
@@ -2128,7 +2139,7 @@
 
 	fflush(stderr);
 
-	os_thread_sleep(2000000);
+	os_thread_sleep(1000000);
 
 	if (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP) {
 

=== modified file 'storage/innobase/srv/srv0start.c'
--- a/storage/innobase/srv/srv0start.c	2008-03-31 09:37:03 +0000
+++ b/storage/innobase/srv/srv0start.c	2008-06-12 00:08:07 +0000
@@ -202,13 +202,13 @@
 
 		str = srv_parse_megabytes(str, &size);
 
-		if (0 == memcmp(str, ":autoextend",
-				(sizeof ":autoextend") - 1)) {
+		if (0 == strncmp(str, ":autoextend",
+				 (sizeof ":autoextend") - 1)) {
 
 			str += (sizeof ":autoextend") - 1;
 
-			if (0 == memcmp(str, ":max:",
-					(sizeof ":max:") - 1)) {
+			if (0 == strncmp(str, ":max:",
+					 (sizeof ":max:") - 1)) {
 
 				str += (sizeof ":max:") - 1;
 
@@ -290,14 +290,15 @@
 		(*data_file_names)[i] = path;
 		(*data_file_sizes)[i] = size;
 
-		if (0 == memcmp(str, ":autoextend",
-				(sizeof ":autoextend") - 1)) {
+		if (0 == strncmp(str, ":autoextend",
+				 (sizeof ":autoextend") - 1)) {
 
 			*is_auto_extending = TRUE;
 
 			str += (sizeof ":autoextend") - 1;
 
-			if (0 == memcmp(str, ":max:", (sizeof ":max:") - 1)) {
+			if (0 == strncmp(str, ":max:",
+					 (sizeof ":max:") - 1)) {
 
 				str += (sizeof ":max:") - 1;
 
@@ -1122,12 +1123,12 @@
 	if (srv_file_flush_method_str == NULL) {
 		/* These are the default options */
 
-		srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;
+		srv_unix_file_flush_method = SRV_UNIX_FSYNC;
 
 		srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
 #ifndef __WIN__
-	} else if (0 == ut_strcmp(srv_file_flush_method_str, "fdatasync")) {
-		srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;
+	} else if (0 == ut_strcmp(srv_file_flush_method_str, "fsync")) {
+		srv_unix_file_flush_method = SRV_UNIX_FSYNC;
 
 	} else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) {
 		srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;

=== modified file 'storage/innobase/sync/sync0arr.c'
--- a/storage/innobase/sync/sync0arr.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/sync/sync0arr.c	2008-06-12 00:08:07 +0000
@@ -40,24 +40,23 @@
 say 200. In NT 3.51, allocating events seems to be a quadratic
 algorithm, because 10 000 events are created fast, but
 100 000 events takes a couple of minutes to create.
-*/
+
+As of 5.0.30 the above mentioned design is changed. Since now
+OS can handle millions of wait events efficiently, we no longer
+have this concept of each cell of wait array having one event.
+Instead, now the event that a thread wants to wait on is embedded
+in the wait object (mutex or rw_lock). We still keep the global
+wait array for the sake of diagnostics and also to avoid infinite
+wait The error_monitor thread scans the global wait array to signal
+any waiting threads who have missed the signal. */
 
 /* A cell where an individual thread may wait suspended
 until a resource is released. The suspending is implemented
 using an operating system event semaphore. */
 struct sync_cell_struct {
-	/* State of the cell. SC_WAKING_UP means
-	sync_array_struct->n_reserved has been decremented, but the thread
-	in this cell has not waken up yet. When it does, it will set the
-	state to SC_FREE. Note that this is done without the protection of
-	any mutex. */
-	enum { SC_FREE, SC_RESERVED, SC_WAKING_UP } state;
-
 	void*		wait_object;	/* pointer to the object the
-					thread is waiting for; this is not
-					reseted to NULL when a cell is
-					freed. */
-
+					thread is waiting for; if NULL
+					the cell is free for use */
 	mutex_t*	old_wait_mutex;	/* the latest wait mutex in cell */
 	rw_lock_t*	old_wait_rw_lock;/* the latest wait rw-lock in cell */
 	ulint		request_type;	/* lock type requested on the
@@ -71,13 +70,23 @@
 	ibool		waiting;	/* TRUE if the thread has already
 					called sync_array_event_wait
 					on this cell */
-	ibool		event_set;	/* TRUE if the event is set */
-	os_event_t	event;		/* operating system event
-					semaphore handle */
+	ib_longlong	signal_count;	/* We capture the signal_count
+					of the wait_object when we
+					reset the event. This value is
+					then passed on to os_event_wait
+					and we wait only if the event
+					has not been signalled in the
+					period between the reset and
+					wait call. */
 	time_t		reservation_time;/* time when the thread reserved
 					the wait cell */
 };
 
+/* NOTE: It is allowed for a thread to wait
+for an event allocated for the array without owning the
+protecting mutex (depending on the case: OS or database mutex), but
+all changes (set or reset) to the state of the event must be made
+while owning the mutex. */
 struct sync_array_struct {
 	ulint		n_reserved;	/* number of currently reserved
 					cells in the wait array */
@@ -220,12 +229,9 @@
 
 	for (i = 0; i < n_cells; i++) {
 		cell = sync_array_get_nth_cell(arr, i);
-		cell->state = SC_FREE;
-		cell->wait_object = NULL;
-
-		/* Create an operating system event semaphore with no name */
-		cell->event = os_event_create(NULL);
-		cell->event_set = FALSE; /* it is created in reset state */
+	cell->wait_object = NULL;
+		cell->waiting = FALSE;
+		cell->signal_count = 0;
 	}
 
 	return(arr);
@@ -239,19 +245,12 @@
 /*============*/
 	sync_array_t*	arr)	/* in, own: sync wait array */
 {
-	ulint		i;
-	sync_cell_t*	cell;
 	ulint		protection;
 
 	ut_a(arr->n_reserved == 0);
 
 	sync_array_validate(arr);
 
-	for (i = 0; i < arr->n_cells; i++) {
-		cell = sync_array_get_nth_cell(arr, i);
-		os_event_free(cell->event);
-	}
-
 	protection = arr->protection;
 
 	/* Release the mutex protecting the wait array complex */
@@ -285,8 +284,7 @@
 
 	for (i = 0; i < arr->n_cells; i++) {
 		cell = sync_array_get_nth_cell(arr, i);
-
-		if (cell->state == SC_RESERVED) {
+		if (cell->wait_object != NULL) {
 			count++;
 		}
 	}
@@ -296,6 +294,29 @@
 	sync_array_exit(arr);
 }
 
+/***********************************************************************
+Puts the cell event in reset state. */
+static
+ib_longlong
+sync_cell_event_reset(
+/*==================*/
+				/* out: value of signal_count
+				at the time of reset. */
+	ulint		type,	/* in: lock type mutex/rw_lock */
+	void*		object) /* in: the rw_lock/mutex object */
+{
+	if (type == SYNC_MUTEX) {
+		return(os_event_reset(((mutex_t *) object)->event));
+#ifdef __WIN__
+	} else if (type == RW_LOCK_WAIT_EX) {
+		return(os_event_reset(
+		       ((rw_lock_t *) object)->wait_ex_event));
+#endif
+	} else {
+		return(os_event_reset(((rw_lock_t *) object)->event));
+	}
+}
+
 /**********************************************************************
 Reserves a wait array cell for waiting for an object.
 The event of the cell is reset to nonsignalled state. */
@@ -324,21 +345,9 @@
 	for (i = 0; i < arr->n_cells; i++) {
 		cell = sync_array_get_nth_cell(arr, i);
 
-		if (cell->state == SC_FREE) {
-
-			/* We do not check cell->event_set because it is
-			set outside the protection of the sync array mutex
-			and we had a bug regarding it, and since resetting
-			an event when it is not needed does no harm it is
-			safer always to do it. */
-
-			cell->event_set = FALSE;
-			os_event_reset(cell->event);
-
-			cell->state = SC_RESERVED;
-			cell->reservation_time = time(NULL);
-			cell->thread = os_thread_get_curr_id();
-
+		if (cell->wait_object == NULL) {
+
+			cell->waiting = FALSE;
 			cell->wait_object = object;
 
 			if (type == SYNC_MUTEX) {
@@ -348,7 +357,6 @@
 			}
 
 			cell->request_type = type;
-			cell->waiting = FALSE;
 
 			cell->file = file;
 			cell->line = line;
@@ -359,6 +367,16 @@
 
 			sync_array_exit(arr);
 
+			/* Make sure the event is reset and also store
+			the value of signal_count at which the event
+			was reset. */
+			cell->signal_count = sync_cell_event_reset(type,
+								object);
+
+			cell->reservation_time = time(NULL);
+
+			cell->thread = os_thread_get_curr_id();
+
 			return;
 		}
 	}
@@ -369,68 +387,6 @@
 }
 
 /**********************************************************************
-Frees the cell. Note that we don't have any mutex reserved when calling
-this. */
-static
-void
-sync_array_free_cell(
-/*=================*/
-	sync_array_t*	arr,	/* in: wait array */
-	ulint		index)	/* in: index of the cell in array */
-{
-	sync_cell_t*	cell;
-
-	cell = sync_array_get_nth_cell(arr, index);
-
-	ut_a(cell->state == SC_WAKING_UP);
-	ut_a(cell->wait_object != NULL);
-
-	cell->state = SC_FREE;
-}
-
-/**********************************************************************
-Frees the cell safely by reserving the sync array mutex and decrementing
-n_reserved if necessary. Should only be called from mutex_spin_wait. */
-
-void
-sync_array_free_cell_protected(
-/*===========================*/
-	sync_array_t*	arr,	/* in: wait array */
-	ulint		index)	/* in: index of the cell in array */
-{
-	sync_cell_t*	cell;
-
-	sync_array_enter(arr);
-
-	cell = sync_array_get_nth_cell(arr, index);
-
-	ut_a(cell->state != SC_FREE);
-	ut_a(cell->wait_object != NULL);
-
-	/* We only need to decrement n_reserved if it has not already been
-	done by sync_array_signal_object. */
-	if (cell->state == SC_RESERVED) {
-		ut_a(arr->n_reserved > 0);
-		arr->n_reserved--;
-	} else if (cell->state == SC_WAKING_UP) {
-		/* This is tricky; if we don't wait for the event to be
-		signaled, signal_object can set the state of a cell to
-		SC_WAKING_UP, mutex_spin_wait can call this and set the
-		state to SC_FREE, and then signal_object gets around to
-		calling os_set_event for the cell but since it's already
-		been freed things break horribly. */
-
-		sync_array_exit(arr);
-		os_event_wait(cell->event);
-		sync_array_enter(arr);
-	}
-
-	cell->state = SC_FREE;
-
-	sync_array_exit(arr);
-}
-
-/**********************************************************************
 This function should be called when a thread starts to wait on
 a wait array cell. In the debug version this function checks
 if the wait for a semaphore will result in a deadlock, in which
@@ -447,15 +403,28 @@
 
 	ut_a(arr);
 
+	sync_array_enter(arr);
+
 	cell = sync_array_get_nth_cell(arr, index);
 
-	ut_a((cell->state == SC_RESERVED) || (cell->state == SC_WAKING_UP));
 	ut_a(cell->wait_object);
 	ut_a(!cell->waiting);
 	ut_ad(os_thread_get_curr_id() == cell->thread);
 
-	event = cell->event;
-	cell->waiting = TRUE;
+	if (cell->request_type == SYNC_MUTEX) {
+		event = ((mutex_t*) cell->wait_object)->event;
+#ifdef __WIN__
+	/* On windows if the thread about to wait is the one which
+	has set the state of the rw_lock to RW_LOCK_WAIT_EX, then
+	it waits on a special event i.e.: wait_ex_event. */
+	} else if (cell->request_type == RW_LOCK_WAIT_EX) {
+		event = ((rw_lock_t*) cell->wait_object)->wait_ex_event;
+#endif
+	} else {
+		event = ((rw_lock_t*) cell->wait_object)->event;
+	}
+
+		cell->waiting = TRUE;
 
 #ifdef UNIV_SYNC_DEBUG
 
@@ -464,7 +433,6 @@
 	recursively sync_array routines, leading to trouble.
 	rw_lock_debug_mutex freezes the debug lists. */
 
-	sync_array_enter(arr);
 	rw_lock_debug_mutex_enter();
 
 	if (TRUE == sync_array_detect_deadlock(arr, cell, cell, 0)) {
@@ -474,16 +442,16 @@
 	}
 
 	rw_lock_debug_mutex_exit();
+#endif
 	sync_array_exit(arr);
-#endif
-	os_event_wait(event);
+
+	os_event_wait_low(event, cell->signal_count);
 
 	sync_array_free_cell(arr, index);
 }
 
 /**********************************************************************
-Reports info of a wait array cell. Note: sync_array_print_long_waits()
-calls this without mutex protection. */
+Reports info of a wait array cell. */
 static
 void
 sync_array_cell_print(
@@ -503,17 +471,8 @@
 		(ulong) os_thread_pf(cell->thread), cell->file,
 		(ulong) cell->line,
 		difftime(time(NULL), cell->reservation_time));
-	fprintf(file, "Wait array cell state %lu\n", (ulong)cell->state);
-
-	/* If the memory area pointed to by old_wait_mutex /
-	old_wait_rw_lock has been freed, this can crash. */
-
-	if (cell->state != SC_RESERVED) {
-		/* If cell has this state, then even if we are holding the sync
-		array mutex, the wait object may get freed meanwhile. Do not
-		print the wait object then. */
-
-	} else if (type == SYNC_MUTEX) {
+
+	if (type == SYNC_MUTEX) {
 		/* We use old_wait_mutex in case the cell has already
 		been freed meanwhile */
 		mutex = cell->old_wait_mutex;
@@ -531,7 +490,11 @@
 #endif /* UNIV_SYNC_DEBUG */
 			(ulong) mutex->waiters);
 
-	} else if (type == RW_LOCK_EX || type == RW_LOCK_SHARED) {
+	} else if (type == RW_LOCK_EX
+#ifdef __WIN__
+		   || type == RW_LOCK_WAIT_EX
+#endif
+		   || type == RW_LOCK_SHARED) {
 
 		fputs(type == RW_LOCK_EX ? "X-lock on" : "S-lock on", file);
 
@@ -565,8 +528,8 @@
 		ut_error;
 	}
 
-	if (cell->event_set) {
-		fputs("wait is ending\n", file);
+	if (!cell->waiting) {
+		fputs("wait has ended\n", file);
 	}
 }
 
@@ -589,7 +552,7 @@
 
 		cell = sync_array_get_nth_cell(arr, i);
 
-		if ((cell->state == SC_RESERVED)
+		if (cell->wait_object != NULL
 		    && os_thread_eq(cell->thread, thread)) {
 
 			return(cell);	/* Found */
@@ -679,7 +642,7 @@
 
 	depth++;
 
-	if (cell->event_set || !cell->waiting) {
+	if (!cell->waiting) {
 
 		return(FALSE); /* No deadlock here */
 	}
@@ -704,10 +667,8 @@
 						       depth);
 			if (ret) {
 				fprintf(stderr,
-					"Mutex %p owned by thread %lu"
-					" file %s line %lu\n",
-					(void*) mutex,
-					(ulong) os_thread_pf(mutex->thread_id),
+			"Mutex %p owned by thread %lu file %s line %lu\n",
+					mutex, (ulong) os_thread_pf(mutex->thread_id),
 					mutex->file_name, (ulong) mutex->line);
 				sync_array_cell_print(stderr, cell);
 
@@ -717,7 +678,8 @@
 
 		return(FALSE); /* No deadlock */
 
-	} else if (cell->request_type == RW_LOCK_EX) {
+	} else if (cell->request_type == RW_LOCK_EX
+		   || cell->request_type == RW_LOCK_WAIT_EX) {
 
 		lock = cell->wait_object;
 
@@ -816,7 +778,8 @@
 			return(TRUE);
 		}
 
-	} else if (cell->request_type == RW_LOCK_EX) {
+	} else if (cell->request_type == RW_LOCK_EX
+		   || cell->request_type == RW_LOCK_WAIT_EX) {
 
 		lock = cell->wait_object;
 
@@ -845,101 +808,47 @@
 	return(FALSE);
 }
 
-/**************************************************************************
-Looks for the cells in the wait array which refer to the wait object
-specified, and sets their corresponding events to the signaled state. In this
-way releases the threads waiting for the object to contend for the object.
-It is possible that no such cell is found, in which case does nothing. */
+/**********************************************************************
+Frees the cell. NOTE! sync_array_wait_event frees the cell
+automatically! */
 
 void
-sync_array_signal_object(
-/*=====================*/
+sync_array_free_cell(
+/*=================*/
 	sync_array_t*	arr,	/* in: wait array */
-	void*		object)	/* in: wait object */
+	ulint		index)  /* in: index of the cell in array */
 {
 	sync_cell_t*	cell;
-	ulint		count;
-	ulint		i;
-	ulint		res_count;
-
-	/* We store the addresses of cells we need to signal and signal
-	them only after we have released the sync array's mutex (for
-	performance reasons). cell_count is the number of such cells, and
-	cell_ptr points to the first one. If there are less than
-	UT_ARR_SIZE(cells) of them, cell_ptr == &cells[0], otherwise
-	cell_ptr points to malloc'd memory that we must free. */
-
-	sync_cell_t*	cells[100];
-	sync_cell_t**	cell_ptr = &cells[0];
-	ulint		cell_count = 0;
-	ulint		cell_max_count = UT_ARR_SIZE(cells);
-
-	ut_a(100 == cell_max_count);
-
+
+	sync_array_enter(arr);
+
+	cell = sync_array_get_nth_cell(arr, index);
+
+	ut_a(cell->wait_object != NULL);
+
+	cell->waiting = FALSE;
+	cell->wait_object =  NULL;
+	cell->signal_count = 0;
+
+	ut_a(arr->n_reserved > 0);
+	arr->n_reserved--;
+
+	sync_array_exit(arr);
+}
+
+/**************************************************************************
+Increments the signalled count. */
+
+void
+sync_array_object_signalled(
+/*========================*/
+	sync_array_t*	arr)	/* in: wait array */
+{
 	sync_array_enter(arr);
 
 	arr->sg_count++;
 
-	i = 0;
-	count = 0;
-
-	/* We need to store this to a local variable because it is modified
-	inside the loop */
-	res_count = arr->n_reserved;
-
-	while (count < res_count) {
-
-		cell = sync_array_get_nth_cell(arr, i);
-
-		if (cell->state == SC_RESERVED) {
-
-			count++;
-			if (cell->wait_object == object) {
-				cell->state = SC_WAKING_UP;
-
-				ut_a(arr->n_reserved > 0);
-				arr->n_reserved--;
-
-				if (cell_count == cell_max_count) {
-					sync_cell_t** old_cell_ptr = cell_ptr;
-					size_t old_size, new_size;
-
-					old_size = cell_max_count
-						* sizeof(sync_cell_t*);
-					cell_max_count *= 2;
-					new_size = cell_max_count
-						* sizeof(sync_cell_t*);
-
-					cell_ptr = malloc(new_size);
-					ut_a(cell_ptr);
-					memcpy(cell_ptr, old_cell_ptr,
-					       old_size);
-
-					if (old_cell_ptr != &cells[0]) {
-						free(old_cell_ptr);
-					}
-				}
-
-				cell_ptr[cell_count] = cell;
-				cell_count++;
-			}
-		}
-
-		i++;
-	}
-
 	sync_array_exit(arr);
-
-	for (i = 0; i < cell_count; i++) {
-		cell = cell_ptr[i];
-
-		cell->event_set = TRUE;
-		os_event_set(cell->event);
-	}
-
-	if (cell_ptr != &cells[0]) {
-		free(cell_ptr);
-	}
 }
 
 /**************************************************************************
@@ -959,33 +868,41 @@
 	sync_cell_t*	cell;
 	ulint		count;
 	ulint		i;
-	ulint		res_count;
 
 	sync_array_enter(arr);
 
 	i = 0;
 	count = 0;
 
-	/* We need to store this to a local variable because it is modified
-	inside the loop */
-
-	res_count = arr->n_reserved;
-
-	while (count < res_count) {
+	while (count < arr->n_reserved) {
 
 		cell = sync_array_get_nth_cell(arr, i);
 
-		if (cell->state == SC_RESERVED) {
+		if (cell->wait_object != NULL) {
 
 			count++;
 
 			if (sync_arr_cell_can_wake_up(cell)) {
-				cell->state = SC_WAKING_UP;
-				cell->event_set = TRUE;
-				os_event_set(cell->event);
-
-				ut_a(arr->n_reserved > 0);
-				arr->n_reserved--;
+
+				if (cell->request_type == SYNC_MUTEX) {
+					mutex_t*	mutex;
+
+					mutex = cell->wait_object;
+					os_event_set(mutex->event);
+#ifdef __WIN__
+				} else if (cell->request_type
+					   == RW_LOCK_WAIT_EX) {
+					rw_lock_t*	lock;
+
+					lock = cell->wait_object;
+					os_event_set(lock->wait_ex_event);
+#endif
+				} else {
+					rw_lock_t*	lock;
+
+					lock = cell->wait_object;
+					os_event_set(lock->event);
+				}
 			}
 		}
 
@@ -1015,7 +932,7 @@
 
 		cell = sync_array_get_nth_cell(sync_primary_wait_array, i);
 
-		if ((cell->state != SC_FREE)
+		if (cell->wait_object != NULL && cell->waiting
 		    && difftime(time(NULL), cell->reservation_time) > 240) {
 			fputs("InnoDB: Warning: a long semaphore wait:\n",
 			      stderr);
@@ -1023,7 +940,7 @@
 			noticed = TRUE;
 		}
 
-		if ((cell->state != SC_FREE)
+		if (cell->wait_object != NULL && cell->waiting
 		    && difftime(time(NULL), cell->reservation_time)
 		    > fatal_timeout) {
 			fatal = TRUE;
@@ -1072,20 +989,25 @@
 				mutex */
 {
 	sync_cell_t*	cell;
+	ulint		count;
 	ulint		i;
 
 	fprintf(file,
-		"OS WAIT ARRAY INFO: reservation count %ld,"
-		" signal count %ld\n",
-		(long) arr->res_count,
-		(long) arr->sg_count);
-	for (i = 0; i < arr->n_cells; i++) {
+		"OS WAIT ARRAY INFO: reservation count %ld, signal count %ld\n",
+						(long) arr->res_count, (long) arr->sg_count);
+	i = 0;
+	count = 0;
+
+	while (count < arr->n_reserved) {
 
 		cell = sync_array_get_nth_cell(arr, i);
 
-		if (cell->state != SC_FREE) {
+	if (cell->wait_object != NULL) {
+		count++;
 			sync_array_cell_print(file, cell);
 		}
+
+		i++;
 	}
 }
 

=== modified file 'storage/innobase/sync/sync0rw.c'
--- a/storage/innobase/sync/sync0rw.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/sync/sync0rw.c	2008-06-12 00:08:07 +0000
@@ -151,6 +151,11 @@
 	lock->last_x_file_name = "not yet reserved";
 	lock->last_s_line = 0;
 	lock->last_x_line = 0;
+	lock->event = os_event_create(NULL);
+
+#ifdef __WIN__
+	lock->wait_ex_event = os_event_create(NULL);
+#endif
 
 	mutex_enter(&rw_lock_list_mutex);
 
@@ -184,6 +189,11 @@
 	mutex_free(rw_lock_get_mutex(lock));
 
 	mutex_enter(&rw_lock_list_mutex);
+	os_event_free(lock->event);
+
+#ifdef __WIN__
+	os_event_free(lock->wait_ex_event);
+#endif
 
 	if (UT_LIST_GET_PREV(list, lock)) {
 		ut_a(UT_LIST_GET_PREV(list, lock)->magic_n == RW_LOCK_MAGIC_N);
@@ -544,7 +554,15 @@
 	rw_x_system_call_count++;
 
 	sync_array_reserve_cell(sync_primary_wait_array,
-				lock, RW_LOCK_EX,
+				lock,
+#ifdef __WIN__
+				/* On windows RW_LOCK_WAIT_EX signifies
+				that this thread should wait on the
+				special wait_ex_event. */
+				(state == RW_LOCK_WAIT_EX)
+				 ? RW_LOCK_WAIT_EX :
+#endif
+				RW_LOCK_EX,
 				file_name, line,
 				&index);
 

=== modified file 'storage/innobase/sync/sync0sync.c'
--- a/storage/innobase/sync/sync0sync.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/sync/sync0sync.c	2008-06-12 00:08:07 +0000
@@ -95,17 +95,47 @@
 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.
+LEMMA 1: After a thread resets the event of a mutex (or rw_lock), some
+=======
+thread will eventually call os_event_set() on that particular event.
+Thus no infinite wait is possible in this case.
 
 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
-sync_array_signal_object with the mutex as an argument.
+os_event_set() with the mutex as an argument.
+Q.E.D.
+
+LEMMA 2: If an os_event_set() call is made after some thread has called
+=======
+the os_event_reset() and before it starts wait on that event, the call
+will not be lost to the second thread. This is true even if there is an
+intervening call to os_event_reset() by another thread.
+Thus no infinite wait is possible in this case.
+
+Proof (non-windows platforms): os_event_reset() returns a monotonically
+increasing value of signal_count. This value is increased at every
+call of os_event_set() If thread A has called os_event_reset() followed
+by thread B calling os_event_set() and then some other thread C calling
+os_event_reset(), the is_set flag of the event will be set to FALSE;
+but now if thread A calls os_event_wait_low() with the signal_count
+value returned from the earlier call of os_event_reset(), it will
+return immediately without waiting.
+Q.E.D.
+
+Proof (windows): If there is a writer thread which is forced to wait for
+the lock, it may be able to set the state of rw_lock to RW_LOCK_WAIT_EX
+The design of rw_lock ensures that there is one and only one thread
+that is able to change the state to RW_LOCK_WAIT_EX and this thread is
+guaranteed to acquire the lock after it is released by the current
+holders and before any other waiter gets the lock.
+On windows this thread waits on a separate event i.e.: wait_ex_event.
+Since only one thread can wait on this event there is no chance
+of this event getting reset before the writer starts wait on it.
+Therefore, this thread is guaranteed to catch the os_set_event()
+signalled unconditionally at the release of the lock.
 Q.E.D. */
 
 /* The number of system calls made in this module. Intended for performance
@@ -217,6 +247,7 @@
 	os_fast_mutex_init(&(mutex->os_fast_mutex));
 	mutex->lock_word = 0;
 #endif
+	mutex->event = os_event_create(NULL);
 	mutex_set_waiters(mutex, 0);
 #ifdef UNIV_DEBUG
 	mutex->magic_n = MUTEX_MAGIC_N;
@@ -300,6 +331,8 @@
 		mutex_exit(&mutex_list_mutex);
 	}
 
+	os_event_free(mutex->event);
+
 #if !defined(_WIN32) || !defined(UNIV_CAN_USE_X86_ASSEMBLER)
 	os_fast_mutex_free(&(mutex->os_fast_mutex));
 #endif
@@ -509,8 +542,7 @@
 		if (mutex_test_and_set(mutex) == 0) {
 			/* Succeeded! Free the reserved wait cell */
 
-			sync_array_free_cell_protected(sync_primary_wait_array,
-						       index);
+			sync_array_free_cell(sync_primary_wait_array, index);
 
 			ut_d(mutex->thread_id = os_thread_get_curr_id());
 #ifdef UNIV_SYNC_DEBUG
@@ -591,8 +623,8 @@
 
 	/* 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);
+	os_event_set(mutex->event);
+	sync_array_object_signalled(sync_primary_wait_array);
 }
 
 #ifdef UNIV_SYNC_DEBUG
@@ -1130,6 +1162,7 @@
 		break;
 	case SYNC_TREE_NODE:
 		ut_a(sync_thread_levels_contain(array, SYNC_INDEX_TREE)
+		     || sync_thread_levels_contain(array, SYNC_DICT_OPERATION)
 		     || sync_thread_levels_g(array, SYNC_TREE_NODE - 1));
 		break;
 	case SYNC_TREE_NODE_NEW:

=== modified file 'storage/innobase/trx/trx0trx.c'
--- a/storage/innobase/trx/trx0trx.c	2008-05-15 20:56:38 +0000
+++ b/storage/innobase/trx/trx0trx.c	2008-06-12 00:08:07 +0000
@@ -757,8 +757,8 @@
 		mutex_enter(&(rseg->mutex));
 
 		if (trx->insert_undo != NULL) {
-			trx_undo_set_state_at_finish(trx, trx->insert_undo,
-						     &mtr);
+			trx_undo_set_state_at_finish(
+				rseg, trx, trx->insert_undo, &mtr);
 		}
 
 		undo = trx->update_undo;
@@ -774,7 +774,7 @@
 			transaction commit for this transaction. */
 
 			update_hdr_page = trx_undo_set_state_at_finish(
-				trx, undo, &mtr);
+				rseg, trx, undo, &mtr);
 
 			/* We have to do the cleanup for the update log while
 			holding the rseg mutex because update log headers
@@ -933,6 +933,7 @@
 	trx->rseg = NULL;
 	trx->undo_no = ut_dulint_zero;
 	trx->last_sql_stat_start.least_undo_no = ut_dulint_zero;
+	trx->mysql_query_str = NULL;
 
 	ut_ad(UT_LIST_GET_LEN(trx->wait_thrs) == 0);
 	ut_ad(UT_LIST_GET_LEN(trx->trx_locks) == 0);

=== modified file 'storage/innobase/trx/trx0undo.c'
--- a/storage/innobase/trx/trx0undo.c	2008-03-31 09:37:03 +0000
+++ b/storage/innobase/trx/trx0undo.c	2008-06-12 00:08:07 +0000
@@ -1725,6 +1725,7 @@
 /*=========================*/
 				/* out: undo log segment header page,
 				x-latched */
+	trx_rseg_t*	rseg,	/* in: rollback segment memory object */
 	trx_t*		trx __attribute__((unused)), /* in: transaction */
 	trx_undo_t*	undo,	/* in: undo log memory copy */
 	mtr_t*		mtr)	/* in: mtr */
@@ -1734,7 +1735,10 @@
 	page_t*		undo_page;
 	ulint		state;
 
-	ut_ad(trx && undo && mtr);
+	ut_ad(trx);
+	ut_ad(undo);
+	ut_ad(mtr);
+	ut_ad(mutex_own(&rseg->mutex));
 
 	if (undo->id >= TRX_RSEG_N_SLOTS) {
 		fprintf(stderr, "InnoDB: Error: undo->id is %lu\n",
@@ -1748,9 +1752,23 @@
 	seg_hdr = undo_page + TRX_UNDO_SEG_HDR;
 	page_hdr = undo_page + TRX_UNDO_PAGE_HDR;
 
-	if (undo->size == 1 && mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE)
-	    < TRX_UNDO_PAGE_REUSE_LIMIT) {
-		state = TRX_UNDO_CACHED;
+	if (undo->size == 1
+	    && mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE)
+	       < TRX_UNDO_PAGE_REUSE_LIMIT) {
+
+		/* This is a heuristic to avoid the problem of all UNDO
+		slots ending up in one of the UNDO lists. Previously if
+		the server crashed with all the slots in one of the lists,
+		transactions that required the slots of a different type
+		would fail for lack of slots. */
+
+		if (UT_LIST_GET_LEN(rseg->update_undo_list) < 500
+		    && UT_LIST_GET_LEN(rseg->insert_undo_list) < 500) {
+
+			state = TRX_UNDO_CACHED;
+		} else {
+			state = TRX_UNDO_TO_FREE;
+		}
 
 	} else if (undo->type == TRX_UNDO_INSERT) {
 

=== modified file 'storage/innobase/ut/ut0ut.c'
--- a/storage/innobase/ut/ut0ut.c	2007-11-06 22:42:58 +0000
+++ b/storage/innobase/ut/ut0ut.c	2008-06-12 00:08:07 +0000
@@ -112,19 +112,45 @@
 }
 
 /**************************************************************
-Returns system time. */
+Returns system time.
+Upon successful completion, the value 0 is returned; otherwise the
+value -1 is returned and the global variable errno is set to indicate the
+error. */
 
-void
+int
 ut_usectime(
 /*========*/
+			/* out: 0 on success, -1 otherwise */
 	ulint*	sec,	/* out: seconds since the Epoch */
 	ulint*	ms)	/* out: microseconds since the Epoch+*sec */
 {
 	struct timeval	tv;
-
-	ut_gettimeofday(&tv, NULL);
-	*sec = (ulint) tv.tv_sec;
-	*ms  = (ulint) tv.tv_usec;
+	int		ret;
+	int		errno_gettimeofday;
+	int		i;
+
+	for (i = 0; i < 10; i++) {
+
+		ret = ut_gettimeofday(&tv, NULL);
+
+		if (ret == -1) {
+			errno_gettimeofday = errno;
+			ut_print_timestamp(stderr);
+			fprintf(stderr, "  InnoDB: gettimeofday(): %s\n",
+				strerror(errno_gettimeofday));
+			os_thread_sleep(100000);  /* 0.1 sec */
+			errno = errno_gettimeofday;
+		} else {
+			break;
+		}
+	}
+
+	if (ret != -1) {
+		*sec = (ulint) tv.tv_sec;
+		*ms  = (ulint) tv.tv_usec;
+	}
+
+	return(ret);
 }
 
 /**************************************************************

--===============0945899152896280985==
Content-Type: text/plain; charset=us-ascii

-- 
MySQL Code Commits Mailing List
For list archives: http://lists.mysql.com/commits
To unsubscribe:    http://lists.mysql.com/commits?unsub=commits@bugs.mysql.com
--===============0945899152896280985==--