43 typedef HANDLE os_native_event_t;
60 struct PSI_mutex* pfs_psi;
89 #define OS_SYNC_INFINITE_TIME ULINT_UNDEFINED
92 #define OS_SYNC_TIME_EXCEEDED 1
104 extern ulint os_event_count;
105 extern ulint os_mutex_count;
106 extern ulint os_fast_mutex_count;
179 ib_int64_t reset_sig_count);
183 #define os_event_wait(event) os_event_wait_low(event, 0)
184 #define os_event_wait_time(event, t) os_event_wait_time_low(event, t, 0)
198 ib_int64_t reset_sig_count);
254 #ifdef UNIV_PFS_MUTEX
255 # define os_fast_mutex_init(K, M) \
256 pfs_os_fast_mutex_init(K, M)
258 # define os_fast_mutex_lock(M) \
259 pfs_os_fast_mutex_lock(M, __FILE__, __LINE__)
261 # define os_fast_mutex_unlock(M) pfs_os_fast_mutex_unlock(M)
263 # define os_fast_mutex_free(M) pfs_os_fast_mutex_free(M)
272 pfs_os_fast_mutex_init(
284 pfs_os_fast_mutex_free(
293 pfs_os_fast_mutex_lock(
305 pfs_os_fast_mutex_unlock(
311 # define os_fast_mutex_init(K, M) \
312 os_fast_mutex_init_func(&((os_fast_mutex_t*)(M))->mutex)
314 # define os_fast_mutex_lock(M) \
315 os_fast_mutex_lock_func(&((os_fast_mutex_t*)(M))->mutex)
317 # define os_fast_mutex_unlock(M) \
318 os_fast_mutex_unlock_func(&((os_fast_mutex_t*)(M))->mutex)
320 # define os_fast_mutex_free(M) \
321 os_fast_mutex_free_func(&((os_fast_mutex_t*)(M))->mutex)
356 #if defined(HAVE_IB_GCC_ATOMIC_BUILTINS)
358 # define HAVE_ATOMIC_BUILTINS
360 # ifdef HAVE_IB_GCC_ATOMIC_BUILTINS_BYTE
361 # define HAVE_ATOMIC_BUILTINS_BYTE
364 # ifdef HAVE_IB_GCC_ATOMIC_BUILTINS_64
365 # define HAVE_ATOMIC_BUILTINS_64
372 # define os_compare_and_swap(ptr, old_val, new_val) \
373 __sync_bool_compare_and_swap(ptr, old_val, new_val)
375 # define os_compare_and_swap_ulint(ptr, old_val, new_val) \
376 os_compare_and_swap(ptr, old_val, new_val)
378 # define os_compare_and_swap_lint(ptr, old_val, new_val) \
379 os_compare_and_swap(ptr, old_val, new_val)
381 # define os_compare_and_swap_uint32(ptr, old_val, new_val) \
382 os_compare_and_swap(ptr, old_val, new_val)
384 # ifdef HAVE_IB_ATOMIC_PTHREAD_T_GCC
385 # define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
386 os_compare_and_swap(ptr, old_val, new_val)
387 # define INNODB_RW_LOCKS_USE_ATOMICS
388 # define IB_ATOMICS_STARTUP_MSG \
389 "Mutexes and rw_locks use GCC atomic builtins"
391 # define IB_ATOMICS_STARTUP_MSG \
392 "Mutexes use GCC atomic builtins, rw_locks do not"
399 # define os_atomic_increment(ptr, amount) \
400 __sync_add_and_fetch(ptr, amount)
402 # define os_atomic_increment_lint(ptr, amount) \
403 os_atomic_increment(ptr, amount)
405 # define os_atomic_increment_uint32(ptr, amount ) \
406 os_atomic_increment(ptr, amount)
408 # define os_atomic_increment_ulint(ptr, amount) \
409 os_atomic_increment(ptr, amount)
411 # define os_atomic_increment_uint64(ptr, amount) \
412 os_atomic_increment(ptr, amount)
417 # define os_atomic_decrement(ptr, amount) \
418 __sync_sub_and_fetch(ptr, amount)
420 # define os_atomic_decrement_uint32(ptr, amount) \
421 os_atomic_decrement(ptr, amount)
423 # define os_atomic_decrement_lint(ptr, amount) \
424 os_atomic_decrement(ptr, amount)
426 # define os_atomic_decrement_ulint(ptr, amount) \
427 os_atomic_decrement(ptr, amount)
429 # define os_atomic_decrement_uint64(ptr, amount) \
430 os_atomic_decrement(ptr, amount)
435 # define os_atomic_test_and_set_byte(ptr, new_val) \
436 __sync_lock_test_and_set(ptr, (byte) new_val)
438 # define os_atomic_test_and_set_ulint(ptr, new_val) \
439 __sync_lock_test_and_set(ptr, new_val)
441 #elif defined(HAVE_IB_SOLARIS_ATOMICS)
443 # define HAVE_ATOMIC_BUILTINS
444 # define HAVE_ATOMIC_BUILTINS_BYTE
445 # define HAVE_ATOMIC_BUILTINS_64
456 # define os_compare_and_swap_uint32(ptr, old_val, new_val) \
457 (atomic_cas_32(ptr, old_val, new_val) == old_val)
459 # define os_compare_and_swap_ulint(ptr, old_val, new_val) \
460 (atomic_cas_ulong(ptr, old_val, new_val) == old_val)
462 # define os_compare_and_swap_lint(ptr, old_val, new_val) \
463 ((lint) atomic_cas_ulong((ulong_t*) ptr, old_val, new_val) == old_val)
465 # ifdef HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS
466 # if SIZEOF_PTHREAD_T == 4
467 # define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
468 ((pthread_t) atomic_cas_32(ptr, old_val, new_val) == old_val)
469 # elif SIZEOF_PTHREAD_T == 8
470 # define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
471 ((pthread_t) atomic_cas_64(ptr, old_val, new_val) == old_val)
473 # error "SIZEOF_PTHREAD_T != 4 or 8"
475 # define INNODB_RW_LOCKS_USE_ATOMICS
476 # define IB_ATOMICS_STARTUP_MSG \
477 "Mutexes and rw_locks use Solaris atomic functions"
479 # define IB_ATOMICS_STARTUP_MSG \
480 "Mutexes use Solaris atomic functions, rw_locks do not"
487 # define os_atomic_increment_uint32(ptr, amount) \
488 atomic_add_32_nv(ptr, amount)
490 # define os_atomic_increment_ulint(ptr, amount) \
491 atomic_add_long_nv(ptr, amount)
493 # define os_atomic_increment_lint(ptr, amount) \
494 os_atomic_increment_ulint((ulong_t*) ptr, amount)
496 # define os_atomic_increment_uint64(ptr, amount) \
497 atomic_add_64_nv(ptr, amount)
502 # define os_atomic_decrement_uint32(ptr, amount) \
503 os_atomic_increment_uint32(ptr, -(amount))
505 # define os_atomic_decrement_lint(ptr, amount) \
506 os_atomic_increment_ulint((ulong_t*) ptr, -(amount))
508 # define os_atomic_decrement_ulint(ptr, amount) \
509 os_atomic_increment_ulint(ptr, -(amount))
511 # define os_atomic_decrement_uint64(ptr, amount) \
512 os_atomic_increment_uint64(ptr, -(amount))
517 # define os_atomic_test_and_set_byte(ptr, new_val) \
518 atomic_swap_uchar(ptr, new_val)
520 # define os_atomic_test_and_set_ulint(ptr, new_val) \
521 atomic_swap_ulong(ptr, new_val)
523 #elif defined(HAVE_WINDOWS_ATOMICS)
525 # define HAVE_ATOMIC_BUILTINS
526 # define HAVE_ATOMIC_BUILTINS_BYTE
529 # define HAVE_ATOMIC_BUILTINS_64
538 win_cmp_and_xchg_lint(
560 win_cmp_and_xchg_ulint(
572 win_cmp_and_xchg_dword(
582 # define os_compare_and_swap_uint32(ptr, old_val, new_val) \
583 (InterlockedCompareExchange(reinterpret_cast<volatile long*>(ptr), \
584 new_val, old_val) == old_val)
586 # define os_compare_and_swap_ulint(ptr, old_val, new_val) \
587 (win_cmp_and_xchg_ulint(ptr, new_val, old_val) == old_val)
589 # define os_compare_and_swap_lint(ptr, old_val, new_val) \
590 (win_cmp_and_xchg_lint(ptr, new_val, old_val) == old_val)
593 # define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
594 (win_cmp_and_xchg_dword(ptr, new_val, old_val) == old_val)
596 # define INNODB_RW_LOCKS_USE_ATOMICS
597 # define IB_ATOMICS_STARTUP_MSG \
598 "Mutexes and rw_locks use Windows interlocked functions"
604 # define os_atomic_increment_lint(ptr, amount) \
605 (win_xchg_and_add(ptr, amount) + amount)
607 # define os_atomic_increment_uint32(ptr, amount) \
608 ((ulint) InterlockedExchangeAdd((long*) ptr, amount))
610 # define os_atomic_increment_ulint(ptr, amount) \
611 ((ulint) (win_xchg_and_add((lint*) ptr, (lint) amount) + amount))
613 # define os_atomic_increment_uint64(ptr, amount) \
614 ((ib_uint64_t) (InterlockedExchangeAdd64( \
616 (ib_int64_t) amount) + amount))
622 # define os_atomic_decrement_uint32(ptr, amount) \
623 ((ulint) InterlockedExchangeAdd((long*) ptr, (-amount)))
625 # define os_atomic_decrement_lint(ptr, amount) \
626 (win_xchg_and_add(ptr, -(lint) amount) - amount)
628 # define os_atomic_decrement_ulint(ptr, amount) \
629 ((ulint) (win_xchg_and_add((lint*) ptr, -(lint) amount) - amount))
631 # define os_atomic_decrement_uint64(ptr, amount) \
632 ((ib_uint64_t) (InterlockedExchangeAdd64( \
634 -(ib_int64_t) amount) - amount))
641 # define os_atomic_test_and_set_byte(ptr, new_val) \
642 ((byte) InterlockedExchange(ptr, new_val))
644 # define os_atomic_test_and_set_ulong(ptr, new_val) \
645 InterlockedExchange(ptr, new_val)
648 # define IB_ATOMICS_STARTUP_MSG \
649 "Mutexes and rw_locks use InnoDB's own implementation"
651 #ifdef HAVE_ATOMIC_BUILTINS
652 #define os_atomic_inc_ulint(m,v,d) os_atomic_increment_ulint(v, d)
653 #define os_atomic_dec_ulint(m,v,d) os_atomic_decrement_ulint(v, d)
655 #define os_atomic_inc_ulint(m,v,d) os_atomic_inc_ulint_func(m, v, d)
656 #define os_atomic_dec_ulint(m,v,d) os_atomic_dec_ulint_func(m, v, d)
663 #ifdef HAVE_ATOMIC_BUILTINS
664 #define os_increment_counter_by_amount(mutex, counter, amount) \
665 (void) os_atomic_increment_ulint(&counter, amount)
667 #define os_decrement_counter_by_amount(mutex, counter, amount) \
668 (void) os_atomic_increment_ulint(&counter, (-((lint) amount)))
670 #define os_increment_counter_by_amount(mutex, counter, amount) \
672 mutex_enter(&(mutex)); \
673 (counter) += (amount); \
674 mutex_exit(&(mutex)); \
677 #define os_decrement_counter_by_amount(mutex, counter, amount) \
679 ut_a(counter >= amount); \
680 mutex_enter(&(mutex)); \
681 (counter) -= (amount); \
682 mutex_exit(&(mutex)); \
686 #define os_inc_counter(mutex, counter) \
687 os_increment_counter_by_amount(mutex, counter, 1)
689 #define os_dec_counter(mutex, counter) \
691 os_decrement_counter_by_amount(mutex, counter, 1);\
695 #if defined __i386__ || defined __x86_64__ || defined _M_IX86 || defined _M_X64 || defined __WIN__
700 # define IB_MEMORY_BARRIER_STARTUP_MSG \
701 "Memory barrier is not used"
702 #elif defined(HAVE_IB_GCC_ATOMIC_THREAD_FENCE)
703 # define HAVE_MEMORY_BARRIER
704 # define os_rmb __atomic_thread_fence(__ATOMIC_ACQUIRE)
705 # define os_wmb __atomic_thread_fence(__ATOMIC_RELEASE)
706 # define IB_MEMORY_BARRIER_STARTUP_MSG \
707 "GCC builtin __atomic_thread_fence() is used for memory barrier"
709 #elif defined(HAVE_IB_GCC_SYNC_SYNCHRONISE)
710 # define HAVE_MEMORY_BARRIER
711 # define os_rmb __sync_synchronize()
712 # define os_wmb __sync_synchronize()
713 # define IB_MEMORY_BARRIER_STARTUP_MSG \
714 "GCC builtin __sync_synchronize() is used for memory barrier"
716 #elif defined(HAVE_IB_MACHINE_BARRIER_SOLARIS)
717 # define HAVE_MEMORY_BARRIER
718 # include <mbarrier.h>
719 # define os_rmb __machine_r_barrier()
720 # define os_wmb __machine_w_barrier()
721 # define IB_MEMORY_BARRIER_STARTUP_MSG \
722 "Solaris memory ordering functions are used for memory barrier"
724 #elif defined(HAVE_WINDOWS_MM_FENCE) && defined(_WIN64)
725 # define HAVE_MEMORY_BARRIER
726 # include <mmintrin.h>
727 # define os_rmb _mm_lfence()
728 # define os_wmb _mm_sfence()
729 # define IB_MEMORY_BARRIER_STARTUP_MSG \
730 "_mm_lfence() and _mm_sfence() are used for memory barrier"
735 # define IB_MEMORY_BARRIER_STARTUP_MSG \
736 "Memory barrier is not used"