Skip to content

Commit

Permalink
fix v -cg -cstrict -o v cmd/v with tcc on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed May 10, 2024
1 parent 6e55b2f commit b14c901
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
6 changes: 4 additions & 2 deletions thirdparty/stdatomic/nix/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,12 @@ static inline void* atomic_load_ptr(void** x) {
static inline void atomic_store_ptr(void** x, void* y) {
atomic_store_explicit((_Atomic(uintptr_t)*)x, (uintptr_t)y, memory_order_seq_cst);
}
static inline int atomic_compare_exchange_weak_ptr(void** x, void** expected, intptr_t y) {
static inline int atomic_compare_exchange_weak_ptr(void** x, void** expected, void* y) {
// `void* y` should be `intptr_t y`, but tcc fails for it
return (int)atomic_compare_exchange_weak_explicit((_Atomic(uintptr_t)*)x, (unsigned long *)expected, (uintptr_t)y, memory_order_seq_cst, memory_order_seq_cst);
}
static inline int atomic_compare_exchange_strong_ptr(void** x, void** expected, intptr_t y) {
static inline int atomic_compare_exchange_strong_ptr(void** x, void** expected, void* y) {
// `void* y` should be `intptr_t y`, but tcc fails for it
return (int)atomic_compare_exchange_strong_explicit((_Atomic(uintptr_t)*)x, (unsigned long *)expected, (uintptr_t)y, memory_order_seq_cst, memory_order_seq_cst);
}
static inline void* atomic_exchange_ptr(void** x, void* y) {
Expand Down
24 changes: 12 additions & 12 deletions vlib/sync/channels.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn (mut ch Channel) close() {
}
mut nulladr := unsafe { nil }
for !C.atomic_compare_exchange_weak_ptr(voidptr(&ch.adr_written), voidptr(&nulladr),
isize(-1)) {
voidptr(-1)) {
nulladr = unsafe { nil }
}
ch.readsem_im.post()
Expand Down Expand Up @@ -185,13 +185,13 @@ fn (mut ch Channel) try_push_priv(src voidptr, no_block bool) ChanState {
mut wradr := C.atomic_load_ptr(unsafe { &voidptr(&ch.write_adr) })
for wradr != C.NULL {
if C.atomic_compare_exchange_strong_ptr(voidptr(&ch.write_adr), voidptr(&wradr),
isize(0))
unsafe { nil })
{
// there is a reader waiting for us
unsafe { C.memcpy(wradr, src, ch.objsize) }
mut nulladr := unsafe { nil }
for !C.atomic_compare_exchange_weak_ptr(voidptr(&ch.adr_written), voidptr(&nulladr),
isize(wradr)) {
voidptr(wradr)) {
nulladr = unsafe { nil }
}
ch.readsem_im.post()
Expand Down Expand Up @@ -226,7 +226,7 @@ fn (mut ch Channel) try_push_priv(src voidptr, no_block bool) ChanState {
if wradr != C.NULL {
mut src2 := src
if C.atomic_compare_exchange_strong_ptr(voidptr(&ch.read_adr), voidptr(&src2),
isize(0))
unsafe { nil })
{
ch.writesem.post()
continue
Expand All @@ -248,7 +248,7 @@ fn (mut ch Channel) try_push_priv(src voidptr, no_block bool) ChanState {
mut src2 := src
for sp := u32(0); sp < spinloops_ || read_in_progress; sp++ {
if C.atomic_compare_exchange_strong_ptr(voidptr(&ch.adr_read), voidptr(&src2),
isize(0))
unsafe { nil })
{
have_swapped = true
read_in_progress = true
Expand All @@ -271,15 +271,15 @@ fn (mut ch Channel) try_push_priv(src voidptr, no_block bool) ChanState {
}
if C.atomic_load_u16(&ch.closed) != 0 {
if have_swapped
|| C.atomic_compare_exchange_strong_ptr(voidptr(&ch.adr_read), voidptr(&src2), isize(0)) {
|| C.atomic_compare_exchange_strong_ptr(voidptr(&ch.adr_read), voidptr(&src2), unsafe { nil }) {
ch.writesem.post()
return .success
} else {
return .closed
}
}
if have_swapped
|| C.atomic_compare_exchange_strong_ptr(voidptr(&ch.adr_read), voidptr(&src2), isize(0)) {
|| C.atomic_compare_exchange_strong_ptr(voidptr(&ch.adr_read), voidptr(&src2), unsafe { nil }) {
ch.writesem.post()
break
} else {
Expand Down Expand Up @@ -376,13 +376,13 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
mut rdadr := C.atomic_load_ptr(unsafe { &voidptr(&ch.read_adr) })
for rdadr != C.NULL {
if C.atomic_compare_exchange_strong_ptr(voidptr(&ch.read_adr), voidptr(&rdadr),
isize(0))
unsafe { nil })
{
// there is a writer waiting for us
unsafe { C.memcpy(dest, rdadr, ch.objsize) }
mut nulladr := unsafe { nil }
for !C.atomic_compare_exchange_weak_ptr(voidptr(&ch.adr_read), voidptr(&nulladr),
isize(rdadr)) {
voidptr(rdadr)) {
nulladr = unsafe { nil }
}
ch.writesem_im.post()
Expand Down Expand Up @@ -473,7 +473,7 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
if rdadr != C.NULL {
mut dest2 := dest
if C.atomic_compare_exchange_strong_ptr(voidptr(&ch.write_adr), voidptr(&dest2),
isize(0))
unsafe { nil })
{
ch.readsem.post()
continue
Expand All @@ -495,7 +495,7 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
mut dest2 := dest
for sp := u32(0); sp < spinloops_ || write_in_progress; sp++ {
if C.atomic_compare_exchange_strong_ptr(voidptr(&ch.adr_written), voidptr(&dest2),
isize(0))
unsafe { nil })
{
have_swapped = true
break
Expand All @@ -519,7 +519,7 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
ch.readsem_im.wait()
}
if have_swapped
|| C.atomic_compare_exchange_strong_ptr(voidptr(&ch.adr_written), voidptr(&dest2), isize(0)) {
|| C.atomic_compare_exchange_strong_ptr(voidptr(&ch.adr_written), voidptr(&dest2), unsafe { nil }) {
ch.readsem.post()
break
} else {
Expand Down
4 changes: 2 additions & 2 deletions vlib/sync/stdatomic/1.declarations.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ $if linux {
// The following functions are actually generic in C
fn C.atomic_load_ptr(voidptr) voidptr
fn C.atomic_store_ptr(voidptr, voidptr)
fn C.atomic_compare_exchange_weak_ptr(voidptr, voidptr, isize) bool
fn C.atomic_compare_exchange_strong_ptr(voidptr, voidptr, isize) bool
fn C.atomic_compare_exchange_weak_ptr(voidptr, voidptr, voidptr) bool
fn C.atomic_compare_exchange_strong_ptr(voidptr, voidptr, voidptr) bool
fn C.atomic_exchange_ptr(voidptr, voidptr) voidptr
fn C.atomic_fetch_add_ptr(voidptr, voidptr) voidptr
fn C.atomic_fetch_sub_ptr(voidptr, voidptr) voidptr
Expand Down

0 comments on commit b14c901

Please sign in to comment.