????????????????????????????????????????????μ??????????????????????????????????????????????????????????????????????????????????????????????????????????£???????????????????????????????л?????????????????????????????????api??????????????????????????????????????????????????????????????linux?????????????4????????????????????д???????????????????????????????????
????1. spinlock
????1)  ????
????spinlock??????????????CPU??????????????????????????????????????????????????????????δ??????????????????????????????з????
????2)  ???
????spinlock??????????read-modify-write????????read-modify-write???????????CPU??????????????????????????????д????????????????????????????????????CPU???????????????????test-and-set?????????????ж????????????0?????????????е???????1??
????3) ??????
????a) ??????
????1   test_and_set(volatile int* addr?? value)
????2   {
????3         return os_atomic_test_and_set_int(addr??value);
????4    }
????????volatile???δ???????????????л???????????????????????ж?????????????
????b) ??????????
1 set_spinlock(lock_word)
2 {
3          int i = 0;
4          int value;
5          while (true)
6          {
7               value = test_and_set(&lock_word?? 1);
8               if (value == 0) //??δ????????????
9                      break;
10               else   //???????????????????????
11                     do nothing
12          }
13 }
????c)       ??????????
????1 reset_spinlock(lock_word)
????2 {
????3    test_and_set (lock_word?? 0);
????4 }
????2.  mutex
????1) ????
?????? spinlock????????????????????????????
????2) ??????
????int pthread_mutex_lock(pthread_mutex_t *mutex)
????int pthread_mutex_trylock(pthread_mutex_t *mutex)
????int pthread_mutex_unlock(pthread_mutex_t *mutex)
????pthread_mutex_trylock()??????pthread_mutex_lock()?????????????????????????????EBUSY?????????????
????3)  spinlock??mutex??????
????a) ???÷?Χ??mutex?????????????????????????????????spinlock???÷?Χ???????(??????????????)???????????????????
????b) spinlock????????????????????
????c) ?????spinlock???????????????????????????????????????CPU??????????mutex???????????????????wait???????????????
????d) ????3??????????????????????????????漰??CPU?????????л?????CPU?л????????????????25us????????spinlock???????????????Ч??????
????e) ?????3?????spinlock??????????????????CPU???????????????????????????????????ò?????????spinlock????????“??????”????ó?????