PB Coroutines Ver. 2 [Crossplatform]

Share your advanced PureBasic knowledge/code with the community.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

PB Coroutines Ver. 2 [Crossplatform]

Post 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()
Fred
Administrator
Administrator
Posts: 18199
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PB Coroutines Ver. 2 [Crossplatform]

Post by Fred »

It looks interesting, what would be the use of those comparing to a thread version ?
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: PB Coroutines Ver. 2 [Crossplatform]

Post 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.
Fred
Administrator
Administrator
Posts: 18199
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PB Coroutines Ver. 2 [Crossplatform]

Post by Fred »

Ok, that makes sense, thanks !
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: PB Coroutines Ver. 2 [Crossplatform]

Post 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...
Regards,
JamiroKwai
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: PB Coroutines Ver. 2 [Crossplatform]

Post 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.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: PB Coroutines Ver. 2 [Crossplatform]

Post 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.
Post Reply