/*
 * Really Simple Threads package.  Copyright Robert S. Thau 1996.
 * This code may be used for any purpose.  It may be redistributed on
 * the following terms: 1) this paragraph and the next must appear
 * unaltered in any source distribution, and be prominently displayed
 * in the supporting documentation for any source or binary
 * distribution; 2) if you wish to distribute modified or derived
 * works, each such work must bear a notice of who last altered the
 * included version of Really Simple Threads, and when.  The notice of
 * alterations shall be attached to the derivative works in the same
 * manner as these two paragraphs, and shall be at least as prominent.
 * Redistribution for any purpose is otherwise unrestricted.
 *
 * THIS CODE IS IN AN UNFINISHED STATE, AND HAS NOT BEEN TESTED
 * TO THE POINT THAT THE AUTHOR BELIEVES IT CAN BE EXPECTED TO
 * WORK RELIABLY.  IT IS SUITABLE, AT THIS POINT, FOR EXPERIMENTATION
 * AND DEBUGGING ONLY.  TREAT ACCORDINGLY.
 */

#include <stdio.h>
#include <stdlib.h>
#include "rsthreads.h"

/*
 * Simple smoke test --- verifies that basic thread setup and
 * context switching works OK.  If you want to do a port, start
 * with this.
 */

void *my_action_func (void *aarg) {
    int i, j;
    char *arg = (char *)aarg;
    for (i = 0; i < 10; ++i) {
	fprintf (stderr, "%s: %d\n", arg, i);
	
	/* Make sure time between rst_yield() calls isn't less than
	 * the resolution of gettimeofday() on fast systems.
	 */
	
	for (j = 0; j < 100000; ++j)
	    continue;
	
	rst_yield ();
    }
    return 0;
}

int main (int argc, char **argv) {
    int i = 0;
    
    rst_init ();
    
    rst_spawn (my_action_func, "fubar");
    rst_spawn (my_action_func, "baz");
    rst_set_priority (rst_spawn (my_action_func, "shazam"),
		      RST_MAX_PRIORITY);

    while (rst_count_threads() > 1) {
	fprintf (stderr, "main: %d threads\n", rst_count_threads());
	if (++i == 5) rst_set_priority (rst_current_thread(), 0);
	rst_yield ();
    }

    fprintf (stderr, "All done\n");
    exit(0);
}
