cforth icon indicating copy to clipboard operation
cforth copied to clipboard

multitasking/cororoutines

Open andrewtholt opened this issue 4 years ago • 12 comments

Hi Mitch,

I seem to recall asking you this question a very long time ago, when I was working at Sun, certainly last century.

Did you ever implement a co-operative task system for cforth ?

I see that pause is a deferred word, IIRC it was in openfirmware too.

Thanks

andrewtholt avatar Jan 26 '21 16:01 andrewtholt

Yes, I have used cooperative MT with an active pause. I don't use it often, but all of my Forth systems are set up so pause can do the right thing.

MitchBradley avatar Jan 26 '21 19:01 MitchBradley

Hi Mitch,

Is there any way I can make use of this ?

On Tue, 26 Jan 2021 at 19:12, Mitch Bradley [email protected] wrote:

Yes, I have used cooperative MT with an active pause. I don't use it often, but all of my Forth systems are set up so pause can do the right thing.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MitchBradley/cforth/issues/73#issuecomment-767765005, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAYVWSK3LWY73CCF2RIN7XLS34HYVANCNFSM4WTWD4NA .

andrewtholt avatar Jan 27 '21 08:01 andrewtholt

I updated the multitasking code for CForth. Pull the latest commit and look at src/lib/test-tasking.fth

MitchBradley avatar Feb 07 '21 22:02 MitchBradley

Thanks for that. I'll take a look ASAP.

Regards, Andrew

On Sun, 7 Feb 2021 at 22:37, Mitch Bradley [email protected] wrote:

I updated the multitasking code for CForth. Pull the latest commit and look at src/lib/test-tasking.fth

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MitchBradley/cforth/issues/73#issuecomment-774780923, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAYVWSLK62QL3P6RJOIOI2TS54I3TANCNFSM4WTWD4NA .

andrewtholt avatar Feb 08 '21 09:02 andrewtholt

That sounds great. Unfortunately, I am not able to compile tasking.fth under esp8266-rtos the words asleep (pause and link are missing. It would be nice if it is also possible to use tasking.fth with esp8266-rtos!

Kind regards, Jos

Jos-Ven avatar Feb 08 '21 11:02 Jos-Ven

A quick scan would suggest that you replace (pause with (pause)

Haven't actually tried it yet, that's from observation.

andrewtholt avatar Feb 08 '21 14:02 andrewtholt

Hi Andrew,

Thanks for the quick scan. Can't replace (pause with (pause) since (pause) is not defined under esp8266-rtos.

Kind regards, Jos

Jos-Ven avatar Feb 08 '21 15:02 Jos-Ven

asleep, (pause, and link are defined in forth.c according to the part of the commit shown below. You will need to recompile to get them.

image

MitchBradley avatar Feb 08 '21 17:02 MitchBradley

(pause is the correct name.

MitchBradley avatar Feb 08 '21 18:02 MitchBradley

Hi Mitch, Sorry, that did not work. Here is what I did: If I clone the main branch then after: cd /home/pi/cf/cforth/build/esp8266-rtos bash: cd: /home/pi/cf/cforth/build/esp8266-rtos: No such file or directory

The esp8266-rtos directory seems only to be present in the branch WIP and not in the branch MAIN After: git clone https://github.com/MitchBradley/cforth --branch WIP cd /home/pi/cf/cforth/build/esp8266-rtos make make flash

\ Then in cforth I saw: sifting pause In vocabulary forth (40285388) pause ok see pause defer pause is : noop

; Kind regards, Jos

Jos-Ven avatar Feb 09 '21 00:02 Jos-Ven

Hi Mitch,

After I made the changes manually in forth.c for the words asleep (pause and link Then it was possible to compile and run test-tasking.fth The counter was indeed increased when the background was running. To be sure multiple tasks could also be handled I added 1 more task. Here is the code:

\needs multi fl tasking.fth

\ Tests and examples for cooperative multitasking

\ Global variable used by the test tasks Global counts0 Global counts1 Global counts2

\ Explicit creation of word and task

: do-count0 begin 1 counts0 +! pause again ; task: count-task0 ' do-count0 count-task0 fork count-task0 wake

: do-count1 begin 2 counts1 +! pause again ; task: count-task1 ' do-count1 count-task1 fork count-task1 wake

\ Combined creation of task with word to execute \ background counters3 begin 3 counts0 +! 2 counts1 +! 1 counts2 +! pause again ;

background counters2 begin pause again ;

: .counts ( -- ) ." counts = " counts0 @ .d counts1 @ .d counts2 @ .d cr ;

.counts pause .counts pause .counts

: run-background ( -- ) begin pause key? until key drop ;

.( Type a key to return to prompt) cr run-background .counts \ end code

Output when counters3 is active: counts = 0 0 0 counts = 3 4 1 counts = 6 8 2 Type a key to return to prompt counts = 1332300 1776400 444100

Output when counters2 is active: ok fl test-tasking.fth counts = 0 0 0 counts = 0 2 0 counts = 0 4 0 Type a key to return to prompt counts = 0 656194 0

\ Manual changing: ok multi ok pause .counts counts = 0 656200 0 ok pause .counts counts = 0 656202 0 ok pause .counts counts = 0 656204 0

It seems pause does not run all tasks Here is the link to the changed forth.c: https://github.com/Jos-Ven/cforth/commit/f13c871bc969efaba1fd847017e2e37ca40329a1 I hope the problem can be solved.

Thanks in advance, kind regards, Jos

Jos-Ven avatar Feb 09 '21 14:02 Jos-Ven

Hi, For those who are interested: I just uploaded some files for pre-emptive multitasking.

See: https://github.com/Jos-Ven/cforth/tree/WIP/src/app/esp8266-rtos/ Added and changed files: app.fth tasking_rtos.fth extend.c interface.h

and: https://github.com/Jos-Ven/cforth/tree/WIP/src/app/esp8266-rtos/tests Added for a test case: pe_tasking.fth

It is all quite experimental. I could not get it better due to my lack of knowledge of C. So far only successfully tested on an ESP-12F Improvements are welcome!

Kind regards Jos

Jos-Ven avatar Mar 09 '21 15:03 Jos-Ven