/*
 * $RCSfile: event.h,v $
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 *
 * $Log: event.h,v $
 * Revision 1.2  1996/01/29  20:25:55  slm
 * Updated copyright and version.
 *
 * Revision 1.1  1995/07/28  21:29:10  slm
 * Initial revision
 *
 * Revision 1.22.3.1  1994/10/27  20:47:48  hkaram
 * New branch
 *
 * Revision 1.22  1994/01/10  17:52:33  menze
 *   [ 1994/01/04          menze ]
 *   Added an additional field to struct Event for per-platform state
 */

#ifndef event_h
#define event_h

#include "idmap.h"
#include "xtime.h"

/* Return values for evCancel */
typedef enum {
    EVENT_FINISHED = -1,
    EVENT_RUNNING = 0,
    EVENT_CANCELLED = 1
} EvCancelReturn;

/*
 * This structure is to be opaque.  We have to define it here, but protocol
 * implementors are not allowed to know what it contains.
 */
typedef enum {
    E_PENDING,
    E_SCHEDULED,
    E_RUNNING,
    E_BLOCKED,
    E_FINISHED
} EvState;

#if defined(XK_DEBUG)
#  define XK_THREAD_TRACE
#endif

#ifdef XK_THREAD_TRACE
#  define EV_CHECK_STACK(_str) evCheckStack(_str)
#else
#  define EV_CHECK_STACK(_str) do {} while(0)
#endif

typedef struct Event {
    struct Event *next, *prev;
    unsigned     deltat;
#ifdef __STDC__
    void         (*func)(struct Event *event, void *arg);
#else
    void         (*func)();
#endif
    void         *arg;
    EvState      state;
    unsigned     flags;
#ifdef XK_THREAD_TRACE
    XTime        startTime;	/* Time started */
    XTime        stopTime;	/* Time stopped or blocked */
    Binding      bind;
    void         *earlyStack;
#endif
    void         *extra;	/* any platform-specific state */
} *Event;

#ifdef __STDC__
typedef void (*EvFunc)(Event event, void *arg);
#else
typedef void (*EvFunc)();
#endif

/*
 * schedule an event that executes f w/ argument a after delay t usec;
 * t may equal 0, which implies createprocess
 */
#ifdef __STDC__
Event evSchedule(EvFunc func, void *arg, unsigned usec);
#else
Event evSchedule();
#endif

/*
 * releases a handle to an event; as soon f completes, the resources
 * associated with the event are freed
 */
#ifdef __STDC__
void evDetach(Event event);
#else
void evDetach();
#endif

/* cancel event e:
 *  returns EVENT_FINISHED if it knows that the event has already executed
 *     to completion
 *  returns EVENT_RUNNING if the event is currently running
 *  returns EVENT_CANCELLED if the event has been successfully cancelled
 */
#ifdef __STDC__
EvCancelReturn evCancel(Event event);
#else
EvCancelReturn evCancel();
#endif

/* returns true (non-zero) if an 'evCancel' has been performed on the event */
#ifdef __STDC__
int evIsCancelled(Event event);
#else
int evIsCancelled();
#endif

/* Displays a 'ps'-style listing of x-kernel threads */
#ifdef __STDC__
void evDump(void);
#else
void evDump();
#endif

/*
 * Platform-specific check to see if this event is in danger of overrunning
 * the stack, triggering warning/error messages if so.
 */
#ifdef __STDC__
void evCheckStack(char *);
#else
void evCheckStack();
#endif

#endif
