Giter Club home page Giter Club logo

runner-multitasking's Introduction

                                    ======
                                    Runner
                                    ======
                                    
  
  smallest (and fastest?) Cooperative Multitasking ever

  (C) 2019 Dipl. Phys. Helmut Weber

  

  Runner2.c   = Linux-Version     (12.000.000    taskswitches/s) Lenovo W520
  Runner4.ino = ESP32-Version     (   403.000    taskswitches/s)
  Runner4.ino = ARDUINO-Version   (    32.000    taskswitches/s)


  Is it possible to use a cooperative OS as an RTOS ?
 
  What is an RTOS? It garantees a Job to be done just in a predefined time. Same is true for a reaction on an
  Sensor-Action at the outside  - interrupts are inventented for this. 

  If the mouse pointer on the screen reacts on mouse movements without a delay a human can recognize the OS (Windows, Linux)
  is an RTOS in this sense.

  Most RTOS have a ticktime (mostly 1 ms) after which the OS saves the actual task state and starts the task which is READY and
  has the highest priority.

  But changing tasks only every 1ms is not enough. A Task with highest priority, which is allways READY, would suppress any 
  other task.
  For that the YIELD is invented. This allows a task to initiate a taskswitch long before its 1 ms timeslice is over.
  In reality most tasks give up their timeslices with YIELD or DELAY!
  That means, most tasks are cooperative in an RTOS.

  A real RTOS has a MMU to protect Program- an Data-Space of a task! And that is the most important feature of an RTOS.
  If a CPU does not have this feature a cooperative system can be used as an RTOS as well.

  A lot of RTOS tasks have the structure:
    while(1) {
      ...              Some code
      DELAY
      ...
      YIELD
      ...
      DELAY
    }

  My "CoopOS" is build to rebuild such structures!

  But very often you have to do a task from the beginning to the end - running in determined intervals.

  This program RUNNER is built for such systems.
  It is extreme compact (about 100 lines). There are only 3 functions:

    >>>>>>>>>> INITRUN()
    This function is used to build the tasklist.

  
    >>>>>>>>>> RUNNER
      This function is called as often as possible to start tasks which are READY (interval has passed by).
      while(1) {
        RUNNER();
      }
      RUNNER calls the task, which is READY and is the first in the tasklist.
      RUNNER is called (average) every 2.3 µs.
      The longest time gap between 2 calls is measured 20 µs.

    >>>>>>>>>> DELAY()
    Is used in tasks at points, where a delay is possible. Delay calls RUNNER recursively. It is used to give 
    high priority tasks the chance to run. Running (and delayed) tasks are not started again.

    

  The advantages:
  
    - PURE ANSI C (well, without micros() and IO-operations)
    - TRANSPORTABLE SOURCES
    - VERY SIMPLE, BUT VERSATILE
    - NO STACKSWICTH
    - NO SETJMP/LONGJMP
    - VERY FAST TASKSWITCHES
    - TASKSWITCHING "DELAY" IS NOT ONLY USABLE IN TASKS BUT ALSO IN ALL CALLED FUNCTIONS
    - TASKS ARE NORMAL FUNCTIONS WITHOUT CALLING SUCH FUNCTIONS/DEFINES LIKE "BEGIN_TASK/END_TASK"
    - NO TIMERS/INTERRUPTS ARE USED
    - NO LIBRARIES USED
    - ABOUT 100 LINES OF SOURCE CODE
    
  A Task should run complete from start to end - but it may contain as much DELAYS as you want !

  A lot of people think, an ESP32 is mostly used as an interacting device using BLE or WLAN. That may be
  true for most users, but I use it often without these features - just like a superfast Arduino.
  But with the 2 cores of the ESP32 it is possible to use BLE/WLAN together with RUNNER with no introduced delays.

  This program is thought as an example.
  

  Here are the 8 tasks:

    - ID_Fast=      InitRun(Fast,     50-13,   PRI_KERNEL);       // Send 2. DAC value every 38µs (max.: 53µs)
    Marks start of task (digitalWrite) and sends a value to DAC channel 1 to build s sawtooth
    This task is called ever 38µs. The deadline of this task is 55µs which is never reached or exceeded.
      
    
    - ID_Fast2=     InitRun(Fast2,    50-12,   PRI_KERNEL);       // Send DAC value every 38µs (max.: 53µs)
    Like FAST, but using DAC channel2 and build a sine function. It is independent of FAST and the interval time could be
    changed indepentently.
    
        
    - ID_Blink=     InitRun(Blink,    100000,  PRI_USER1);        // Toggle "LED" every 100 ms
    A blinking LED is the "Hello world" of each multitasker ;)
    
    
    - ID_Count=     InitRun(Count,    1000000, PRI_USER1);        // Count and print every second
    Counts and print seconds (and the longest time gap between 2 RUNNER-calls - never reaching or exeeding 20 µs)

    
    - ID_Runs=      InitRun(Runs,     1000000, PRI_USER1);        // Show Taskswitch calls every second
    Shows system-information every second:  (see Output.jpg)
     1)      2)  3) 4) 5)     6)
    438099 25980 53 52 103200 1
        1) number of RUNNER calls per second.    The average is one call every 2.3 µs !!!!!
        2) number of FAST is called per second.  The average is one call every 38 µs !
        3) longest time from call to call of FAST.  53 µs
        4) longest time from call to call of FAST2.  53 µs
        5) number of total Lotto-draws ( 20 per second )
        6) level of recursion of RUNNER
    
    - ID_SerOut=    InitRun(SerOut,   1000,    PRI_USER2);        // Send buffered characters to serial line
    Serial output of strings is a break for all systems. Here the serial output is buffered an the characters
    are sent to the line with an interval of 1 ms. So serial output induces much less system-delay.

    
    - ID_WaitEvent= InitRun(WaitEvent,1000,    PRI_EVENT);        // Wait for an event (1000µs deadtime after an event)
    For an RTOS it is important to react fast on external or internal events.
    A task can set to state WAITEVENT. Another task (or an interrupt) can activate a waiting task.
    Here WAITEVENT is activated from BLINK every 10 seconds.
    The delay from activating(BLINK) to run WAITEVENT is 2-3 µs!
    This is may considered as VERY FAST. 

    
    - ID_Lotto=     InitRun(Lottery,  50000,   PRI_USER2);        // Check Lotto translucent  20 times a second and show results (hits>=4)
    This task should show how to build state machine tasks and longer delays.
    LOTTO draws 6 numbers out of 49. This is the winning combination.
    Then LOTTO draws 20 times 6/49 combinations per second and compares them with the winning combination.
    If 4 or more numbers are correct the combination is print out.

    Here we have 8 tasks are running very deterministic!
    Even 2 tasks running every 38 µs are determinstic in the way, they are never delayed more than 15 µs.
    Once again: No interrupts are needed.

    Using serial output and achieving these results show, how a very simple cooperative system running from AtTiny over
    Arduino, ESP32 to supercomputers can be used as an RTOS. 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.