Order Linux device drivers lock

When the resource to be protected is very small, very simple, will be accessed frequently and rarely write access and must be fast time (that is, reading is not allowed to make written starvation), you can use sequential lock (seqlock); Essentially order lock allows readers free access to resources, but those who need to read and write the check for clashes, when such conflicts occur, you need to retry the access to resources;

While sequentially locks are usually not suitable for protecting a data structure containing pointers, as in the modification of the data structure of writers, which may follow a read pointer is invalid;

The method defined in seqlock <linux / seqlock> usually are used to initialize seqlock:

Statically defined and initialized:

1 #define DEFINE_SEQLOCK(x)

Dynamic Definition and initialization:

1 seqlock_t x;
2 #define seqlock_init(x)

Write lock use is as follows, write locks are implemented using spin locks:

1 write_seqlock(&x)
2 /* 写数据 */
3 write_sequnlock(&x);

Read access by obtaining an unsigned integer value into the order of the critical region, on exit, the current sequence value rendezvous value, if not equal, the read access must be retried; code readers written as follows:

. 1  do {
 2      SEQ read_seqbegin = (& the_lock);
 . 3      / * needs to be done * / 
. 4 } the while read_deqretry (& the_lock, SEQ);

This type of lock is typically used to protect certain types of simple calculations, this calculation requires a plurality of identical values, found concurrent modifications have occurred, the result can be simply discarded and recalculated if the calculation result;

The kernel used jiffies acquired, as follows:

 1 u64 get_jiffies_64(void)
 2 {
 3     unsigned long seq;
 4     u64 ret;
 5 
 6     do {
 7         seq = read_seqbegin(&jiffies_lock);
 8         ret = jiffies_64;
 9     } while (read_seqretry(&jiffies_lock, seq));
10     return ret;
11 }

 

If you use seqlock in the interrupt handler, you should use IRQ secure version;

 

Guess you like

Origin www.cnblogs.com/wanpengcoder/p/11760767.html