/*
    $Id$
    $Log$
    Desc:
    Lang: english
*/
#include "exec_intern.h"
#include <exec/execbase.h>
#include <aros/libcall.h>

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

    NAME */
	#include <clib/exec_protos.h>

	__AROS_LH1(ULONG, Wait,

/*  SYNOPSIS */
	__AROS_LA(unsigned long, signalSet, D0),

/*  LOCATION */
	struct ExecBase *, SysBase, 53, Exec)

/*  FUNCTION
	Wait until some signals are sent to the current task. If any signal
	of the specified set is already set when entering this function it
	returns immediately. Since almost any event in the OS can send a
	signal to your task if you specify it to do so signals are a very
	powerful mechanism.

    INPUTS
	signalSet - The set of signals to wait for.

    RESULT
	The set of active signals.

    NOTES
	Naturally it's not allowed to wait in supervisor mode.

	Calling Wait() breaks an active Disable() or Forbid().

    EXAMPLE

    BUGS

    SEE ALSO
	Signal(), SetSignal(), AllocSignal(), FreeSignal()

    INTERNALS

    HISTORY
	29-10-95    digulla automatically created from
			    exec_lib.fd and clib/exec_protos.h
	17-12-95    digulla Incorporated code by Matthias Fleischner

*****************************************************************************/
{
    __AROS_FUNC_INIT
    ULONG rcvd;
    struct Task *me;

    /* Get pointer to current task - I'll need it very often */
    me=SysBase->ThisTask;

    /* Protect the task lists against access by other tasks. */
    Disable();

    /* If at least one of the signals is already set do not wait. */
    while(!(me->tc_SigRecvd&signalSet))
    {
	/* Set the wait signal mask */
	me->tc_SigWait=signalSet;

	/*
	    Clear TDNestCnt (because Switch() will not care about it),
	    but memorize it first. IDNestCnt is handled by Switch().
	    This could as well be stored in a local variable which makes
	    the tc_TDNestCnt field somehow redundant.
	*/
	me->tc_TDNestCnt=SysBase->TDNestCnt;
	SysBase->TDNestCnt=-1;

	/* Move current task to the waiting list. */
	me->tc_State=TS_WAIT;
	Enqueue(&SysBase->TaskWait,&me->tc_Node);

	/* And switch to the next ready task. */
	Switch();
	/*
	    OK. Somebody awakened me. This means that either the
	    signals are there or it's just a finished task exception.
	    Test again to be sure (see above).
	*/

	/* Restore TDNestCnt. */
	SysBase->TDNestCnt=me->tc_TDNestCnt;
    }
    /* Get active signals. */
    rcvd=me->tc_SigRecvd&signalSet;

    /* And clear them. */
    me->tc_SigRecvd&=~signalSet;

    /* All done. */
    Enable();
    return rcvd;
    __AROS_FUNC_EXIT
} /* Wait */
