mirror of
https://github.com/apache/nuttx.git
synced 2026-05-31 23:40:19 +08:00
seqlock: Improve seqlock performance.
This commit improved the seqlock performance on non-SMP and SMP platforms by 31.7% on average (83Mops -> 106Mops, tested on qemu-intel64/KVM). Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit is contained in:
committed by
Alan C. Assis
parent
ef1fb35462
commit
a8724387e7
+10
-6
@@ -34,7 +34,7 @@
|
|||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#define SEQLOCK_INITIALIZER {0}
|
#define SEQLOCK_INITIALIZER { 1u, 1u }
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Data Types
|
* Public Data Types
|
||||||
@@ -47,6 +47,7 @@
|
|||||||
typedef struct seqclock
|
typedef struct seqclock
|
||||||
{
|
{
|
||||||
atomic_t sequence;
|
atomic_t sequence;
|
||||||
|
uint32_t next_sequence;
|
||||||
} seqcount_t;
|
} seqcount_t;
|
||||||
|
|
||||||
#undef EXTERN
|
#undef EXTERN
|
||||||
@@ -78,7 +79,8 @@ extern "C"
|
|||||||
|
|
||||||
static inline_function void seqlock_init(FAR seqcount_t *s)
|
static inline_function void seqlock_init(FAR seqcount_t *s)
|
||||||
{
|
{
|
||||||
atomic_init(&s->sequence, 0u);
|
atomic_init(&s->sequence, 1u);
|
||||||
|
s->next_sequence = 1u;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -107,7 +109,7 @@ uint32_t read_seqbegin(FAR const seqcount_t *s)
|
|||||||
|
|
||||||
seq = atomic_read_acquire(&s->sequence);
|
seq = atomic_read_acquire(&s->sequence);
|
||||||
}
|
}
|
||||||
while (seq & 1);
|
while (seq == 0u);
|
||||||
|
|
||||||
return seq;
|
return seq;
|
||||||
}
|
}
|
||||||
@@ -163,11 +165,11 @@ irqstate_t write_seqlock_irqsave(FAR seqcount_t *s)
|
|||||||
{
|
{
|
||||||
sequence = atomic_read(&s->sequence);
|
sequence = atomic_read(&s->sequence);
|
||||||
|
|
||||||
if (predict_true(!(sequence & 1)))
|
if (predict_true(sequence != 0u))
|
||||||
{
|
{
|
||||||
/* Try to acquire the lock ownership. */
|
/* Try to acquire the lock ownership. */
|
||||||
|
|
||||||
if (atomic_cmpxchg_acquire(&s->sequence, &sequence, sequence + 1))
|
if (atomic_cmpxchg_acquire(&s->sequence, &sequence, 0u))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -198,7 +200,9 @@ irqstate_t write_seqlock_irqsave(FAR seqcount_t *s)
|
|||||||
static inline_function
|
static inline_function
|
||||||
void write_sequnlock_irqrestore(seqcount_t *s, irqstate_t flags)
|
void write_sequnlock_irqrestore(seqcount_t *s, irqstate_t flags)
|
||||||
{
|
{
|
||||||
atomic_set_release(&s->sequence, s->sequence + 1);
|
uint32_t next = s->next_sequence + 2;
|
||||||
|
s->next_sequence = next;
|
||||||
|
atomic_set_release(&s->sequence, next);
|
||||||
up_irq_restore(flags);
|
up_irq_restore(flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user