├── .editorconfig ├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── examples ├── simple_auto.v ├── simple_handler.v └── simple_manual.v ├── main.v └── v.mod /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | charset = utf-8 3 | end_of_line = lf 4 | insert_final_newline = true 5 | trim_trailing_whitespace = true 6 | 7 | [*.v] 8 | indent_style = tab 9 | indent_size = 4 10 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.v linguist-language=V text=auto eol=lf 2 | *.vv linguist-language=V text=auto eol=lf 3 | *.vsh linguist-language=V text=auto eol=lf 4 | **/v.mod linguist-language=V text=auto eol=lf 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | main 3 | coroutine 4 | *.exe 5 | *.exe~ 6 | *.so 7 | *.dylib 8 | *.dll 9 | vls.log 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 mohamed latifi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vroutine 2 | Vroutine is a coroutine library that allow code execution in "parallel" with the use of simple function 3 | # Platforms 4 | the library is built on top of minicoro which support most of the popular platforms like windows linux macos and android 5 | # Install 6 | `v install mohamedLT.vroutine` 7 | make sure that you install lazalong.minicoro 8 | `v install lazalong.minicoro` 9 | # Why 10 | V does have a very good concurrency model but it only supports native threads which means having a shared variable or a variable that can be accessed from every thread can be challanging 11 | 12 | but with coroutines you dont actually use threads you are still in the main thread but you make code excute and freeze in certain states 13 | 14 | this picture of kotlin coroutines explains it better 15 | 16 | ![image](https://user-images.githubusercontent.com/58748332/192161898-c5264598-749c-4291-8b91-05018ef4bfca.png) 17 | 18 | # Examples 19 | you can find some examples with the library to explain how it works 20 | -------------------------------------------------------------------------------- /examples/simple_auto.v: -------------------------------------------------------------------------------- 1 | import mohamedlt.vroutine 2 | 3 | fn main() { 4 | mut s := vroutine.Scheduler{} 5 | s.add_f(f1, 0) 6 | s.add_f(f2, 0) 7 | s.run() 8 | } 9 | 10 | fn f1() { 11 | for i in 0 .. 100 { 12 | println('f1 $i') 13 | vroutine.yield() 14 | } 15 | } 16 | 17 | fn f2() { 18 | for i in 0 .. 2 { 19 | println('f2 $i') 20 | vroutine.yield() 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /examples/simple_handler.v: -------------------------------------------------------------------------------- 1 | import vroutine 2 | 3 | fn main() { 4 | mut h := vroutine.new(f, voidptr(0)) 5 | h.step() 6 | println('hi') 7 | h.step() 8 | println('hi') 9 | h.step() 10 | println('hi') 11 | h.step() 12 | println('hi') 13 | h.step() 14 | } 15 | 16 | fn f() { 17 | for i in 0 .. 100 { 18 | println(' $i') 19 | vroutine.yield() 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /examples/simple_manual.v: -------------------------------------------------------------------------------- 1 | import mohamedlt.vroutine 2 | 3 | fn main() { 4 | mut s := vroutine.Scheduler{} 5 | s.add_f(f1, 0) 6 | s.add_f(f2, 0) 7 | s.step() 8 | s.step() 9 | s.step() 10 | s.step() 11 | s.step() 12 | } 13 | 14 | fn f1() { 15 | for i in 0 .. 100 { 16 | println('f1 $i') 17 | vroutine.yield() 18 | } 19 | } 20 | 21 | fn f2() { 22 | for i in 0 .. 2 { 23 | println('f2 $i') 24 | vroutine.yield() 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /main.v: -------------------------------------------------------------------------------- 1 | module vroutine 2 | 3 | import lazalong.minicoro 4 | 5 | const time_out = 10 6 | 7 | [inline] 8 | pub fn get_data() &T { 9 | return &T(minicoro.get_user_data(minicoro.running())) 10 | } 11 | 12 | pub struct Scheduler { 13 | mut: 14 | coroutines []Handler 15 | } 16 | 17 | pub fn new(func_ptr voidptr, args voidptr) Handler { 18 | mut desc := minicoro.desc_init(func_ptr, 0) 19 | desc.user_data = args 20 | mut co := &minicoro.Coro{} 21 | minicoro.create(&co, &desc) 22 | return Handler{ 23 | coroutine: co 24 | } 25 | } 26 | 27 | pub fn (mut s Scheduler) add_f(func_ptr voidptr, args voidptr) { 28 | s.coroutines << new(func_ptr, args) 29 | } 30 | 31 | pub fn (mut s Scheduler) add(hand Handler) { 32 | s.coroutines << hand 33 | } 34 | 35 | pub fn (mut s Scheduler) step() { 36 | for i in 0 .. s.coroutines.len { 37 | if s.coroutines[i].step() { 38 | s.coroutines.delete(i) 39 | } 40 | } 41 | } 42 | 43 | pub fn (mut s Scheduler) run() { 44 | for s.coroutines.len > 0 { 45 | s.step() 46 | } 47 | } 48 | 49 | [inline] 50 | pub fn yield() { 51 | minicoro.yield(minicoro.running()) 52 | } 53 | 54 | pub struct Handler { 55 | coroutine minicoro.Coro 56 | time_out int = vroutine.time_out 57 | mut: 58 | repeats int 59 | last_state minicoro.State = .suspended 60 | } 61 | 62 | pub fn (mut h Handler) step() bool { 63 | state := minicoro.status(h.coroutine) 64 | if state == .running && state == h.last_state { 65 | h.repeats++ 66 | } else { 67 | h.repeats = 0 68 | } 69 | h.last_state = state 70 | if state == .dead || h.repeats >= h.time_out { 71 | if h.repeats >= h.time_out { 72 | eprintln('the coroutine has passed the allowed time out ') 73 | } 74 | return true 75 | } else if state == .suspended { 76 | res := minicoro.resume(h.coroutine) 77 | if res != .success { 78 | eprintln('error in coroutine ERROR $res : ${minicoro.result_description(res)} ') 79 | return true 80 | } 81 | } 82 | return false 83 | } 84 | -------------------------------------------------------------------------------- /v.mod: -------------------------------------------------------------------------------- 1 | Module { 2 | name: 'vroutine' 3 | description: '' 4 | version: '' 5 | license: '' 6 | dependencies: [] 7 | } 8 | --------------------------------------------------------------------------------