home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / TASK.ZIP / TASK.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-27  |  2.6 KB  |  87 lines

  1. // TASK.HPP   Copyright 1989 by Dlugosz Software
  2. // from multitasking classes
  3.  
  4. class task;  //forward reference
  5.  
  6. class task_head {  // allow tasks to be placed in lists
  7.    task_head* prev;
  8.    task_head* next;  // a two-way linked list
  9. public:
  10.    friend class task_iterator;
  11.    void add_to_head (task *);   //place at beginning of list
  12.    void add_to_end (task *);    //place at end of list
  13.    void unlink();  //remove from its list
  14.    void zero();  //initialize to empty
  15.    task* fetch_and_remove();
  16.    };
  17.  
  18. class task_list : public task_head {
  19.    // add a constructor and destructor destructor to manage a list of tasks
  20.    void unlink() {}
  21. public:
  22.    task_list() { zero(); }
  23.    ~task_list();
  24.    bool resume_one();
  25.    };
  26.  
  27. class task_iterator {   //scan through a task_list
  28.    task_list* start;
  29.    task_head* current;
  30.    inline task* result();
  31. public:
  32.    task_iterator (task_list& l) {current= start= &l;}
  33.    ~task_iterator() {}
  34.    task* next();  // next() and prev() return NULL when the list is done
  35.    task* prev();
  36.    task* same();  // don't move, but fetch the same one again
  37.    };
  38.  
  39.  
  40. typedef void (*starter_function) (void *);
  41.  
  42. class task : public task_head {
  43.    friend void scheduler();
  44.    friend void fall_through();
  45.    friend void preemptable (bool);
  46.    void far* stack;  //all other values on stack
  47.    int priority_level;
  48.    enum {preemptive=1, killable=2, killed=4, blocked=8, faulted=16 }  flags;
  49. public:
  50.    void kill();
  51.    bool iskilled() { return flags&killed; }
  52.    bool isblocked() { return flags&blocked; }
  53.    bool isfaulted() { return flags&faulted; }
  54.    task (starter_function f, int priority, void* stack, int stacksize, void* arg= NULL);
  55.    ~task() { kill(); }
  56.    //not generally used directly, but used by other classes in the system
  57.    void block (task_list&);
  58.    void unblock (bool error= FALSE);
  59.    void add_to_list (task_list& l) { l.add_to_end(this); }
  60.    };
  61.  
  62.  
  63. extern void task_yield();
  64. extern task* active_task;
  65.  
  66. extern void scheduler();  //call to enter multitasking mode
  67.  
  68. // do NOT call these functions yourself.
  69. // There is no way to prevent entering the names into global scope if I
  70. // want the class below to generate inline code.
  71. extern void preempt_off();
  72. extern void preempt_on();
  73.  
  74.  
  75. // a dummy class for disableing preemption
  76. // this makes sure preempt state cannot be messed up with
  77. // mismatched off/on calls
  78. struct race_condition_cure {
  79.    race_condition_cure() {
  80.       preempt_off();
  81.       }
  82.    ~race_condition_cure() {
  83.       preempt_on();
  84.       // >>should check for slice overrun
  85.       }
  86.    };
  87.