-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for threads and atomic operations
- Loading branch information
Showing
4 changed files
with
75 additions
and
5 deletions.
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,58 @@ | ||
#include <uvm/syscalls.h> | ||
#include <stdio.h> | ||
|
||
#define NUM_THREADS 100 | ||
#define NUM_INCRS 100 | ||
|
||
u64 thread_ids[NUM_THREADS]; | ||
|
||
u64 locked = 0; | ||
|
||
u64 counter = 0; | ||
|
||
void lock() | ||
{ | ||
for (;;) | ||
{ | ||
// Try to acquire the lock | ||
u64 val = asm (&locked, 0, 1) -> u64 { atomic_cas_u64; }; | ||
|
||
// If we got the lock, stop | ||
if (val == 0) | ||
break; | ||
} | ||
} | ||
|
||
void unlock() | ||
{ | ||
asm (&locked, 0) -> void { atomic_store_u64; }; | ||
} | ||
|
||
void thread_fn() | ||
{ | ||
for (int i = 0; i < NUM_INCRS; ++i) | ||
{ | ||
lock(); | ||
++counter; | ||
unlock(); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
for (int i = 0; i < NUM_THREADS; ++i) | ||
{ | ||
thread_ids[i] = thread_spawn(thread_fn, NULL); | ||
} | ||
|
||
for (int i = 0; i < NUM_THREADS; ++i) | ||
{ | ||
thread_join(thread_ids[i]); | ||
} | ||
|
||
// Check that the counter value is correct | ||
assert(counter == NUM_THREADS * NUM_INCRS); | ||
printf("counter = %d\n", counter); | ||
|
||
return 0; | ||
} |
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