home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / tasks.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  4KB  |  126 lines

  1. /* Very simple, non-preemptive, "multitasking" system
  2.    with round-robin scheduling */
  3.  
  4. /* Originally Written by Bernie Roehl, June 1992 */
  5.  
  6. // PORTED TO vr-386 api BY dAVE sTAMPE, 9/1/94
  7.  
  8. // This code is old now: animation support will be much better
  9. // usingg STATMACH.C and new functions in the pipeline
  10.  
  11. // actually does a simple, non-speed-related update betweeen
  12. // frame draws.  All new routines will be speed-independant
  13.  
  14. // OLD OLD CODE: NOT REALLY UPDATED FOR API.
  15. // USE AT OWN RISK!  TASKS WILL CHANGE IN FUTURE API RELEASES!
  16.  
  17.  
  18. /*
  19.  This code is part of the VR-386 project, created by Dave Stampe.
  20.  VR-386 is a desendent of REND386, created by Dave Stampe and
  21.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  22.  Stampre for VR-386.
  23.  
  24.  Copyright (c) 1994 by Dave Stampe:
  25.  May be freely used to write software for release into the public domain
  26.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  27.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  28.  this software or source code into their products!  Usually there is no
  29.  charge for under 50-100 items for low-cost or shareware products, and terms
  30.  are reasonable.  Any royalties are used for development, so equipment is
  31.  often acceptable payment.
  32.  
  33.  ATTRIBUTION:  If you use any part of this source code or the libraries
  34.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  35.  and any other authors in your documentation, source code, and at startup
  36.  of your program.  Let's keep the freeware ball rolling!
  37.  
  38.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  39.  REND386, improving programmer access by rewriting the code and supplying
  40.  a standard API.  If you write improvements, add new functions rather
  41.  than rewriting current functions.  This will make it possible to
  42.  include you improved code in the next API release.  YOU can help advance
  43.  VR-386.  Comments on the API are welcome.
  44.  
  45.  CONTACT: dstampe@psych.toronto.edu
  46. */
  47.  
  48.  
  49.  
  50. #include <stdio.h>
  51. #include <alloc.h>  /* malloc() */
  52.  
  53. #include "vr_api.h"
  54. #include "segment.h"
  55. #include "wparse.h"
  56.  
  57. typedef struct _task TASK;
  58.  
  59. struct _task {
  60.     void (*fn)(int cmd, char *msg, long now, long period);
  61.     void *data;
  62.     long period, lastran, wakeup;
  63.     TASK *next;
  64. };
  65.  
  66. static TASK *current_task = NULL;
  67.  
  68. TASK *get_current_task()
  69. {
  70.   return current_task;
  71. }
  72.  
  73. TASK *add_task(TASK **tasklist,
  74.     void (*fn)(int cmd, char *init, long now, long period),
  75.     long period, void *param)
  76. {
  77.   TASK *t;
  78.   if ((t = malloc(sizeof(TASK))) == NULL) return NULL;
  79.   t->fn = fn;
  80.   t->data = NULL;
  81.   t->period = period;
  82.   t->lastran = 0L;
  83.   t->wakeup = current_time();
  84.   t->next = *tasklist;
  85.   current_task = *tasklist = t;
  86.   if (fn) (*fn)(0, param, current_time(), period); /* initialize */
  87.   return t;
  88. }
  89.  
  90. void del_task(TASK **tasklist, TASK *tsk)
  91. {
  92.   TASK *t;
  93.   if (tsk == NULL) return;
  94.   if (tsk == *tasklist)
  95.     *tasklist = tsk->next;
  96.   else
  97.     for (t = *tasklist; t; t = t->next)
  98.       if (t->next == tsk)
  99.         {
  100.           t->next = tsk->next;
  101.           break;
  102.         }
  103.     free(tsk);
  104. }
  105.  
  106. void *find_task_data(TASK *task)
  107. {
  108.   return &task->data;
  109. }
  110.  
  111. void run_tasks(TASK *tasklist)
  112. {
  113.   long now;
  114.   now = current_time();
  115.   for (current_task = tasklist; current_task; current_task = current_task->next)
  116.     {
  117.       if (current_task->fn && (now >= current_task->wakeup))
  118.     {
  119.       current_task->wakeup = now + current_task->period;
  120.       current_task->fn(1, NULL, now, current_task->period);
  121.       current_task->lastran = now;
  122.     }
  123.     }
  124. }
  125.  
  126.