Page 1 of 1

PB Coroutines Ver. 2 [Crossplatform]

Posted: Thu Apr 17, 2025 3:56 pm
by Justin
I did a rewrite of my previous coroutine system (resumable functions):
https://www.purebasic.fr/english/viewto ... es#p635874
Now is cleaner and supports MacOS but only tested on Monterrey on a VM, it may not work on newer systems, testing will be appreciated.
It uses fibers on windows, libaco on linux and ucontext on macos.
Project at github:
https://github.com/omegakode/PBCoroutines
I changed a lot of things, if you need the old version make a backup first.
This is the example code:

Code: Select all

EnableExplicit

XIncludeFile "PBCoroutines.pb"

Procedure.l my_task1(co.i) 
	Debug "Task1: Step 1 "
	Debug co_get_arg(co)
	
	co_yield(co)
	Debug "Task1: Step 2 End"
EndProcedure

Procedure.l my_task2(co.i) 
	Debug "Task2: Step 1 "
	
	co_yield(co)
	Debug "Task2: Step 2 End"
EndProcedure

Procedure main()
	Protected.i co, co2
	
	co = co_create(@my_task1(), 10)
	co2 = co_create(@my_task2(), 20)

	Debug "Main: Resume Task1"
	co_resume(co)
	Debug "Main: Back from Task1"
	Debug "Main: Resume Task2"
	co_resume(co2)
	Debug "Main: Back from Task2"
	Debug "Main: Resume Task1"
	co_resume(co)
	Debug "Main: Back from Task1"
	Debug "Main: Resume Task2"
	co_resume(co2)

	co_destroy(co)
	co_destroy(co2)
EndProcedure

main()

Re: PB Coroutines Ver. 2 [Crossplatform]

Posted: Fri Apr 18, 2025 12:57 pm
by Fred
It looks interesting, what would be the use of those comparing to a thread version ?

Re: PB Coroutines Ver. 2 [Crossplatform]

Posted: Fri Apr 18, 2025 8:23 pm
by Justin
Fibers are more lightweight and faster when switching, and since only one fiber runs at a time per thread they don't need synchronization like threads. It's task switching vs thread parallel execution.
In one of my projects threads won't work because all the code needed to be executed on the same thread so that's why i did it.

Re: PB Coroutines Ver. 2 [Crossplatform]

Posted: Fri Apr 18, 2025 9:55 pm
by Fred
Ok, that makes sense, thanks !

Re: PB Coroutines Ver. 2 [Crossplatform]

Posted: Fri Apr 18, 2025 10:17 pm
by jamirokwai
Hi there,

tried your code with MacOS 15.4.1 with an M2 Max.
It crashed after debug-output "Main: Resume Task1" in line 35 of PBCoroutines.pb

Code: Select all

CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
	Procedure.l _co_trampoline(low.l, high.l)
		Protected.co_coroutine_t *co
				
		*co = (high << 32) | low
		*co\func(*co) ; <-- here
		*co\finished = 1
		setcontext(*co\caller) ;Return to caller
	EndProcedure
CompilerEndIf
Do your routines support arm64? Or is it Intel only. I saw some code in your routines about x86, but am not familiar with it...

Re: PB Coroutines Ver. 2 [Crossplatform]

Posted: Sat Apr 19, 2025 3:56 pm
by Justin
Hi,
the fact that reaches the trampoline function is a good sign, i changed some things in macos, can you test it?

I also added some new parameters in co_create, a coroutine cleanup callback in the context of a window application, only for win/linux for now i''ll add it to macos once the basic things work.

Re: PB Coroutines Ver. 2 [Crossplatform]

Posted: Mon Apr 21, 2025 9:24 am
by Justin
Added the destroy callback on MacOs, when you supply a windowid and a callback in co_create(), the callback will be called when the coroutine has finished, so you can clean up stuff or destroy the coroutine.