Multiplatform coroutines in C and also in FreeBasic

For other topics related to the FreeBASIC project or its community.
angros47
Posts: 1614
Joined: Jun 21, 2005 19:04

Multiplatform coroutines in C and also in FreeBasic

Postby angros47 » Mar 28, 2020 1:00

I found this:
https://byuu.org/projects/libco

I guess it can be ported to FreeBasic. Or perhaps it can be integrated in the runtime and compiler? (in the past I remember someone wanted to add coroutine support in FB)
angros47
Posts: 1614
Joined: Jun 21, 2005 19:04

Re: Multiplatform coroutines in C and also in FreeBasic

Postby angros47 » Mar 28, 2020 14:53

Ok, it works in FreeBasic, too.
First of all, you need to download the files, and compile the library with:

Code: Select all

gcc -c libco.c


You should get the file libco.o (it can be done in DOS, in Windows and in Linux)

Then, the header for FreeBasic (that must be saved as "libco.bi") is:

Code: Select all

'  libco v20 (2019-10-16)
'  author: byuu
'  license: ISC

declare function co_active cdecl alias "co_active" () as any ptr
declare function co_derive cdecl alias "co_derive" (p as any ptr, b as unsigned integer, s as sub) as any ptr
declare function co_create cdecl alias "co_create" (b as unsigned integer, s as sub) as any ptr
declare sub co_delete cdecl alias "co_delete" (t as any ptr)
declare sub co_switch cdecl alias "co_switch" (t as any ptr)
declare function co_serializable cdecl alias "co_serializable" as integer



And now, to use it in FreeBasic:

Code: Select all

#include "libco.bi"

dim shared as any ptr t0,t1

sub task
   dim z as integer
   do
      z+=1
      print "this is the thread, iteration ";z
      co_switch(t0)
   loop

end sub




t0=co_active()
t1=co_create(65536,@task)


for i as integer=1 to 50
   print "this is main"
   co_switch(t1)
next


The "do..loop" cycle in the subroutine "task" and the "for...next" loop in the main section of the program will be executed at the same time, although it will happen in a single thread, with no need to set mutex or semaphores, and each iteration of one cycle will match with an iteration of the other one (the loop in "task" will do exactly 50 iterations)
dodicat
Posts: 6363
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Multiplatform coroutines in C and also in FreeBasic

Postby dodicat » Mar 28, 2020 15:57

I tested this angros47

Code: Select all

this is main
this is the thread, iteration  1
this is main
this is the thread, iteration  2
this is main
this is the thread, iteration  3
this is main
this is the thread, iteration  4
this is main
this is the thread, iteration  5 
. . .
. . .


Since I have 32 bit minGW on path I just made the .a file from the .o file.
Thank you.
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Multiplatform coroutines in C and also in FreeBasic

Postby paul doe » Mar 28, 2020 15:59

Mmm, this could prove tremendously useful, thanks. I guess that it can also be built-in in the dialect itself (fork/yield/join?)
angros47
Posts: 1614
Joined: Jun 21, 2005 19:04

Re: Multiplatform coroutines in C and also in FreeBasic

Postby angros47 » Mar 28, 2020 16:17

I would like if it could be integrated in the language. I can already see an use for OpenGL programs (since OpenGL is not thread safe, and it can be accessed only by the thread that created the context). The keyword "fork" should not be used for that, anyway, since it would create confusion with the "fork" instruction that on Linux (and in general on Posix systems) creates a new process
Tourist Trap
Posts: 2844
Joined: Jun 02, 2015 16:24

Re: Multiplatform coroutines in C and also in FreeBasic

Postby Tourist Trap » Mar 28, 2020 17:03

Just for those who may want to test this, more precision:
The thing may have to be compiled this way in a batch file:

Code: Select all

@echo on

"C:\Program Files (x86)\FreeBasic\Freebasic7\FreeBASIC-1.07.1-win64/fbc" libco.o test.bas

test.exe

pause

No need of any include here. Just put every declaration at top:

Code: Select all

'  libco v20 (2019-10-16)
'  author: byuu
'  license: ISC
declare function co_active cdecl alias "co_active" () as any ptr
declare function co_derive cdecl alias "co_derive" (p as any ptr, b as unsigned integer, s as sub) as any ptr
declare function co_create cdecl alias "co_create" (b as unsigned integer, s as sub) as any ptr
declare sub co_delete cdecl alias "co_delete" (t as any ptr)
declare sub co_switch cdecl alias "co_switch" (t as any ptr)
declare function co_serializable cdecl alias "co_serializable" as integer

dim shared as any ptr t0,t1
sub task
   dim z as integer
   do
      z+=1
      print "this is the thread, iteration ";z
      co_switch(t0)
   loop
end sub
t0=co_active()
t1=co_create(65536,@task)

for i as integer=1 to 50
   print "this is main"
   co_switch(t1)
next


To find GCC, I have one in my Octave installation, so I guess everyone has one somewhere in its system, in order to make the .o file.
dodicat
Posts: 6363
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Multiplatform coroutines in C and also in FreeBasic

Postby dodicat » Mar 28, 2020 19:02

You can get a minGW from the NEWS section (the latest fb compilers).
Just unpack it and set the bin folder on the system path.
You have gcc and g++ (C++) compilers, Fortran, gendef (for dll work), nm to look into libraries and many other handy things.

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests