/*
 * Simple check for broken core.
 */

#include <../source/TAL/objbase.h>
#include <../source/PAL/dataman.h>

#if		!defined(CHECKED_BUILD) || !defined(MALLOC_DEBUG)
#error	This program requires extra per-object information; define
#error	both CHECKED_BUILD and MALLOC_DEBUG, and than recompile libV.
#error	See libv/include/v_conf.h for details.
#endif  /* !CHECKED_BUILD || !MALLOC_DEBUG */

/*
 * Prints address and magic of thread - specific data.
 */
void print_tsd(char *ind)
{
	char *tsd;
	tsd=(char*)GetThreadData();
	/* Assume 'magic' is the first field of TSD */
	conPrintf("%s: TSD %X, magic '%c%c%c%c'\n",ind,tsd,tsd[3],tsd[2],tsd[1],tsd[0]);
}
	
void V_CALL test_thread_entry(void *arg)
{
	if(arg); /* make compiler happy */
	print_tsd("test_thread");
	conPrintf("test_thread: sleep for 1 second ...\n");
	timeDelay(1000);
	conPrintf("test_thread: woken up.\n");
	threadExit(0);
}

int main()
{
	thread_t test;
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
	print_tsd("main thread - before threadCreate()");
	test=threadCreate(test_thread_entry,NULL);
	timeDelay(50);
	print_tsd("main thread - after threadCreate()");
	conPrintf("Memory stats:\n");
	memPrintObjStats();
	memPrintMemStats();
	conPrintf("===============================\n");
	threadJoin(test,NULL);
	print_tsd("main thread - after threadJoin()");
	/* Note: last block remains in hash until logStopConsoleLog() */
	conPrintf("Memory stats:\n");
	memPrintObjStats();
	memPrintMemStats();
	conPrintf("===============================\n");
	return 0;
}
