Skip to content

Commit 0dc7b9e

Browse files
Nicolas Pitrenashif
authored andcommitted
k_current_get(): make it a "const" function
This function always returns the same value for a given thread. Add the const attribute to it so the compiler won't call it over and over needlessly each time _current is referenced, making for far more efficient code. The __attribute_const__ symbol is used to mimic the Linux equivalent. We want to make it clear that this is distinct from the const keyword. Fix the test_x86_cpu_scrubs_regs where the compiler wasn't told that a bunch of registers are being clobbered as highlighted by this change. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent a0905ef commit 0dc7b9e

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
"__used",
181181
"__unused",
182182
"__weak",
183+
"__attribute_const__",
183184
"__DEPRECATED_MACRO",
184185
"FUNC_NORETURN",
185186
"__subsystem",

include/kernel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ __syscall void k_wakeup(k_tid_t thread);
528528
* @return ID of current thread.
529529
*
530530
*/
531-
__syscall k_tid_t k_current_get(void);
531+
__syscall k_tid_t k_current_get(void) __attribute_const__;
532532

533533
/**
534534
* @brief Abort a thread.

include/toolchain/gcc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ do { \
190190
#ifndef __deprecated
191191
#define __deprecated __attribute__((deprecated))
192192
#endif
193+
#ifndef __attribute_const__
194+
#define __attribute_const__ __attribute__((__const__))
195+
#endif
193196
#define ARG_UNUSED(x) (void)(x)
194197

195198
#define likely(x) __builtin_expect((bool)!!(x), true)

tests/arch/x86/cpu_scrubs_regs/src/main.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,26 @@ void z_impl_test_cpu_write_reg(void)
2727
#if CONFIG_X86
2828
#ifndef CONFIG_X86_64
2929
__asm__ volatile (
30-
"movl $0xDEADBEEF, %eax;\n\t"
31-
"movl $0xDEADBEEF, %ebx;\n\t"
32-
"movl $0xDEADBEEF, %ecx;\n\t"
33-
"movl $0xDEADBEEF, %edx;\n\t"
34-
"movl $0xDEADBEEF, %edi;\n\t"
30+
"movl $0xDEADBEEF, %%eax;\n\t"
31+
"movl $0xDEADBEEF, %%ebx;\n\t"
32+
"movl $0xDEADBEEF, %%ecx;\n\t"
33+
"movl $0xDEADBEEF, %%edx;\n\t"
34+
"movl $0xDEADBEEF, %%edi;\n\t"
35+
: : : "eax", "ebx", "ecx", "edx", "edi"
3536
);
3637
#else
3738
__asm__ volatile (
38-
"movq $0xDEADBEEF, %rax;\n\t"
39-
"movq $0xDEADBEEF, %rcx;\n\t"
40-
"movq $0xDEADBEEF, %rdx;\n\t"
41-
"movq $0xDEADBEEF, %rsi;\n\t"
42-
"movq $0xDEADBEEF, %rdi;\n\t"
43-
"movq $0xDEADBEEF, %r8;\n\t"
44-
"movq $0xDEADBEEF, %r9;\n\t"
45-
"movq $0xDEADBEEF, %r10;\n\t"
46-
"movq $0xDEADBEEF, %r11;\n\t"
39+
"movq $0xDEADBEEF, %%rax;\n\t"
40+
"movq $0xDEADBEEF, %%rcx;\n\t"
41+
"movq $0xDEADBEEF, %%rdx;\n\t"
42+
"movq $0xDEADBEEF, %%rsi;\n\t"
43+
"movq $0xDEADBEEF, %%rdi;\n\t"
44+
"movq $0xDEADBEEF, %%r8;\n\t"
45+
"movq $0xDEADBEEF, %%r9;\n\t"
46+
"movq $0xDEADBEEF, %%r10;\n\t"
47+
"movq $0xDEADBEEF, %%r11;\n\t"
48+
: : : "rax", "rcx", "rdx", "rsi", "rdi",
49+
"r8", "r9", "r10", "r11"
4750
);
4851
#endif
4952
#endif

0 commit comments

Comments
 (0)