📄 sync0rw.c
字号:
/******************************************************The read-write lock (for thread synchronization)(c) 1995 Innobase OyCreated 9/11/1995 Heikki Tuuri*******************************************************/#include "sync0rw.h"#ifdef UNIV_NONINL#include "sync0rw.ic"#endif#include "os0thread.h"#include "mem0mem.h"#include "srv0srv.h"ulint rw_s_system_call_count = 0;ulint rw_s_spin_wait_count = 0;ulint rw_s_os_wait_count = 0;ulint rw_s_exit_count = 0;ulint rw_x_system_call_count = 0;ulint rw_x_spin_wait_count = 0;ulint rw_x_os_wait_count = 0;ulint rw_x_exit_count = 0;/* The global list of rw-locks */rw_lock_list_t rw_lock_list;mutex_t rw_lock_list_mutex;#ifdef UNIV_SYNC_DEBUG/* The global mutex which protects debug info lists of all rw-locks.To modify the debug info list of an rw-lock, this mutex has to beacquired in addition to the mutex protecting the lock. */mutex_t rw_lock_debug_mutex;os_event_t rw_lock_debug_event; /* If deadlock detection does not get immediately the mutex, it may wait for this event */ibool rw_lock_debug_waiters; /* This is set to TRUE, if there may be waiters for the event *//**********************************************************************Creates a debug info struct. */staticrw_lock_debug_t*rw_lock_debug_create(void);/*======================*//**********************************************************************Frees a debug info struct. */staticvoidrw_lock_debug_free(/*===============*/ rw_lock_debug_t* info);/**********************************************************************Creates a debug info struct. */staticrw_lock_debug_t*rw_lock_debug_create(void)/*======================*/{ return((rw_lock_debug_t*) mem_alloc(sizeof(rw_lock_debug_t)));}/**********************************************************************Frees a debug info struct. */staticvoidrw_lock_debug_free(/*===============*/ rw_lock_debug_t* info){ mem_free(info);}#endif /* UNIV_SYNC_DEBUG *//**********************************************************************Creates, or rather, initializes an rw-lock object in a specified memorylocation (which must be appropriately aligned). The rw-lock is initializedto the non-locked state. Explicit freeing of the rw-lock with rw_lock_freeis necessary only if the memory block containing it is freed. */voidrw_lock_create_func(/*================*/ rw_lock_t* lock, /* in: pointer to memory */ const char* cfile_name, /* in: file name where created */ ulint cline, /* in: file line where created */ const char* cmutex_name) /* in: mutex name */{ /* If this is the very first time a synchronization object is created, then the following call initializes the sync system. */ mutex_create(rw_lock_get_mutex(lock)); mutex_set_level(rw_lock_get_mutex(lock), SYNC_NO_ORDER_CHECK); lock->mutex.cfile_name = cfile_name; lock->mutex.cline = cline;#ifndef UNIV_HOTBACKUP lock->mutex.cmutex_name = cmutex_name; lock->mutex.mutex_type = 1;#endif /* !UNIV_HOTBACKUP */ rw_lock_set_waiters(lock, 0); rw_lock_set_writer(lock, RW_LOCK_NOT_LOCKED); lock->writer_count = 0; rw_lock_set_reader_count(lock, 0); lock->writer_is_wait_ex = FALSE;#ifdef UNIV_SYNC_DEBUG UT_LIST_INIT(lock->debug_list); lock->level = SYNC_LEVEL_NONE;#endif /* UNIV_SYNC_DEBUG */ lock->magic_n = RW_LOCK_MAGIC_N; lock->cfile_name = cfile_name; lock->cline = cline; lock->last_s_file_name = "not yet reserved"; lock->last_x_file_name = "not yet reserved"; lock->last_s_line = 0; lock->last_x_line = 0; mutex_enter(&rw_lock_list_mutex); if (UT_LIST_GET_LEN(rw_lock_list) > 0) { ut_a(UT_LIST_GET_FIRST(rw_lock_list)->magic_n == RW_LOCK_MAGIC_N); } UT_LIST_ADD_FIRST(list, rw_lock_list, lock); mutex_exit(&rw_lock_list_mutex);}/**********************************************************************Calling this function is obligatory only if the memory buffer containingthe rw-lock is freed. Removes an rw-lock object from the global list. Therw-lock is checked to be in the non-locked state. */voidrw_lock_free(/*=========*/ rw_lock_t* lock) /* in: rw-lock */{#ifdef UNIV_DEBUG ut_a(rw_lock_validate(lock));#endif /* UNIV_DEBUG */ ut_a(rw_lock_get_writer(lock) == RW_LOCK_NOT_LOCKED); ut_a(rw_lock_get_waiters(lock) == 0); ut_a(rw_lock_get_reader_count(lock) == 0); lock->magic_n = 0; mutex_free(rw_lock_get_mutex(lock)); mutex_enter(&rw_lock_list_mutex); if (UT_LIST_GET_PREV(list, lock)) { ut_a(UT_LIST_GET_PREV(list, lock)->magic_n == RW_LOCK_MAGIC_N); } if (UT_LIST_GET_NEXT(list, lock)) { ut_a(UT_LIST_GET_NEXT(list, lock)->magic_n == RW_LOCK_MAGIC_N); } UT_LIST_REMOVE(list, rw_lock_list, lock); mutex_exit(&rw_lock_list_mutex);}/**********************************************************************Checks that the rw-lock has been initialized and that there are nosimultaneous shared and exclusive locks. */iboolrw_lock_validate(/*=============*/ rw_lock_t* lock){ ut_a(lock); mutex_enter(rw_lock_get_mutex(lock)); ut_a(lock->magic_n == RW_LOCK_MAGIC_N); ut_a((rw_lock_get_reader_count(lock) == 0) || (rw_lock_get_writer(lock) != RW_LOCK_EX)); ut_a((rw_lock_get_writer(lock) == RW_LOCK_EX) || (rw_lock_get_writer(lock) == RW_LOCK_WAIT_EX) || (rw_lock_get_writer(lock) == RW_LOCK_NOT_LOCKED)); ut_a((rw_lock_get_waiters(lock) == 0) || (rw_lock_get_waiters(lock) == 1)); ut_a((lock->writer != RW_LOCK_EX) || (lock->writer_count > 0)); mutex_exit(rw_lock_get_mutex(lock)); return(TRUE);}/**********************************************************************Lock an rw-lock in shared mode for the current thread. If the rw-lock islocked in exclusive mode, or there is an exclusive lock request waiting,the function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waitingfor the lock, before suspending the thread. */voidrw_lock_s_lock_spin(/*================*/ rw_lock_t* lock, /* in: pointer to rw-lock */ ulint pass, /* in: pass value; != 0, if the lock will be passed to another thread to unlock */ const char* file_name, /* in: file name where lock requested */ ulint line) /* in: line where requested */{ ulint index; /* index of the reserved wait cell */ ulint i; /* spin round count */ ut_ad(rw_lock_validate(lock));lock_loop: rw_s_spin_wait_count++; /* Spin waiting for the writer field to become free */ i = 0; while (rw_lock_get_writer(lock) != RW_LOCK_NOT_LOCKED && i < SYNC_SPIN_ROUNDS) { if (srv_spin_wait_delay) { ut_delay(ut_rnd_interval(0, srv_spin_wait_delay)); } i++; } if (i == SYNC_SPIN_ROUNDS) { os_thread_yield(); } if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu spin wait rw-s-lock at %p cfile %s cline %lu rnds %lu\n", (ulong) os_thread_pf(os_thread_get_curr_id()), lock, lock->cfile_name, (ulong) lock->cline, (ulong) i); } mutex_enter(rw_lock_get_mutex(lock)); /* We try once again to obtain the lock */ if (TRUE == rw_lock_s_lock_low(lock, pass, file_name, line)) { mutex_exit(rw_lock_get_mutex(lock)); return; /* Success */ } else { /* If we get here, locking did not succeed, we may suspend the thread to wait in the wait array */ rw_s_system_call_count++; sync_array_reserve_cell(sync_primary_wait_array, lock, RW_LOCK_SHARED, file_name, line, &index); rw_lock_set_waiters(lock, 1); mutex_exit(rw_lock_get_mutex(lock)); if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu OS wait rw-s-lock at %p cfile %s cline %lu\n", os_thread_pf(os_thread_get_curr_id()), lock, lock->cfile_name, (ulong) lock->cline); } rw_s_system_call_count++; rw_s_os_wait_count++; sync_array_wait_event(sync_primary_wait_array, index); goto lock_loop; } }/**********************************************************************This function is used in the insert buffer to move the ownership of anx-latch on a buffer frame to the current thread. The x-latch was set bythe buffer read operation and it protected the buffer frame while theread was done. The ownership is moved because we want that the currentthread is able to acquire a second x-latch which is stored in an mtr.This, in turn, is needed to pass the debug checks of index pageoperations. */voidrw_lock_x_lock_move_ownership(/*==========================*/ rw_lock_t* lock) /* in: lock which was x-locked in the buffer read */{ ut_ad(rw_lock_is_locked(lock, RW_LOCK_EX)); mutex_enter(&(lock->mutex)); lock->writer_thread = os_thread_get_curr_id(); lock->pass = 0; mutex_exit(&(lock->mutex));}/**********************************************************************Low-level function for acquiring an exclusive lock. */UNIV_INLINEulintrw_lock_x_lock_low(/*===============*/ /* out: RW_LOCK_NOT_LOCKED if did not succeed, RW_LOCK_EX if success, RW_LOCK_WAIT_EX, if got wait reservation */ rw_lock_t* lock, /* in: pointer to rw-lock */ ulint pass, /* in: pass value; != 0, if the lock will be passed to another thread to unlock */ const char* file_name,/* in: file name where lock requested */ ulint line) /* in: line where requested */{#ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(rw_lock_get_mutex(lock)));#endif /* UNIV_SYNC_DEBUG */ if (rw_lock_get_writer(lock) == RW_LOCK_NOT_LOCKED) { if (rw_lock_get_reader_count(lock) == 0) { rw_lock_set_writer(lock, RW_LOCK_EX); lock->writer_thread = os_thread_get_curr_id(); lock->writer_count++; lock->pass = pass; #ifdef UNIV_SYNC_DEBUG rw_lock_add_debug_info(lock, pass, RW_LOCK_EX, file_name, line);#endif lock->last_x_file_name = file_name; lock->last_x_line = line; /* Locking succeeded, we may return */ return(RW_LOCK_EX); } else { /* There are readers, we have to wait */ rw_lock_set_writer(lock, RW_LOCK_WAIT_EX); lock->writer_thread = os_thread_get_curr_id(); lock->pass = pass; lock->writer_is_wait_ex = TRUE;#ifdef UNIV_SYNC_DEBUG rw_lock_add_debug_info(lock, pass, RW_LOCK_WAIT_EX, file_name, line);#endif return(RW_LOCK_WAIT_EX); } } else if ((rw_lock_get_writer(lock) == RW_LOCK_WAIT_EX) && os_thread_eq(lock->writer_thread, os_thread_get_curr_id())) { if (rw_lock_get_reader_count(lock) == 0) { rw_lock_set_writer(lock, RW_LOCK_EX); lock->writer_count++; lock->pass = pass; lock->writer_is_wait_ex = FALSE;#ifdef UNIV_SYNC_DEBUG rw_lock_remove_debug_info(lock, pass, RW_LOCK_WAIT_EX); rw_lock_add_debug_info(lock, pass, RW_LOCK_EX, file_name, line);#endif lock->last_x_file_name = file_name; lock->last_x_line = line; /* Locking succeeded, we may return */ return(RW_LOCK_EX); } return(RW_LOCK_WAIT_EX); } else if ((rw_lock_get_writer(lock) == RW_LOCK_EX) && os_thread_eq(lock->writer_thread, os_thread_get_curr_id()) && (lock->pass == 0) && (pass == 0)) { lock->writer_count++;#ifdef UNIV_SYNC_DEBUG rw_lock_add_debug_info(lock, pass, RW_LOCK_EX, file_name, line);#endif lock->last_x_file_name = file_name; lock->last_x_line = line; /* Locking succeeded, we may return */ return(RW_LOCK_EX); } /* Locking did not succeed */ return(RW_LOCK_NOT_LOCKED);}/**********************************************************************NOTE! Use the corresponding macro, not directly this function! Lock anrw-lock in exclusive mode for the current thread. If the rw-lock is lockedin shared or exclusive mode, or there is an exclusive lock request waiting,the function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waitingfor the lock before suspending the thread. If the same thread has an x-lockon the rw-lock, locking succeed, with the following exception: if pass != 0,only a single x-lock may be taken on the lock. NOTE: If the same thread hasan s-lock, locking does not succeed! */voidrw_lock_x_lock_func(/*================*/ rw_lock_t* lock, /* in: pointer to rw-lock */ ulint pass, /* in: pass value; != 0, if the lock will be passed to another thread to unlock */ const char* file_name,/* in: file name where lock requested */ ulint line) /* in: line where requested */{ ulint index; /* index of the reserved wait cell */ ulint state; /* lock state acquired */ ulint i; /* spin round count */ ut_ad(rw_lock_validate(lock));lock_loop: /* Acquire the mutex protecting the rw-lock fields */ mutex_enter_fast(&(lock->mutex)); state = rw_lock_x_lock_low(lock, pass, file_name, line); mutex_exit(&(lock->mutex)); if (state == RW_LOCK_EX) { return; /* Locking succeeded */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -