57 static ibool os_sync_mutex_inited = FALSE;
59 static ibool os_sync_free_called = FALSE;
66 static UT_LIST_BASE_NODE_T(
os_event) os_event_list;
69 static UT_LIST_BASE_NODE_T(
os_mutex_t) os_mutex_list;
71 UNIV_INTERN ulint os_event_count = 0;
72 UNIV_INTERN ulint os_mutex_count = 0;
73 UNIV_INTERN ulint os_fast_mutex_count = 0;
76 static const ulint MICROSECS_IN_A_SECOND = 1000000;
79 UNIV_INTERN mysql_pfs_key_t event_os_mutex_key;
80 UNIV_INTERN mysql_pfs_key_t os_mutex_key;
91 static void os_cond_module_init(
void);
95 typedef VOID (WINAPI* InitializeConditionVariableProc)
96 (PCONDITION_VARIABLE ConditionVariable);
97 static InitializeConditionVariableProc initialize_condition_variable;
99 typedef BOOL (WINAPI* SleepConditionVariableCSProc)
100 (PCONDITION_VARIABLE ConditionVariable,
101 PCRITICAL_SECTION CriticalSection,
102 DWORD dwMilliseconds);
103 static SleepConditionVariableCSProc sleep_condition_variable;
105 typedef VOID (WINAPI* WakeAllConditionVariableProc)
106 (PCONDITION_VARIABLE ConditionVariable);
107 static WakeAllConditionVariableProc wake_all_condition_variable;
109 typedef VOID (WINAPI* WakeConditionVariableProc)
110 (PCONDITION_VARIABLE ConditionVariable);
111 static WakeConditionVariableProc wake_condition_variable;
125 ut_a(initialize_condition_variable != NULL);
126 initialize_condition_variable(cond);
128 ut_a(pthread_cond_init(cond, NULL) == 0);
154 ut_a(sleep_condition_variable != NULL);
156 ret = sleep_condition_variable(cond, mutex, time_in_ms);
159 err = GetLastError();
166 if ((err == WAIT_TIMEOUT) || (err == ERROR_TIMEOUT)) {
177 ret = pthread_cond_timedwait(cond, mutex, abstime);
188 fprintf(stderr,
" InnoDB: pthread_cond_timedwait() returned: "
189 "%d: abstime={%lu,%lu}\n",
190 ret, (ulong) abstime->tv_sec, (ulong) abstime->tv_nsec);
194 return(ret == ETIMEDOUT);
211 ut_a(sleep_condition_variable != NULL);
212 ut_a(sleep_condition_variable(cond, mutex, INFINITE));
214 ut_a(pthread_cond_wait(cond, mutex) == 0);
229 ut_a(wake_all_condition_variable != NULL);
230 wake_all_condition_variable(cond);
232 ut_a(pthread_cond_broadcast(cond) == 0);
247 ut_a(wake_condition_variable != NULL);
248 wake_condition_variable(cond);
250 ut_a(pthread_cond_signal(cond) == 0);
265 ut_a(pthread_cond_destroy(cond) == 0);
275 os_cond_module_init(
void)
281 if (!srv_use_native_conditions)
284 h_dll = GetModuleHandle(
"kernel32");
286 initialize_condition_variable = (InitializeConditionVariableProc)
287 GetProcAddress(h_dll,
"InitializeConditionVariable");
288 sleep_condition_variable = (SleepConditionVariableCSProc)
289 GetProcAddress(h_dll,
"SleepConditionVariableCS");
290 wake_all_condition_variable = (WakeAllConditionVariableProc)
291 GetProcAddress(h_dll,
"WakeAllConditionVariable");
292 wake_condition_variable = (WakeConditionVariableProc)
293 GetProcAddress(h_dll,
"WakeConditionVariable");
296 ut_a(initialize_condition_variable);
297 ut_a(sleep_condition_variable);
298 ut_a(wake_all_condition_variable);
299 ut_a(wake_condition_variable);
313 os_sync_mutex = NULL;
314 os_sync_mutex_inited = FALSE;
317 os_cond_module_init();
321 os_sync_mutex_inited = TRUE;
334 os_sync_free_called = TRUE;
347 if (mutex == os_sync_mutex) {
351 os_sync_mutex_inited = FALSE;
358 os_sync_free_called = FALSE;
374 if(!srv_use_native_conditions) {
378 event->handle = CreateEvent(NULL, TRUE, FALSE, NULL);
379 if (!event->handle) {
381 "InnoDB: Could not create a Windows event"
382 " semaphore; Windows error %lu\n",
383 (ulong) GetLastError());
390 #ifndef PFS_SKIP_EVENT_MUTEX
391 os_fast_mutex_init(event_os_mutex_key, &event->
os_mutex);
393 os_fast_mutex_init(PFS_NOT_INSTRUMENTED, &event->
os_mutex);
398 event->is_set = FALSE;
406 event->signal_count = 1;
412 if (os_sync_mutex != NULL) {
421 if (os_sync_mutex != NULL) {
440 if (!srv_use_native_conditions) {
441 ut_a(SetEvent(event->handle));
446 os_fast_mutex_lock(&(event->
os_mutex));
451 event->is_set = TRUE;
452 event->signal_count += 1;
456 os_fast_mutex_unlock(&(event->
os_mutex));
478 if(!srv_use_native_conditions) {
479 ut_a(ResetEvent(event->handle));
484 os_fast_mutex_lock(&(event->
os_mutex));
489 event->is_set = FALSE;
491 ret =
event->signal_count;
493 os_fast_mutex_unlock(&(event->
os_mutex));
501 os_event_free_internal(
506 if(!srv_use_native_conditions) {
508 ut_a(CloseHandle(event->handle));
515 os_fast_mutex_free(&(event->
os_mutex));
539 if(!srv_use_native_conditions){
540 ut_a(CloseHandle(event->handle));
544 os_fast_mutex_free(&(event->
os_mutex));
583 ib_int64_t reset_sig_count)
588 if(!srv_use_native_conditions) {
596 err = WaitForSingleObject(event->handle, INFINITE);
598 ut_a(err == WAIT_OBJECT_0);
603 os_fast_mutex_lock(&event->
os_mutex);
605 if (!reset_sig_count) {
606 reset_sig_count =
event->signal_count;
617 os_fast_mutex_unlock(&event->
os_mutex);
632 ib_int64_t reset_sig_count)
636 ibool timed_out = FALSE;
641 if (!srv_use_native_conditions) {
647 time_in_ms = time_in_usec / 1000;
648 err = WaitForSingleObject(event->handle, time_in_ms);
650 err = WaitForSingleObject(event->handle, INFINITE);
653 if (err == WAIT_OBJECT_0) {
655 }
else if ((err == WAIT_TIMEOUT) || (err == ERROR_TIMEOUT)) {
663 ut_a(sleep_condition_variable != NULL);
666 time_in_ms = time_in_usec / 1000;
668 time_in_ms = INFINITE;
686 tv.tv_usec += time_in_usec;
688 if ((ulint) tv.tv_usec >= MICROSECS_IN_A_SECOND) {
689 tv.tv_sec += time_in_usec / MICROSECS_IN_A_SECOND;
690 tv.tv_usec %= MICROSECS_IN_A_SECOND;
693 abstime.tv_sec = tv.tv_sec;
694 abstime.tv_nsec = tv.tv_usec * 1000;
696 abstime.tv_nsec = 999999999;
697 abstime.tv_sec = (time_t) ULINT_MAX;
700 ut_a(abstime.tv_nsec <= 999999999);
704 os_fast_mutex_lock(&event->
os_mutex);
706 if (!reset_sig_count) {
707 reset_sig_count =
event->signal_count;
725 }
while (!timed_out);
727 os_fast_mutex_unlock(&event->
os_mutex);
747 os_fast_mutex_init(os_mutex_key, mutex);
751 mutex_str->
handle = mutex;
752 mutex_str->
count = 0;
755 if (UNIV_LIKELY(os_sync_mutex_inited)) {
764 if (UNIV_LIKELY(os_sync_mutex_inited)) {
779 os_fast_mutex_lock(static_cast<os_fast_mutex_t*>(mutex->
handle));
799 os_fast_mutex_unlock(static_cast<os_fast_mutex_t*>(mutex->
handle));
812 if (UNIV_LIKELY(!os_sync_free_called)) {
813 os_event_free_internal(mutex->
event);
816 if (UNIV_LIKELY(os_sync_mutex_inited)) {
824 if (UNIV_LIKELY(os_sync_mutex_inited)) {
828 os_fast_mutex_free(static_cast<os_fast_mutex_t*>(mutex->
handle));
844 InitializeCriticalSection((LPCRITICAL_SECTION) fast_mutex);
846 ut_a(0 == pthread_mutex_init(fast_mutex, MY_MUTEX_INIT_FAST));
848 if (UNIV_LIKELY(os_sync_mutex_inited)) {
855 os_fast_mutex_count++;
857 if (UNIV_LIKELY(os_sync_mutex_inited)) {
871 EnterCriticalSection((LPCRITICAL_SECTION) fast_mutex);
873 pthread_mutex_lock(fast_mutex);
886 LeaveCriticalSection(fast_mutex);
888 pthread_mutex_unlock(fast_mutex);
903 DeleteCriticalSection((LPCRITICAL_SECTION) fast_mutex);
907 ret = pthread_mutex_destroy(fast_mutex);
909 if (UNIV_UNLIKELY(ret != 0)) {
912 " InnoDB: error: return value %lu when calling\n"
913 "InnoDB: pthread_mutex_destroy().\n", (ulint) ret);
915 "InnoDB: Byte contents of the pthread mutex at %p:\n",
921 if (UNIV_LIKELY(os_sync_mutex_inited)) {
928 ut_ad(os_fast_mutex_count > 0);
929 os_fast_mutex_count--;
931 if (UNIV_LIKELY(os_sync_mutex_inited)) {