RT-Thread / RT-Thread/rt-thread

Whether to consider adding "rt_hw_spin_trylock" function before spinlock to avoid CPU busy waiting?【是否考虑在spinlock之前先尝试获取锁trylock,避免CPU忙等】

Open
#8,354 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
12.2k
Forks
5.4k
Avg merge
4d 12h
Merged PRs (30d)
40

Description

The Linux kernel spinlock will try to lock before each spinlock. This attempt to lock can bring performance gains under fierce lock competition. Refer to the following kernel code:

static inline int arch_spin_trylock(arch_spinlock_t *lock)
{
	unsigned long contended, res;
	u32 slock;

	prefetchw(&lock->slock);
	do {
		__asm__ __volatile__(
		"	ldrex	%0, [%3]\n"
		"	mov	%2, #0\n"
		"	subs	%1, %0, %0, ror #16\n"
		"	addeq	%0, %0, %4\n"
		"	strexeq	%2, %0, [%3]"
		: "=&r" (slock), "=&r" (contended), "=&r" (res)
		: "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
		: "cc");
	} while (res);

	if (!contended) {
		smp_mb();
		return 1;
	} else {
		return 0;
	}
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating RT-Thread's spinlock implementation and architecture-specific lock entry points, then compare their contention behavior with the cited Linux trylock. Confirm the supported targets and expected API before deciding whether a trylock path belongs before blocking acquisition; completion would require an agreed scope and regression or performance evidence.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, linux
Domain
embedded-iot, operating-systems, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.