home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / devel5 / tasks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-21  |  2.2 KB  |  88 lines

  1. /* Very simple, non-preemptive, multitasking system
  2.    with round-robin scheduling */
  3.  
  4. /* Written by Bernie Roehl, June 1992 */
  5.  
  6. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  7.    May be freely used to write software for release into the public domain;
  8.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  9.    for permission to incorporate any part of this software into their
  10.    products!
  11.  
  12.      ATTRIBUTION:  If you use any part of this source code or the libraries
  13.      in your projects, you must give attribution to REND386, Dave Stampe,
  14.      and Bernie Roehl in your documentation, source code, and at startup
  15.      of your program.  Let's keep the freeware ball rolling!
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <alloc.h>  /* malloc() */
  20. #include "rend386.h"  /* for current_time() */
  21.  
  22. typedef struct _task TASK;
  23.  
  24. struct _task {
  25.     void (*fn)(int cmd, char *msg, long now, long period);
  26.     void *data;
  27.     long period, lastran, wakeup;
  28.     TASK *next;
  29. };
  30.  
  31. static TASK *current_task = NULL;
  32.  
  33. TASK *get_current_task()
  34. {
  35.     return current_task;
  36. }
  37.  
  38. TASK *add_task(TASK **tasklist,
  39.     void (*fn)(int cmd, char *init, long now, long period),
  40.     long period, void *param)
  41. {
  42.     TASK *t;
  43.     if ((t = malloc(sizeof(TASK))) == NULL) return NULL;
  44.     t->fn = fn; 
  45.     t->data = NULL; 
  46.     t->period = period;
  47.     t->lastran = 0L; 
  48.     t->wakeup = current_time();
  49.     t->next = *tasklist;
  50.     current_task = *tasklist = t;
  51.     if (fn) (*fn)(0, param, current_time(), period); /* initialize */
  52.     return t;
  53. }
  54.  
  55. void del_task(TASK **tasklist, TASK *tsk)
  56. {
  57.     TASK *t;
  58.     if (tsk == NULL) return;
  59.     if (tsk == *tasklist)
  60.         *tasklist = tsk->next;
  61.     else
  62.         for (t = *tasklist; t; t = t->next)
  63.         if (t->next == tsk) {
  64.             t->next = tsk->next;
  65.             break;
  66.         }
  67.     free(tsk);
  68. }
  69.  
  70. void *find_task_data(TASK *task)
  71. {
  72.     return &task->data;
  73. }
  74.  
  75. void run_tasks(TASK *tasklist)
  76. {
  77.     long now;
  78.     now = current_time();
  79.     for (current_task = tasklist; current_task; current_task = current_task->next) {
  80.         if (current_task->fn && (now >= current_task->wakeup)) {
  81.             current_task->wakeup = now + current_task->period;
  82.             current_task->fn(1, NULL, now, current_task->period);
  83.             current_task->lastran = now;
  84.         }
  85.     }
  86. }
  87.  
  88.