#include <v_lib.h>

vboolean done = false;

void	V_CALL
second_thread(void *arg)
{
	char buf[4096];
	rwl_fifo_t accum=(rwl_fifo_t)arg;
	
	while(!done)
	{
		rwlPushBuffer(accum,buf,4096);
		timeDelay(23);
	}

	threadExit(0);
}

#define in_len	(44100 * 4)

char buf[in_len];

int main()
{
	u32_t count, len, limit = 4 * in_len;
	
	rwl_fifo_t accum;
	thread_t recv_thread;
	
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
	accum=rwlCreate();
	rwlBlockMode(accum,true);
	conPuts("Spawning second thread ...\n");
	recv_thread=threadCreate(second_thread,accum);

	for(count=0;count < limit;)
	{
		len=rwlPopBuffer(accum,buf,in_len);
		conPrintf("rwlPopBuffer(): received %u bytes\n",len);
		count+=len;
		timeDelay(1);
	}
	done = true;

	threadJoin(recv_thread,NULL);
	rwlDestroy(accum);
	
	memPrintMemStats();
	
	return 0;
}
