1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fix cmd/tools/vtest.v build

This PR enables building them again with v2, by making vlib/sync/pool.v
single threaded for now, and by removing the use of generics till they
are fixed in v2.
This commit is contained in:
Delyan Angelov
2020-04-02 16:52:23 +03:00
committed by GitHub
parent 4ada412a05
commit ad9848d983
2 changed files with 89 additions and 38 deletions

View File

@@ -98,9 +98,10 @@ pub fn (pool mut PoolProcessor) set_max_jobs(njobs int) {
// by the number of available cores on the system.
// work_on_items returns *after* all threads finish.
// You can optionally call get_results after that.
pub fn (pool mut PoolProcessor) work_on_items<T>(items []T) {
pool.work_on_pointers( items.pointers() )
}
// TODO: uncomment, when generics work again
//pub fn (pool mut PoolProcessor) work_on_items<T>(items []T) {
// pool.work_on_pointers( items.pointers() )
//}
pub fn (pool mut PoolProcessor) work_on_pointers(items []voidptr) {
mut njobs := runtime.nr_jobs()
@@ -115,7 +116,9 @@ 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++ {
go process_in_thread(pool,i)
// TODO: uncomment when `go func()` works again with v2
// go process_in_thread(pool,i)
process_in_thread(pool,i)
}
pool.waitgroup.wait()
}
@@ -145,36 +148,44 @@ fn process_in_thread(pool mut PoolProcessor, task_id int) {
// get_item - called by the worker callback.
// Retrieves a type safe instance of the currently processed item
pub fn (pool &PoolProcessor) get_item<T>(idx int) T {
return *(&T(pool.items[idx]))
}
// TODO: uncomment, when generics work again
//pub fn (pool &PoolProcessor) get_item<T>(idx int) T {
// return *(&T(pool.items[idx]))
//}
// TODO: the below is a hack, remove it when v2 &string() casting works again
type mystring string
// get_string_item - called by the worker callback.
// It does not use generics so it does not mess up vfmt.
// TODO: remove the need for this when vfmt becomes smarter.
pub fn (pool &PoolProcessor) get_string_item(idx int) string {
return *(&string(pool.items[idx]))
// return *(&string(pool.items[idx]))
// TODO: the below is a hack, remove it when v2 casting works again
return &mystring( pool.items[idx] )
}
// get_int_item - called by the worker callback.
// It does not use generics so it does not mess up vfmt.
// TODO: remove the need for this when vfmt becomes smarter.
pub fn (pool &PoolProcessor) get_int_item(idx int) int {
return *(&int(pool.items[idx]))
item := pool.items[idx]
return *(&int(item))
}
pub fn (pool &PoolProcessor) get_result<T>(idx int) T {
return *(&T(pool.results[idx]))
}
// TODO: uncomment, when generics work again
//pub fn (pool &PoolProcessor) get_result<T>(idx int) T {
// return *(&T(pool.results[idx]))
//}
// TODO: uncomment, when generics work again
// get_results - can be called to get a list of type safe results.
pub fn (pool &PoolProcessor) get_results<T>() []T {
mut res := []T
for i in 0 .. pool.results.len {
res << *(&T(pool.results[i]))
}
return res
}
//pub fn (pool &PoolProcessor) get_results<T>() []T {
// mut res := []T
// for i in 0 .. pool.results.len {
// res << *(&T(pool.results[i]))
// }
// return res
//}
// set_shared_context - can be called during the setup so that you can
// provide a context that is shared between all worker threads, like
@@ -203,3 +214,38 @@ pub fn (pool mut PoolProcessor) set_thread_context(idx int, context voidptr) {
pub fn (pool &PoolProcessor) get_thread_context(idx int) voidptr {
return pool.thread_contexts[idx]
}
// TODO: remove everything below this line after generics are fixed:
pub struct SResult {
pub:
s string
}
pub struct IResult {
pub:
i int
}
//
pub fn (pool mut PoolProcessor) work_on_items_s(items []string) {
pool.work_on_pointers( items.pointers() )
}
pub fn (pool mut PoolProcessor) work_on_items_i(items []int) {
pool.work_on_pointers( items.pointers() )
}
pub fn (pool &PoolProcessor) get_results_s() []SResult {
mut res := []SResult
for i in 0 .. pool.results.len {
res << *(&SResult(pool.results[i]))
}
return res
}
pub fn (pool &PoolProcessor) get_results_i() []IResult {
mut res := []IResult
for i in 0 .. pool.results.len {
res << *(&IResult(pool.results[i]))
}
return res
}