-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(multi-cores): printf and malloc are now thread-safe
- Loading branch information
Showing
4 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "klib.h" | ||
#include <klib-macros.h> | ||
|
||
uint64_t compare_and_swap(volatile uint64_t* addr, uint64_t old_val, uint64_t new_val) { | ||
uint64_t check = 0; | ||
uint64_t value = 0; | ||
asm volatile ( | ||
"lr.d %[value], (%[addr]);" | ||
: [value]"=r"(value) | ||
: [addr]"p"(addr) | ||
); | ||
if (value != old_val) return 1; | ||
asm volatile ( | ||
"sc.d %[check], %[write], (%[addr]);" | ||
: [check]"=r"(check) | ||
: [write]"r"(new_val), [addr]"p"(addr) | ||
); | ||
return check; | ||
} | ||
|
||
void lock(volatile uint64_t *addr) { | ||
asm volatile("csrci mstatus, 0x8"); | ||
while(compare_and_swap(addr, 0, 1)); | ||
} | ||
|
||
void release(volatile uint64_t *addr) { | ||
*addr = 1; | ||
asm volatile("fence"); | ||
asm volatile("csrsi mstatus, 0x8"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters