/*
 * mips/threads.h
 * Mips threading information.
 *
 * Copyright (c) 1996 T. J. Wilkinson & Associates, London, UK.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

#ifndef __mips_threads_h
#define __mips_threads_h

/**/
/* Thread handling */
/**/
#define	USE_INTERNAL_THREADS

#define	THREADSTACKSIZE		(32 * 1024)

#define	THREADSWITCH(to, from) \
	_threadswitch(&((to)->restorePoint), &((from)->restorePoint))

#define THREADINIT(to, func) {                                  \
                int* regstore = (int*)((to)->stackEnd - (32 * 4));\
                (to)->restorePoint = (void*)regstore;		\
		regstore[29] = ((int)regstore) - (32 * 4);	\
                regstore[31] = func;				\
		regstore[25] = func;				\
        }

#define	THREADINFO(ee)						\
		do {						\
			void** ptr;				\
			ptr = _threadsp();			\
			(ee)->restorePoint = 0;			\
			(ee)->stackEnd = (void*)ptr;		\
			(ee)->stackBase = (ee)->stackEnd - threadStackSize;\
			(ee)->flags = THREAD_FLAGS_NOSTACKALLOC;\
		} while(0)

#define	THREADFRAMES(tid, cnt)					\
		do {						\
			void** ptr;				\
			cnt = 0;				\
			if (tid == currentThread) {		\
				ptr = _threadsp();		\
			}					\
			else {					\
				ptr = ((void***)TCTX(tid)->restorePoint)[2];\
			}					\
			while (*ptr != 0) {			\
				cnt++;				\
				ptr = (void**)ptr[2];		\
			}					\
		} while (0)

#endif
