From b411d29577c74be40e61d968c91d46abce0d6930 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 3 Apr 2020 19:44:49 +0300 Subject: [PATCH] v2: fix support for 'go fn()' in modules. sync.pool is again multithreaded. --- vlib/sync/pool.v | 8 +++----- vlib/v/gen/cgen.v | 23 ++++++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/vlib/sync/pool.v b/vlib/sync/pool.v index 59bef8e136..506873b796 100644 --- a/vlib/sync/pool.v +++ b/vlib/sync/pool.v @@ -116,9 +116,7 @@ pub fn (pool mut PoolProcessor) work_on_pointers(items []voidptr) { pool.thread_contexts << [voidptr(0)].repeat(pool.items.len) pool.waitgroup.add(njobs) for i := 0; i < njobs; i++ { - // TODO: uncomment when `go func()` works again with v2 - // go process_in_thread(pool,i) - process_in_thread(pool,i) + go process_in_thread(pool,i) } pool.waitgroup.wait() } @@ -217,11 +215,11 @@ pub fn (pool &PoolProcessor) get_thread_context(idx int) voidptr { // TODO: remove everything below this line after generics are fixed: pub struct SResult { -pub: +pub: s string } pub struct IResult { -pub: +pub: i int } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 8ecb9cf123..f65cabf0f7 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -25,6 +25,7 @@ struct Gen { typedefs strings.Builder definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file) inits strings.Builder // contents of `void _vinit(){}` + gowrappers strings.Builder // all go callsite wrappers table &table.Table pref &pref.Preferences mut: @@ -63,6 +64,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string out: strings.new_builder(100) typedefs: strings.new_builder(100) definitions: strings.new_builder(100) + gowrappers: strings.new_builder(100) inits: strings.new_builder(100) table: table pref: pref @@ -100,7 +102,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string if g.is_test { g.write_tests_main() } - return g.hashes() + g.typedefs.str() + g.definitions.str() + g.out.str() + return g.hashes() + g.typedefs.str() + g.definitions.str() + g.gowrappers.str() + g.out.str() } pub fn (g &Gen) hashes() string { @@ -2839,22 +2841,26 @@ fn (g mut Gen) go_stmt(node ast.GoStmt) { g.definitions.writeln('\t$styp arg${i+1};') } g.definitions.writeln('} $wrapper_struct_name;') - g.definitions.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg) {') - g.definitions.write(name + '(') + g.definitions.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg);') + + g.gowrappers.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg) {') + g.gowrappers.write('\t${name}(') if it.is_method { - g.definitions.write('arg->arg0') + g.gowrappers.write('arg->arg0') if it.args.len > 0 { - g.definitions.write(', ') + g.gowrappers.write(', ') } } for i in 0..it.args.len { - g.definitions.write('arg->arg${i+1}') + g.gowrappers.write('arg->arg${i+1}') if i < it.args.len - 1 { - g.definitions.write(', ') + g.gowrappers.write(', ') } } - g.definitions.writeln(');\n return 0; }') + g.gowrappers.writeln(');') + g.gowrappers.writeln('\treturn 0;') + g.gowrappers.writeln('}') g.threaded_fns << name } else{} @@ -2898,4 +2904,3 @@ fn type_to_fmt(typ table.Type) string { } return '%d' } -