/*
 *	Copyright 1990 by Rayan S. Zachariassen, all rights reserved.
 *	This will be free software, but only when it is finished.
 */
/*
 *	Lots of modifications (new guts, more or less..) by
 *	Matti Aarnio <mea@nic.funet.fi>  (copyright) 1992-1995
 */

#include "hostenv.h"
#include <stdio.h>
#include "scheduler.h"
#include "prototypes.h"

time_t qipcretry = 0;

int	/* return non-zero when there are some childs
	   available for start				*/
doagenda()
{
	int didsomething;
	struct thread *ncuritem;

	time(&now);

	ncuritem = thread_head;
	didsomething = 0;
	do {
	  didsomething = 0;
	  if (verbose)
	    printf("curitem %lx curitem->wakeup %lu now %d\n", (long)ncuritem,
		   ncuritem != NULL ? ncuritem->wakeup : 0, (int)now);

	  /* thread_head -chain should be in time order, thus the while-loop
	     should be traversed only once, because  thread_start kicks
	     right away (or reschedules) */

	  while (ncuritem != NULL /* && ncuritem->wakeup <= now */) {
	    if (ncuritem->proc == NULL && ncuritem->wakeup <= now) {
	      didsomething += thread_start(ncuritem);
	    }

	    ncuritem = ncuritem->nexttr;
	  }

	  mux(0); /* Rather fast check.. */
	} while (didsomething && ncuritem);

	time(&now);

	/* if (verbose)
	   printf("alarmed %d\n", now);  */

	if (qipcretry > 0 && qipcretry <= now) {
	  qipcretry = 0;
	  queryipcinit();
	  /*
	   * If qipcretry is set here, the value will be ignored, but
	   * that's ok since sweepretry is active by now
	   */
	}
	return (didsomething);
}
