vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.71k stars 2.16k forks source link

atomics do not work properly in tcc #16204

Open spytheman opened 1 year ago

spytheman commented 1 year ago

V doctor:

OS: linux, Ubuntu 20.04.3 LTS
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz
CC version: cc (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0

getwd: /v/vnew
vmodules: /home/delian/.vmodules
vroot: /v/vnew
vexe: /v/vnew/v
vexe mtime: 2022-10-25 09:33:52
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.1 bfbfe78.eaefe01

Git version: git version 2.25.1
Git vroot status: weekly.2022.43-2-geaefe019-dirty
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

What did you do? v -g -o vdbg cmd/v && vdbg i.v

struct App {
mut:
    aidx atomic int
    idx  int
}

fn (mut app App) worker() {
    for _ in 0 .. 10000 {
        app.aidx++
        app.idx++
    }
}

fn main() {
    mut app := &App{}
    dump(app)
    mut threads := []thread{}
    for _ in 0 .. 100 {
        threads << go app.worker()
    }
    threads.wait()
    dump(app)
}

What did you expect to see?

The second dump should show aidx: 1000000 with tcc too. It does with gcc and clang.

What did you see instead?

[i.v:16] app: &App{
    aidx: 0
    idx: 0
}
[i.v:22] app: &App{
    aidx: 773688
    idx: 988763
}
felipensp commented 1 year ago

It seems working to me. Can you check it again?

spytheman commented 1 year ago

yep, the problem remains with latest V 0.3.2 9b28a7a:

#0 20:14:17 ᛋ master /v/vclean❱./v version
V 0.3.2 9b28a7a
#0 20:14:23 ᛋ master /v/vclean❱./v -cc gcc run zz.v 
[zz.v:16] app: &App{
    aidx: 0
    idx: 0
}
[zz.v:22] app: &App{
    aidx: 1000000
    idx: 669888
}
#0 20:14:28 ᛋ master /v/vclean❱
#0 20:14:28 ᛋ master /v/vclean❱./v -cc tcc run zz.v 
[zz.v:16] app: &App{
    aidx: 0
    idx: 0
}
[zz.v:22] app: &App{
    aidx: 921126
    idx: 932339
}
#0 20:14:32 ᛋ master /v/vclean❱
felipensp commented 5 days ago

tcc does not support atomic operations. it just parses it.