Skynet Source Learning-read-write lock

Source: Internet
Author: User

  Skynet uses the built-in atomic operation to implement a read-write lock that focuses on understanding "full memory barrier", a read-write lock implemented in UNPV2 using mutual exclusion and conditional variables. The former is in the case of hardware support, appear simple and clear, the level of the station is not the same.
Source Stickers:
struct Rwlock {int write;int read;}; Static inline Voidrwlock_init (struct Rwlock *lock) {lock->write = 0;lock->read = 0;} Static inline Voidrwlock_rlock (struct Rwlock *lock) {for (;;) {//isuued a full memory barrier. This typically means, operations issued//prior to the barrier is guaranteed to be performed before operations issue D after the Barrier.while (lock->write) {__sync_synchronize ();} __sync_add_and_fetch (&lock->read,1);//After giving Nreaders + 1 to check again if there is a writer, some words this read lock request failed if (Lock->write) {__sync_sub _and_fetch (&lock->read,1);} else {break;}}} Static inline Voidrwlock_wlock (struct Rwlock *lock) {//If there is no writer, __sync_lock_test_and_set will return 0, indicating that the request was successfully written, or else that there was another writer, Then the idle while (__sync_lock_test_and_set (&lock->write,1)) {}//discovers that a reader enters before starting the write, wait until the previous operation completes while (Lock->read) {__ Sync_synchronize ();}} Static inline Voidrwlock_wunlock (struct Rwlock *lock) {__sync_lock_release (&lock->write);} Static inline Voidrwlock_runlock (struct Rwlock *lock) {__sync_sub_and_fetch (&lock->read,1);} 

Write a simple program to run down:





Skynet Source Learning-read-write lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.