{"id":440,"date":"2013-10-31T23:55:17","date_gmt":"2013-10-31T15:55:17","guid":{"rendered":"http:\/\/www.gpfeng.com\/?p=440"},"modified":"2013-11-01T10:54:00","modified_gmt":"2013-11-01T02:54:00","slug":"innodb-rw_lock_t%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/www.gpfeng.com\/?p=440","title":{"rendered":"InnoDB \u8bfb\u5199\u9501rw_lock_t\u5b9e\u73b0"},"content":{"rendered":"<p>10\u6708\u5fd9\u5f97\u50cfshi\u4e00\u6837\uff0c\u4e0d\u7ba1\u600e\u6837\uff0c\u5fd9\u91cc\u5077\u95f2\u5728\u8fd9\u4e2a\u6708\u7ed3\u675f\u4e4b\u524d\u5199\u4e0a\u4e00\u7bc7\uff0c\u4ecb\u7ecdInnoDB\u8bfb\u5199\u9501\u7684\u5b9e\u73b0<\/p>\n<h3>InnoDB\u8bfb\u5199\u9501\u7279\u70b9<\/h3>\n<p>1. \u8bfb\u4e0e\u8bfb\u4e4b\u95f4\u4e0d\u51b2\u7a81\uff0c\u8bfb\u4e0e\u5199\uff0c\u5199\u4e0e\u5199\u4e4b\u95f4\u51b2\u7a81\uff0c\u8fd9\u4e2a\u662f\u8bfb\u5199\u9501\u6700\u57fa\u672c\u7684\u7279\u5f81<br \/>\n2. \u7533\u8bf7\u5199\u9501\u65f6\uff0c\u5982\u679c\u9501\u88ab\u8bfb\u6301\u6709\uff0c\u5c06\u53d1\u751f\u5199\u9501\u7b49\u5f85\uff0c\u8be5\u5199\u9501\u4e4b\u540e\u7684\u6240\u6709\u8bfb\u9501\u4f1a\u7b49\u5f85\u8be5\u5199\u9501\u6210\u529f\u83b7\u53d6\u540e\u624d\u80fd\u7533\u8bf7\u6210\u529f\uff0c\u8fd9\u4fdd\u8bc1\u4e86\u516c\u5e73\uff1a\u5199\u9501\u8bf7\u6c42\u4e0d\u4f1a\u88ab\u997f\u6b7b<\/p>\n<h3>\u57fa\u672c\u6570\u636e\u7ed3\u6784<\/h3>\n<p><strong>sync_array_struct<\/strong>: \u4fdd\u5b58\u7528\u4e8e\u7ebf\u7a0b\u540c\u6b65\u7ed3\u6784\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u6210\u5458\u7c7b\u578b\u4e3async_cell_struct<br \/>\n<strong>sync_cell_struct<\/strong>: \u4e00\u4e2a\u7ebf\u7a0b\u540c\u6b65\u7ed3\u6784\u4f53\uff0c\u90e8\u5206\u4ee3\u7801\uff1a<!--more--><\/p>\n<pre lang=\"mysql\">\r\nstruct sync_cell_struct {\r\n\tvoid*\t\twait_object;\t\/*!< pointer to the object the\r\n\t\t\t\t\tthread is waiting for; if NULL\r\n\t\t\t\t\tthe cell is free for use *\/\r\n\tulint\t\trequest_type;\t\/*!< lock type requested on the\r\n\t\t\t\t\tobject *\/\r\n\tos_thread_id_t\tthread;\t\t\/*!< thread id of this waiting\r\n\t\t\t\t\tthread *\/\r\n\tibool\t\twaiting;\t\/*!< TRUE if the thread has already\r\n\t\t\t\t\tcalled sync_array_event_wait\r\n\t\t\t\t\ton this cell *\/\r\n\tib_int64_t\tsignal_count;\t\/*!< We capture the signal_count\r\n\t\t\t\t\tof the wait_object when we\r\n\t\t\t\t\treset the event. This value is\r\n\t\t\t\t\tthen passed on to os_event_wait\r\n\t\t\t\t\tand we wait only if the event\r\n\t\t\t\t\thas not been signalled in the\r\n\t\t\t\t\tperiod between the reset and\r\n\t\t\t\t\twait call. *\/\r\n\ttime_t\t\treservation_time;\/*!< time when the thread reserved\r\n\t\t\t\t\tthe wait cell *\/\r\n};\r\n<\/pre>\n<p>\u4ee5\u4e0a\u662f\u90e8\u5206\u6210\u5458\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4e0esrv_slot_struct\uff0csrv_conc_slot_struct\u4f5c\u7528\u57fa\u672c\u76f8\u540c\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u540c\u6b65\u4e8b\u4ef6\uff1awati_object\uff0c\u5176\u7c7b\u578b\u53ef\u4ee5\u662f\uff1arw_lock_t\u548cmutext_t\uff0c\u5176\u5185\u90e8\u90fd\u6709\u4e00\u4e2aos_event_t\u7c7b\u578b\u7684\u6210\u5458event\uff0c\u53ef\u4ee5\u5bf9\u5176\u6267\u884cset\/reset\/wait\uff0c\u60f3\u4e86\u89e3\u5176\u5177\u4f53\u5b9e\u73b0\uff0c\u8bf7\u53c2\u8003\u4ee3\u7801<\/p>\n<p><strong>rw_lock_struct<\/strong>\uff1a\u8bfb\u5199\u9501\u7ed3\u6784\u4f53\uff0c\u90e8\u5206\u6210\u5458\uff1a<\/p>\n<pre lang=\"mysql\">\r\ntypedef struct rw_lock_struct\t\trw_lock_t;\r\nstruct rw_lock_struct {\r\n\tvolatile lint\tlock_word;\r\n\t\t\t\t\/*!< Holds the state of the lock. *\/\r\n\tvolatile ulint\twaiters;\/*!< 1: there are waiters *\/\r\n\tvolatile os_thread_id_t\twriter_thread;\r\n\t\t\t\t\/*!< Thread id of writer thread. Is only\r\n\t\t\t\tguaranteed to have sane and non-stale\r\n\t\t\t\tvalue iff recursive flag is set. *\/\r\n\tos_event_t\tevent;\t\/*!< Used by sync0arr.c for thread queueing *\/\r\n\tos_event_t\twait_ex_event;\r\n\t\t\t\t\/*!< Event for next-writer to wait on. A thread\r\n\t\t\t\tmust decrement lock_word before waiting. *\/\r\n<\/pre>\n<p>\u6709\u4e24\u4e2aos_event_t\u7c7b\u578b\u7684\u6210\u5458\uff1aevent\u548cwait_ex_eveint\uff0c\u5176\u4e2dwait_ex_event\u7684\u4f5c\u7528\u662f\uff1a\u5982\u679c\u5f53\u524d\u9501\u53ea\u88ab\u8bfb\u8005\u6301\u6709\uff0c\u4e4b\u540e\u7b2c\u4e00\u4e2a\u5199\u9501\u8bf7\u6c42\u5c06\u5728\u8fd9\u4e2a\u4e8b\u4ef6\u4e0a\u7b49\u5f85\uff0c\u5f53\u8fd9\u4e9b\u8bfb\u8005\u4e2d\u6700\u540e\u4e00\u4e2aunlock\u65f6\uff0c\u4f1a\u5524\u9192\u7b49\u5f85\u8fd9\u4e2a\u4e8b\u4ef6\u4e0a\u7684\u5199\u7b49\u5f85\uff0c\u5176\u5b83\u7533\u8bf7\u9501\u65f6\u51b2\u7a81\u7684\u60c5\u51b5\u5c06\u5728event\u4e0a\u53d1\u751f\u6240\u7b49\u5f85\uff0c\u8fd9\u4e2a\u4e8b\u4ef6\u5728\u5199\u8005unlock\u65f6\u53bb\u5524\u9192<\/p>\n<p><strong>\u795e\u5947\u7684lock_word<\/strong><br \/>\nInnoDB\u5b9e\u73b0\u516c\u5e73\u8bfb\u5199\u9501\u662f\u9760\u8fd9\u4e2a\u6210\u5458\u6765\u4fdd\u8bc1\u7684\uff0c\u5b83\u88ab\u521d\u59cb\u5316\u4e3a\uff1aX_LOCK_DECR<\/p>\n<pre lang=\"mysql\">\r\n\/* We decrement lock_word by this amount for each x_lock. It is also the\r\nstart value for the lock_word, meaning that it limits the maximum number\r\nof concurrent read locks before the rw_lock breaks. The current value of\r\n0x00100000 allows 1,048,575 concurrent readers and 2047 recursive writers.*\/\r\n#define X_LOCK_DECR\t\t0x00100000\r\n<\/pre>\n<p>rw_lock_struct\u652f\u6301recursive\u6a21\u5f0f\uff0c\u672c\u6587\u4e0d\u6253\u7b97\u4ecb\u7ecd\u8fd9\u4e2a\uff0c\u4ece\u6ce8\u91ca\u4e2d\u53ef\u4ee5\u770b\u51faX_LOCK_DECR\u88ab\u521d\u59cb\u4e3a\u4e00\u4e2a\u8f83\u5927\u7684\u503c\uff0c\u90a3\u4e48\u5b83\u4ee3\u8868\u4ec0\u4e48\u610f\u601d\u5462\uff1f<br \/>\n1. \u6210\u529f\u7533\u8bf7\u4e00\u4e2a\u8bfb\u9501\uff0clock_word\u51cf1\uff0c\u91ca\u653e\u65f6\u52a01<br \/>\n2. \u6210\u529f\u7533\u8bf7\u4e00\u4e2a\u5199\u9501\uff0clock_word\u51cfX_LOCK_DECR\uff0c\u91ca\u653e\u65f6\u52a0X_LOCK_DECR<br \/>\n\u6839\u636e\u4ee5\u4e0a\u884c\u4e3a\uff0clock_word\u7684\u53d6\u503c\u533a\u95f4\u6709\uff1a\uff08recursive writers\u6a21\u5f0f\u4e0d\u5305\u542b\u5728\u5185\uff09<br \/>\n1. lock_word = X_LOCK_DECR\uff1a \u9501\u6ca1\u6709\u88ab\u6301\u6709<br \/>\n2. 0 < lock_word < X_LOCK_DECR\uff1a \u9501\u4e0a\u6709X_LOCK_DECR-lock_word\u4e2a\u8bfb\u8005\n3. lock_word = 0\uff1a \u9501\u4e0a\u6709\u4e00\u4e2a\u5199\u8005\n4. -X_LOCK_DECR < lock_word < 0\uff1a \u9501\u88ab\u8bfb\u8005\u6301\u6709\uff0c\u4f46\u662f\u4e0a\u9762\u5b58\u5728\u4e00\u4e2a\u5199\u7533\u8bf7\n\n<strong>\u539f\u7406<\/strong>: \u5bf9lock_word\u7684\u64cd\u4f5c\u88ab\u4fdd\u8bc1\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u4e14\u53ea\u6709\u5f53lock > 0 \u65f6\u624d\u5141\u8bb8\u52a0\u8bfb\u9501\u6216\u8005\u5199\u9501\uff0c\u5f53\u9501\u4e0a\u6709X_LOCK_DECR-lock_word\u4e2a\u8bfb\u8005\u65f6(\u60c5\u51b52)\uff0c\u4e4b\u540e\u7b2c\u4e00\u4e2a\u5199\u8bf7\u6c42\u4f1a\u539f\u5b50\u5730\u5c06lock_word\u51cf\u53bbX_LOCK_DECR(\u60c5\u51b53)\uff0c\u8fd9\u4fdd\u8bc1\u4e4b\u540e\u6765\u5230\u7684\u8bfb\u8bf7\u6c42\u4e0d\u4f1a\u5728\u8be5\u5199\u9501\u88ab\u83b7\u53d6\u4e4b\u524d\u8fdb\u5165<\/p>\n<h3>LOCK\/UNLOCK\u64cd\u4f5c<\/h3>\n<p><strong>rw_lock_x_lock_func<\/strong><br \/>\n1. \u539f\u5b50\u64cd\u4f5c\uff08\u5982\u679clock_word > 0\uff0c \u5c06\u5176\u51cf\u53bbX_LOCK_DECR\uff09\uff0c\u4e4b\u540espin\uff0c\u7b49\u5f85lock_word=0\uff0cspin\u6b21\u6570\u8d85\u8fc7\u4e00\u5b9a\u6b21\u6570\u540e\uff0c\u4ecesync_primary_wait_array\u4e2d\u627e\u5230\u4e00\u4e2async_cell\uff0c\u5c06\u5176event\u6307\u5411rw_lock_struct\u4e2d\u7684wait_ex_event\uff0c\u7b49\u5f85\u88ab\u5524\u9192\uff0c\u6ee1\u8db3lock_word=0\u65f6\u9000\u51fa<br \/>\n2. lock_word <= 0(\u5df2\u7ecf\u5b58\u5728\u5176\u4ed6\u5199\u9501\u8bf7\u6c42)\uff0c\u8fdb\u5165spin\uff0c\u7b49\u5f85lock_word >0, \u4e4b\u540e\u4ecesync_primary_wait_array\u4e2d\u627e\u5230\u4e00\u4e2async_cell\uff0c\u5c06\u5176event\u6307\u5411rw_lock_struct\u4e2d\u7684event\uff0c\u7b49\u5f85\u88ab\u5524\u9192\uff0c\u5524\u9192\u540e\uff0c\u8fdb\u51651\u4e2d\u903b\u8f91\u6267\u884c<\/p>\n<p><strong>rw_lock_x_unlock_func<\/strong><br \/>\n1. \u539f\u5b50\u64cd\u4f5c\uff08\u5c06lock_word\u589e\u52a0X_LOCK_DECR\uff09\uff0c\u5524\u9192event\u4e0a\u7b49\u5f85\u7684\u4e8b\u4ef6<\/p>\n<p><strong>rw_lock_s_lock_func<\/strong><br \/>\n1. \u539f\u5b50\u64cd\u4f5c\uff08\u5982\u679clock_word > 0\uff0c \u5c06\u5176\u51cf\u53bb1\u540e\u8fd4\u56de\uff09\uff0c\u5982\u679clock_word > 0\uff0c\u52a0\u9501\u6210\u529f\uff0c\u8fd4\u56de<br \/>\n2. lock_word <= 0\uff0c\u7b49\u5f85lock_word > 0\uff0cspin\u4e00\u5b9a\u6b21\u6570\u540e\uff0c\u4ecesync_primary_wait_array\u4e2d\u627e\u5230\u4e00\u4e2async_cell\uff0c\u5c06\u5176event\u6307\u5411rw_lock_struct\u4e2d\u7684event\uff0c\u7b49\u5f85\u88ab\u5524\u9192<\/p>\n<p><strong>rw_lock_s_unlock_func<\/strong><br \/>\n\u539f\u5b50\u64cd\u4f5c\uff08\u5c06lock_word\u589e\u52a01\uff09\uff0c\u5982\u679clock_word\u4e3a0\uff0c\u8bf4\u660e\u5b58\u5728\u5199\u9501\u7b49\u5f85\uff0c\u5524\u9192wait_ex_event<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>1. \u5199\u9501\u7533\u8bf7\u7531\u4e8e\u5f53\u65f6\u6709\u8bfb\u8005\uff08lock_word > 0\uff09\u65e0\u6cd5\u7533\u8bf7\u6210\u529f\u65f6\uff0c\u5c06lock_word\u51cf\u6210\u8d1f\u6570\uff0c\u5728wait_ex_event\u4e0a\u7b49\u5f85\u88ab\u5524\u9192<br \/>\n2. \u5199\u9501\u7533\u8bf7\u7531\u4e8e\u5f53\u65f6\u6709\u5199\u9501\/\u5199\u7b49\u5f85\uff08lock_word <= 0\uff09\u65e0\u6cd5\u7533\u8bf7\u6210\u529f\u65f6\uff0c\u5c06\u5728event\u4e0a\u7b49\u5f85\u88ab\u5524\u9192\uff0c\u88ab\u5524\u9192\u540e\u5c06\u6700\u7ec8\u8fdb\u51651\u4e2d\u903b\u8f91\n3. \u6700\u540e\u4e00\u4e2a\u8bfb\u9501\u91ca\u653e\u65f6\uff0c\u4f1a\u5524\u9192wait_ex_event\n4. \u5199\u9501\u91ca\u653e\u65f6\uff0c\u4f1a\u5524\u9192event\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>10\u6708\u5fd9\u5f97\u50cfshi\u4e00\u6837\uff0c\u4e0d\u7ba1\u600e\u6837\uff0c\u5fd9\u91cc\u5077\u95f2\u5728\u8fd9\u4e2a\u6708\u7ed3\u675f\u4e4b\u524d\u5199\u4e0a\u4e00\u7bc7\uff0c\u4ecb\u7ecdInnoDB\u8bfb\u5199\u9501\u7684\u5b9e\u73b0 InnoDB &hellip; <a href=\"http:\/\/www.gpfeng.com\/?p=440\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">InnoDB \u8bfb\u5199\u9501rw_lock_t\u5b9e\u73b0<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3tPZp-76","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/440"}],"collection":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=440"}],"version-history":[{"count":9,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":449,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/440\/revisions\/449"}],"wp:attachment":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}