Difference between revisions of "Lib/sequencer offers Multitasking"
Hzwakenberg (talk | contribs) m |
Hzwakenberg (talk | contribs) m (Lingual corrections) |
||
Line 1: | Line 1: | ||
[[Category: Library]] |
[[Category: Library]] |
||
− | The <tt>lib/sequencer</tt> library implements an event-driven, priority-based, cooperative multitasker. The sequencer library does not start new threads or any other separate processes. Unlike "pure" cooperative multitasking, a task cannot stop at any time (using a hypothetical <tt>yield</tt> feature) but only in known and stable states. The sequencer library does it this way to retain conformance to |
+ | The <tt>lib/sequencer</tt> library implements an event-driven, priority-based, cooperative multitasker. The sequencer library does not start new threads or any other separate processes. Unlike "pure" cooperative multitasking, a task cannot stop at any time (using a hypothetical <tt>yield</tt> feature) but only in known and stable states. The sequencer library does it this way to retain conformance to Eiffel principles. |
− | The |
+ | The principles are: |
* the class [[library_class:LOOP_STACK|<tt>LOOP_STACK</tt>]] manages the multitasking; |
* the class [[library_class:LOOP_STACK|<tt>LOOP_STACK</tt>]] manages the multitasking; |
||
Line 8: | Line 8: | ||
* the class [[library_class:EVENTS_SET|<tt>EVENTS_SET</tt>]] describes the condition(s) in which a task can be executed. |
* the class [[library_class:EVENTS_SET|<tt>EVENTS_SET</tt>]] describes the condition(s) in which a task can be executed. |
||
− | We will successively document each of |
+ | We will successively document each of these concepts: |
== The multitasking manager == |
== The multitasking manager == |
||
Line 17: | Line 17: | ||
Of course, additional tasks can be added during the execution. The manager can also be stopped, thanks to the <tt>break</tt> feature. |
Of course, additional tasks can be added during the execution. The manager can also be stopped, thanks to the <tt>break</tt> feature. |
||
− | Note that the |
+ | Note that the loop stack contains many execution loops ([[library_class:LOOP_ITEM|<tt>LOOP_ITEM</tt>]]). This allows, for instance, the implementation of a kind of ''modality'' (e.g. the modal windows in [[lib/vision|Vision]]). |
== Tasks == |
== Tasks == |
||
Line 23: | Line 23: | ||
A task has a life cycle represented by the features executed by the manager (indeed a [[library_class:LOOP_ITEM|<tt>LOOP_ITEM</tt>]] hence the export clauses of those features). |
A task has a life cycle represented by the features executed by the manager (indeed a [[library_class:LOOP_ITEM|<tt>LOOP_ITEM</tt>]] hence the export clauses of those features). |
||
− | # <tt>prepare</tt> allows |
+ | # <tt>prepare</tt> allows preparing the task; in this phase, the task sets the events upon which it wants to be activated. The [[library_class:EVENTS_SET|<tt>EVENTS_SET</tt>]] object is an object upon which one can set conditions (thanks to its <tt>expect</tt> feature). |
− | # <tt>is_ready</tt> allows to test if the task has really been activated.The [[library_class:EVENTS_SET|<tt>EVENTS_SET</tt>]] object is an object upon which one can test conditions (thanks to its <tt>event_occurred</tt> feature). |
+ | # <tt>is_ready</tt> allows to test if the task has really been activated. The [[library_class:EVENTS_SET|<tt>EVENTS_SET</tt>]] object is an object upon which one can test conditions (thanks to its <tt>event_occurred</tt> feature). |
− | # <tt>continue</tt> contains the execution body of the task, and is executed |
+ | # <tt>continue</tt> contains the execution body of the task, and is executed upon task activation. |
# <tt>done</tt> tells if the task has finished its execution. If so, it will be removed from the execution loop. Otherwise, the cycle begins again. |
# <tt>done</tt> tells if the task has finished its execution. If so, it will be removed from the execution loop. Otherwise, the cycle begins again. |
||
− | # <tt>restart</tt> allows |
+ | # <tt>restart</tt> allows reinserting a task in the execution loop. |
The <tt>restart</tt> feature is seldom useful. See [[lib/vision|Vision]] for some use cases. |
The <tt>restart</tt> feature is seldom useful. See [[lib/vision|Vision]] for some use cases. |
||
Line 57: | Line 57: | ||
== Priorities == |
== Priorities == |
||
− | Tasks |
+ | Tasks execution is by priority order. Only the ready task with the ''lowest'' priority is executed. All other tasks, even if ready, will have to wait until no other task with a lower priority is ready. |
-note: we need to document how many priority levels <br> are supported and how the scheduling works when multiple or all tasks were given<br> an identical priority level. Would the scheduler then resort to some sort<br> of round-robin method? By what rules? Apparently '''not''' by evaluating the duration of the<br> wait for next execution, see the '''LOOP_ITEM.run''' method. /HZ |
-note: we need to document how many priority levels <br> are supported and how the scheduling works when multiple or all tasks were given<br> an identical priority level. Would the scheduler then resort to some sort<br> of round-robin method? By what rules? Apparently '''not''' by evaluating the duration of the<br> wait for next execution, see the '''LOOP_ITEM.run''' method. /HZ |
||
Line 63: | Line 63: | ||
== Libraries using the sequencer == |
== Libraries using the sequencer == |
||
− | * [[lib/net]]: servers usually allow |
+ | * [[lib/net]]: servers usually allow many simultaneous connections. This is implemented with the sequencer library. |
* [[lib/vision]]: the event manager loop is implemented with a sequencer. |
* [[lib/vision]]: the event manager loop is implemented with a sequencer. |
Revision as of 12:24, 1 January 2022
The lib/sequencer library implements an event-driven, priority-based, cooperative multitasker. The sequencer library does not start new threads or any other separate processes. Unlike "pure" cooperative multitasking, a task cannot stop at any time (using a hypothetical yield feature) but only in known and stable states. The sequencer library does it this way to retain conformance to Eiffel principles.
The principles are:
- the class LOOP_STACK manages the multitasking;
- the class JOB represents a task. The task core is the continue feature, during the execution of which the task controls the whole system;
- the class EVENTS_SET describes the condition(s) in which a task can be executed.
We will successively document each of these concepts:
The multitasking manager
The class LOOP_STACK is in charge of managing the multitasking. It is used in two steps:
- initialization: creation of the LOOP_STACK object and adding the task(s) to be executed
- execution: execution of the run feature
Of course, additional tasks can be added during the execution. The manager can also be stopped, thanks to the break feature.
Note that the loop stack contains many execution loops (LOOP_ITEM). This allows, for instance, the implementation of a kind of modality (e.g. the modal windows in Vision).
Tasks
A task has a life cycle represented by the features executed by the manager (indeed a LOOP_ITEM hence the export clauses of those features).
- prepare allows preparing the task; in this phase, the task sets the events upon which it wants to be activated. The EVENTS_SET object is an object upon which one can set conditions (thanks to its expect feature).
- is_ready allows to test if the task has really been activated. The EVENTS_SET object is an object upon which one can test conditions (thanks to its event_occurred feature).
- continue contains the execution body of the task, and is executed upon task activation.
- done tells if the task has finished its execution. If so, it will be removed from the execution loop. Otherwise, the cycle begins again.
- restart allows reinserting a task in the execution loop.
The restart feature is seldom useful. See Vision for some use cases.
Execution conditions
The class EVENTS_SET allows to set and test conditions (EVENT_DESCRIPTORs).
The possible conditions are:
- Time: this allows to create periodic tasks. See the classes PERIODIC_JOB, BACKGROUND_JOB, and TIME_EVENTS;
- Input-output:
- Data on an input stream: this allows to build tasks that wait for input data to be available (feature INPUT_STREAM.event_can_read),
- Data on an output stream: this allows to build tasks that wait until they can write on an output stream (feature OUTPUT_STREAM.event_can_write);
- Network: this allows to build tasks waiting network connections (feature SOCKET_SERVER.event_connection).
Tasks scheduling
The manager implements the following cycle:
- call to the feature EVENTS_SET.reset;
- call to the feature JOB.prepare on each task;
- call to the feature EVENTS_SET.wait to wait for at least one condition to happen;
- call to the feature JOB.is_ready on each task;
- for all the ready tasks, call to the feature JOB.continue;
- for those tasks, call to the feature JOB.done and if relevant, remove the task;
- and back to the beginning.
Priorities
Tasks execution is by priority order. Only the ready task with the lowest priority is executed. All other tasks, even if ready, will have to wait until no other task with a lower priority is ready.
-note: we need to document how many priority levels
are supported and how the scheduling works when multiple or all tasks were given
an identical priority level. Would the scheduler then resort to some sort
of round-robin method? By what rules? Apparently not by evaluating the duration of the
wait for next execution, see the LOOP_ITEM.run method. /HZ
Libraries using the sequencer
- lib/net: servers usually allow many simultaneous connections. This is implemented with the sequencer library.
- lib/vision: the event manager loop is implemented with a sequencer.