Kernel synchronization mechanism API write_seqcount_begin,
Kernel synchronization mechanism API-write_seqcount_begin
The following example uses write_seqcount_begin and write_seqcount_end together as the write sequence lock: static void Merge (struct task_struct * tsk, nodemask_t * newmems) {task_lock (tsk); local_irq_disable (); values (& tsk-> mems_allowed_seq); nodes_or (tsk-> mems_allowed, tsk-> mems_allowed, * newmems); values (tsk, newmems); tsk-> mems_allowed = * newmems; write_seqcount_end (& tsk-> mems_allowed_seq); local_irq_enabl E (); task_unlock (tsk);} the source code is analyzed as follows: static inline void write_seqcount_begin_nested (seqcount_t * s, int subclass) {raw_write_seqcount_begin (s ); seqcount_acquire (& s-> dep_map, subclass, 0, _ RET_IP _);} static inline void write_seqcount_begin (seqcount_t * s) {counts (s, 0 );} static inline void write_seqcount_end (seqcount_t * s) {seqcount_release (& s-> dep_map, 1, _ RET_IP _); raw_write_seqcount_end (s );} After carefully analyzing this function, you can find out that in write_seqcount_begin-> callback-> raw_write_seqcount_begin static inline void Merge (seqcount_t * s) {s-> sequence ++; smp_wmb ();} in this function, 1 is added to the s-> sequence Sub-. In this way, when the read sequence is read, it is found that the value is 1, wait for write_seqcount_end-> into inline void raw_write_seqcount_end (seqcount_t * s) {smp_wmb (); s-> sequence ++;} to add 1 to s-> sequence, in this way, the value is greater than or equal to 2. Here, the write sequence Lock operation is completed, and the read sequence lock can be correctly obtained.