DPDK中的同步与互斥

在dpdk一些代码示例中,有一些使用到了读写锁和原子操作,后者也用于无锁队列的实现。
如原子操作:

139 static inline int
140 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
141 {                         
142     uint8_t res;
143 
144     asm volatile(
145             MPLOCKED
146             "cmpxchgl %[src], %[dst];"
147             "sete %[res];"
148             : [res] "=a" (res),     /* output */
149               [dst] "=m" (*dst)
150             : [src] "r" (src),      /* input */
151               "a" (exp),
152               "m" (*dst)
153             : "memory");            /* no-clobber list */
154     return res;
155 }    //2018-7-14从另一实现修改为当前实现

396 /**         
397  * The atomic counter structure.
398  */         
399 typedef struct {
400     volatile int32_t cnt; /**< An internal counter value. */
401 } rte_atomic32_t;

164 static inline void
165 rte_atomic32_inc(rte_atomic32_t *v)
166 {
167     int t;
168 
169     asm volatile(
170             "1: lwarx %[t],0,%[cnt]\n"
171             "addic %[t],%[t],1\n"
172             "stwcx. %[t],0,%[cnt]\n"
173             "bne- 1b\n"
174             : [t] "=&r" (t), "=m" (v->cnt)
175             : [cnt] "r" (&v->cnt), "m" (v->cnt)
176             : "cc", "xer", "memory");
177 }
178 
179 static inline void
180 rte_atomic32_dec(rte_atomic32_t *v)
181 {
182     int t;
183 
184     asm volatile(
185             "1: lwarx %[t],0,%[cnt]\n"
186             "addic %[t],%[t],-1\n"
187             "stwcx. %[t],0,%[cnt]\n"
188             "bne- 1b\n"
189             : [t] "=&r" (t), "=m" (v->cnt)
190             : [cnt] "r" (&v->cnt), "m" (v->cnt)
191             : "cc", "xer", "memory");
192 }

内嵌汇编代码,volatile关键字的作用可以看下以前写的文章。

而读写锁是一种特殊的自旋锁,能提高并发性,读锁之间共享资源,写锁之间互斥,读写锁互斥;用于临界资源代码短的情况;线程在等资源期间不能被投入睡眠,只能是忙等,不然睡眠和唤醒也是相当费时的。

 56 /**
 57  * The rte_rwlock_t type.
 58  *
 59  * cnt is -1 when write lock is held, and > 0 when read locks are held.
 60  */
 61 typedef struct {
 62     volatile int32_t cnt; /**< -1 when W lock held, > 0 when R locks held. */
 63 } rte_rwlock_t;

118 /**
119  * Take a write lock. Loop until the lock is held.
120  *
121  * @param rwl
122  *   A pointer to a rwlock structure.
123  */
124 static inline void
125 rte_rwlock_write_lock(rte_rwlock_t *rwl)
126 {
127     int32_t x;
128     int success = 0;
129 
130     while (success == 0) {
131         x = rwl->cnt;
132         /* a lock is held */
133         if (x != 0) {
134             rte_pause();
135             continue;
136         }
137         success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt,
138                           0, -1);
139     }
140 }
142 /**
143  * Release a write lock.
144  *
145  * @param rwl
146  *   A pointer to a rwlock structure.
147  */
148 static inline void
149 rte_rwlock_write_unlock(rte_rwlock_t *rwl)
150 {
151     rte_atomic32_inc((rte_atomic32_t *)(intptr_t)&rwl->cnt);
152 }

 82 /**
 83  * Take a read lock. Loop until the lock is held.
 84  *
 85  * @param rwl
 86  *   A pointer to a rwlock structure.
 87  */
 88 static inline void
 89 rte_rwlock_read_lock(rte_rwlock_t *rwl)
 90 {   
 91     int32_t x;
 92     int success = 0;
 93     
 94     while (success == 0) {
 95         x = rwl->cnt;
 96         /* write lock is held */
 97         if (x < 0) {
 98             rte_pause();
 99             continue;
100         }
101         success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt,
102                           x, x + 1);
103     }
104 }
105 
106 /**
107  * Release a read lock.
108  *
109  * @param rwl
110  *   A pointer to the rwlock structure.
111  */
112 static inline void
113 rte_rwlock_read_unlock(rte_rwlock_t *rwl)
114 {   
115     rte_atomic32_dec((rte_atomic32_t *)(intptr_t)&rwl->cnt);
116 }

而普通自旋锁用的比较少,在使用过程中需要注意的是:
不能在获得同一个自旋锁的情况下再获得同一个自旋锁,否则会形成死锁的情况;在获得自旋锁前,禁止中断,防止中断处理程序也获得该自旋锁;
相关实现可参考:http://dpdk.org/doc/api/rte__spinlock_8h.html

https://en.wikipedia.org/wiki/Volatile_(computer_programming)

你可能感兴趣的:(DPDK中的同步与互斥)