0

After reading Robert Love's LKD, I learn rwlock and seqlock, both of them are based on spinlock.

When distinguish between reader and writer, rwlock is better than spinlock, it will get better performace. However, rwlock will make writer hungury.

seqlock solve rwlock making writer hungury problem, however, there are less use of seqlock than rwlock. So, why rwlock is more popular than seqlock?

red0ct
  • 4,840
  • 3
  • 17
  • 44
buwei lv
  • 13
  • 1

1 Answers1

1

A seqlock has a strong limitation, that readers should correctly work with inconsistent data.

Not every processing algorithm allows inconstistent data. In most cases, such data can only be numbers: integers, booleans, etc. They rarely can be pointers, because a stale pointer may point to the memory which is already freed, so dereferencing such pointer is no-no.

Locks (and rw-locks among them) doesn't have "inconsitent data" limitations, so they can be used in much more cases.

Example of inconstisten data under seqlock

Assume there are two structure's fields protected by the single seqlock. The first field, a is incremented by each "write", the second field, b is decremented by each "write". Both fields initially are 0.

On may assume, that a reader will always find a + b to be 0.

But in case of seqlock, this is not true. E.g., between reading a and b it could a "write", so a value will be old, and b value will be new, and a + b gives -1.

Community
  • 1
  • 1
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • `do { seq = read_seqcount_begin(&x); do_sth(); } while (read_seqcount_retry(&x, seq));` solves the inconsistency you mentioned. Because `read_seqcount_retry` detects this inconsistency by comparing new seqcount with old seqcount – Donald Wu Jun 02 '21 at 03:35
  • 1
    @DonaldWu: The construction you show only **detects** inconsistency of data. It does NOT **protect** `do_sth()` from viewing inconsistent data and processing it. This is what my answer is talking about. – Tsyvarev Jun 02 '21 at 07:20