Linux???????????
???????????? ???????[ 2015/2/12 13:57:41 ] ????????Linux ?????? ???
????????????????????????????????????????????μ??????????????????????????????????????????????????????????????????????????????????????????????????????????£???????????????????????????????л?????????????????????????????????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????????“??????”????ó?????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11