-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
arm64: implement atomic-store with CAS
Only available with ARMv8.1 or higher, so behind a flag that would be enabled by an ACE compatible compiler depending on -march. As a sideeffect, avoids the races that could result in an infloop with M1.
- Loading branch information
Showing
3 changed files
with
209 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* $CC [-DDEADLOCK] -Isljit_src -o deadlock deadlock.c sljit_src/sljitLir.c | ||
* | ||
* SPDX: 0BSD | ||
* | ||
* © 2023 Carlo Marcelo Arenas Belón | ||
*/ | ||
|
||
#include "sljitLir.h" | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <pthread.h> | ||
|
||
struct data { | ||
long value; | ||
long *canary; | ||
pthread_t id; | ||
}; | ||
typedef void *(*worker_fn)(void *); | ||
|
||
static int gdb; | ||
|
||
static worker_fn create_worker(void) | ||
{ | ||
worker_fn code; | ||
struct sljit_label *retry; | ||
struct sljit_jump *exit, *skip; | ||
struct sljit_compiler *C = sljit_create_compiler(NULL, NULL); | ||
#if SLJIT_CONFIG_X86 | ||
sljit_u8 inst = 0xcc; | ||
#elif SLJIT_CONFIG_S390X | ||
sljit_u8 inst[2] = { 0x0, 0x1 }; | ||
#elif SLJIT_CONFIG_ARM_64 | ||
sljit_u32 inst = 0xd4200000; | ||
#elif SLJIT_CONFIG_ARM_THUMB2 | ||
sljit_u16 inst = 0xde01; | ||
#elif SLJIT_CONFIG_ARM | ||
sljit_u32 inst = 0xe7d001f0; | ||
#else | ||
//#error "Not Supported" | ||
#endif | ||
|
||
sljit_emit_enter(C, 0, SLJIT_ARGS1(P, P), 6, 1, 0, 0, 0); | ||
if (gdb) | ||
sljit_emit_op_custom(C, &inst, sizeof(inst)); | ||
sljit_emit_op2(C, SLJIT_ADD, SLJIT_R5, 0, SLJIT_S0, 0, | ||
SLJIT_IMM, SLJIT_OFFSETOF(struct data, canary)); | ||
sljit_emit_op1(C, SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, 0); | ||
retry = sljit_emit_label(C); | ||
sljit_emit_atomic_load(C, SLJIT_MOV, SLJIT_R2, SLJIT_S0); | ||
// skip = sljit_emit_cmp(C, SLJIT_EQUAL, SLJIT_MEM1(SLJIT_R4), 0, | ||
// SLJIT_IMM, 0); | ||
sljit_emit_op1(C, SLJIT_MOV, SLJIT_R4, 0, SLJIT_MEM1(SLJIT_R5), 0); | ||
#ifdef DEADLOCK | ||
sljit_emit_op1(C, SLJIT_MOV, SLJIT_MEM1(SLJIT_R4), 0, SLJIT_IMM, 1000); | ||
#else | ||
sljit_emit_op1(C, SLJIT_MOV, SLJIT_R4, 0, SLJIT_MEM1(SLJIT_R4), 0); | ||
#endif | ||
// sljit_set_label(skip, sljit_emit_label(C)); | ||
sljit_emit_op2(C, SLJIT_ADD, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1); | ||
sljit_emit_atomic_store(C, SLJIT_MOV | SLJIT_SET_ATOMIC_STORED, | ||
SLJIT_R1, SLJIT_S0, SLJIT_R2); | ||
exit = sljit_emit_jump(C, SLJIT_ATOMIC_STORED); | ||
sljit_set_label(sljit_emit_jump(C, SLJIT_JUMP), retry); | ||
sljit_set_label(exit, sljit_emit_label(C)); | ||
sljit_emit_op1(C, SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 0); | ||
sljit_emit_return(C, SLJIT_MOV_P, SLJIT_RETURN_REG, 0); | ||
|
||
code = sljit_generate_code(C); | ||
|
||
sljit_free_compiler(C); | ||
return code; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int winner = 0, looser = 0, other = 0; | ||
int i, num_threads = -1; | ||
struct data *threads; | ||
worker_fn code; | ||
|
||
if (argc > 1) | ||
num_threads = atoi(argv[1]); | ||
|
||
if (argc > 2) | ||
gdb = 1; | ||
|
||
code = create_worker(); | ||
|
||
if (num_threads <= 0) | ||
num_threads = 1; | ||
|
||
threads = calloc(num_threads, sizeof(struct data)); | ||
for (i = 0; i < num_threads; i++) { | ||
threads[i].canary = i ? &threads[i - 1].value : | ||
&threads[num_threads - 1].value; | ||
pthread_create(&threads[i].id, NULL, code, &threads[i].value); | ||
} | ||
for (i = 0; i < num_threads; i++) | ||
pthread_join(threads[i].id, NULL); | ||
for (i = 0; i < num_threads; i++) { | ||
switch(threads[i].value) { | ||
case 1: ++winner; break; | ||
case 1000: ++looser; break; | ||
default: ++other; printf("%ld\n", threads[i].value); | ||
} | ||
} | ||
|
||
printf("threads: %d won, %d lost, %d hang\n", winner, looser, other); | ||
|
||
free(threads); | ||
sljit_free_code((void *)code, NULL); | ||
|
||
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