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