/**************************************************************************/
/* test3 - shows ksendmessage, kgetmessage async messaging                */
/**************************************************************************/

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

void *peer = NULL;

void switcher1(DWORD arg)
{
    int x;
#if defined(__TURBOC__)||defined(__BORLANDC__)
    randomize();
#else
    srand( time(0) );
#endif
    

    do {
        rt_yield();
        x = rand();
        if ( x > 25000 ) {
            cprintf( " sending %u ", x );
            ksendmessage( peer, x, 0 );
            ksendmessage( peer, x, 0 );
            ksendmessage( peer, x, 0 );
        }
    } while ( 1 );
}
void switcher2(DWORD arg)
{
    int x;
    DWORD y;
    do {
        rt_yield();
        if ( kgetmessage( &x, &y ) )
            cprintf(" read %u \r\n", x );
        else
            cprintf(".");
    } while ( 1 );
}

int main( int argc , char **argv )
{
    kdebug = 1;
    rt_init(100);
    cputs("starting...\r\n");
    peer = rt_newthread( switcher2, 2,4096, 0, "worker" );
    rt_newthread( switcher1, 1,4096, 0, "worker" );
    rt_sleep( 10000 );
}

