/* task.h:
 * This program package constitutes the next to lowest of the five modules of
 * the Pads hierarchy.  Pads provides Parallel And Distributed Support for
 * application programs, as externally specified in the document "PADS, an
 * infrastructure for parallel and distributed programs: Users' Manual",
 * which should be read first.  This module provides tasking within a process.
 * The current implementation does not offer true parallelism: only one task
 * executes at a time.  What it does offer is concurrency, namely freedom
 * from the first in, last out discipline of a single stack.
 */
#if !defined(task_h)
/* Prior inclusion of stack.h is necessary. */

#if !defined(Null)
    typedef unsigned char Byte;		/* byte type */
    typedef Byte *Ptr;			/* generic pointer type */
#   define Null ((Ptr) 0)		/* null value */
#else /* !defined(Null) */
#endif /* !defined(Null) */

/* Task identifier, a pointer to a structure holding its state.
 */
typedef struct taskdesc_ {
    Stack stk;				/* stack id */
    void (*proc)();			/* main task procedure ptr */
    Ptr varb;				/* task variable */
    unsigned long alarm;		/* alarm (wakeup) time */
    struct taskdesc_ *fp;		/* forward circ task list ptr */
    struct taskdesc_ *bp;		/* backward circ task list ptr */
} *Task;

/* Get current task id. */
/*  macro Task				   returns current task id
Ego() */
    extern Task ego_;			/* current task id */
#   define Ego() (ego_)

#define Tvarb (Ego()->varb)		/* task variable */

/* Suspend execution; wait until wakened; then resume. */
    extern unsigned long		/* returns current (resumption) time */
Sleep ();				/* */

/* Wake a task. */
/*  macro void
Wake()
    Task task				   task to wake */
#   define Wake(task) \
    ((void) ((task) && ((task)->alarm = 0)))

/* Wake current task at future time. */
/*  macro void
Alarm()
    unsigned long clock			   time to wake */
#   define Alarm(clock) \
    ((void) ((clock) < Ego()->alarm && (Ego()->alarm = (clock))))

/* Begin critical section (no-op because no true parallelism). */
/*  macro void
Crit()					   */
#   define Crit() ((void) 0)

/* End critical section (no-op because no true parallelism). */
/*  macro void
Uncrit()				   */
#   define Uncrit() ((void) 0)

extern unsigned long Stksize;		/* stack size */

/* Create a new task. */
    extern Task				/* returns new task id or null */
Newtask ();				/*
    void (*proc)()			   main task procedure ptr
    Ptr varb				   initial task variable value */

#define task_h
#endif

/*
     *    * * *  *      * * *  * * *    *    *   *  * * *  * *          *     *
   * *    *      *      *        *    *   *  *   *  *      *   *      *     *
 * * * *  * *    *      * *      *    *      * * *  * *    * *    * * * * *
   * *    *      *      *        *    *   *  *   *  *      *   *      *     *
     *    *      * * *  * * *    *      *    *   *  * * *  *   *        *     *

 * Programmed by John G. Fletcher, who makes it available without any warranty
 * and maintains it on the basis of best effort, time as available.  No
 * organization or other person has any responsibility for it.

 * (C) Copyright 1992, the Regents of the University of California.  All Rights
 * Reserved.  This work was produced under the sponsorship of the U.S.
 * Department of Energy.  The Government retains certain rights therein.
 */
