//
// File defining fields in the Yap_GLOBAL global structure
//
// these fields used to spread all over the place, because they must be used in 3 ways:
//  - they must be defined somewhere
//  - they have an #ifdef to get a shorter name
//  - they must be initialised somewhere
//  - they may be of interest to restore
//
// The defs include 4+ components:
//   Type
//   name in structured
//   global name
//   init code (optional)
//   restore code (optional)
//
//   


// Stuff that must be considered local to a thread or worker
START_WORKER_LOCAL

/* shifts and restore: per local data-structure */
restoreinfo	rinfo
.	rinfo.old_ASP	OldASP				=NULL
.	rinfo.old_LCL0	OldLCL0				=NULL
.	rinfo.old_TR		OldTR			=NULL
.	rinfo.old_GlobalBase	OldGlobalBase		=NULL
.	rinfo.old_H		OldH			=NULL
.	rinfo.old_H0		OldH0			=NULL
.	rinfo.old_TrailBase	OldTrailBase		=NULL
.	rinfo.old_TrailTop	OldTrailTop		=NULL
.	rinfo.old_HeapBase	OldHeapBase		=NULL
.	rinfo.old_HeapTop	OldHeapTop		=NULL
.	rinfo.cl_diff		ClDiff			=0L
.	rinfo.g_diff		GDiff			=0L
.	rinfo.h_diff		HDiff			=0L
.	rinfo.g_diff0		GDiff0			=0L
.	rinfo.g_split		GSplit			=NULL
.	rinfo.l_diff		LDiff			=0L
.	rinfo.tr_diff		TrDiff			=0L
.	rinfo.x_diff		XDiff			=0L
.	rinfo.delay_diff	DelayDiff		=0L
.	rinfo.base_diff		BaseDiff		=0L

struct reduction_counters		call_counters	
.	call_counters.reductions	ReductionsCounter		=0L
.	call_counters.reductions_retries	PredEntriesCounter	=0L
.	call_counters.retries		RetriesCounter			=0L
.	call_counters.reductions_on	ReductionsCounterOn		=0L
.	call_counters.reductions_retries_on	PredEntriesCounterOn	=0L
.	call_counters.retries_on	RetriesCounterOn		=0L

int	interrupts_disabled		Yap_InterruptsDisabled		=FALSE

// support for consulting files
/* current consult stack */
union CONSULT_OBJ*	consultsp	ConsultSp	=NULL
/* current maximum number of cells in consult stack */
UInt	consultcapacity			ConsultCapacity	void
/* top of consult stack  */
union CONSULT_OBJ*	consultbase	ConsultBase	=NULL
/* low-water mark for consult  */
union CONSULT_OBJ*	consultlow	ConsultLow	=NULL

//global variables
Term			global_arena		GlobalArena	=0L TermToGlobalOrAtomAdjust
UInt			global_arena_overflows	GlobalArenaOverflows	=0L
Int			delay_arena_overflows	DelayArenaOverflows	=0L
Int			arena_overflows		ArenaOverflows	=0L
Int			depth_arenas		DepthArenas	=0

int			arith_error		ArithError	=FALSE
struct pred_entry*	last_asserted_pred	LastAssertedPred	=NULL
int			debug_on		DebugOn		=FALSE
struct format_status*	f_info			FormatInfo	=NULL
char*			scanner_stack		ScannerStack	=NULL
struct scanner_extra_alloc*	scanner_extra_blocks	ScannerExtraBlocks	=NULL
struct DB_TERM	       *ball_term		BallTerm	=NULL RestoreBallTerm(wid)
UInt			active_signals		ActiveSignals	=0L
UInt			i_pred_arity		IPredArity	=0L
yamop*			prof_end		ProfEnd	=NULL
int			uncaught_throw		UncaughtThrow	=FALSE
int			doing_undefp		DoingUndefp	=FALSE
Int			start_line		StartLine	=0L
scratch_block		scratchpad		ScratchPad	InitScratchPad(wid)
#ifdef  COROUTINING
Term			woken_goals		WokenGoals	=0L TermToGlobalAdjust
Term			atts_mutable_list	AttsMutableList	=0L TermToGlobalAdjust
#endif

// gc_stuff
Term			gc_generation		GcGeneration	=0L TermToGlobalAdjust
Term			gc_phase		GcPhase		=0L TermToGlobalAdjust
UInt			gc_current_phase	GcCurrentPhase	=0L
UInt			gc_calls		GcCalls		=0L
Int			tot_gc_time		TotGcTime	=0L
YAP_ULONG_LONG		tot_gc_recovered	TotGcRecovered	=0L
Int			last_gc_time		LastGcTime	=0L
Int			last_ss_time		LastSSTime	=0L

// global variables that cannot be global in a thread/or-p implementation
#if defined(YAPOR) || defined(THREADS)
lockvar				signal_lock	SignalLock	MkLock
struct pred_entry*		wpp		WPP		=NULL
/* in a single gc */
Int				tot_marked	total_marked	=0L
Int				tot_oldies	total_oldies	=0L
#if DEBUG && COROUTINING
UInt			tot_smarked	total_smarked	=0L
#endif
struct choicept			*wl_current_B	current_B	=NULL
CELL*				wl_prev_HB	prev_HB		=NULL
CELL*				hgen		HGEN		=NULL
CELL**				ip_top		iptop		=NULL
#if GC_NO_TAGS
char*				b_p		Yap_bp		=NULL
#endif
#if defined(TABLING) || defined(SBA)
struct trail_frame*		wl_sTR		sTR		=NULL
struct trail_frame*		wl_sTR0		sTR0		=NULL
struct trail_frame*		new_tr		new_TR		=NULL
#else
Term				*wl_sTR		sTR		=NULL
Term				*wl_sTR0	sTR0		=NULL
Term				*new_tr		new_TR		=NULL
#endif
struct gc_mark_continuation*	conttop0	cont_top0	=NULL
struct gc_mark_continuation*	conttop		cont_top	=NULL
int				disc_trail_entries	discard_trail_entries		=NULL
gc_ma_hash_entry		Gc_ma_hash_table[GC_MAVARS_HASH_SIZE]	gc_ma_hash_table	void
gc_ma_hash_entry*		Gc_ma_h_top	gc_ma_h_top	=NULL
gc_ma_hash_entry*		Gc_ma_h_list	gc_ma_h_list	=NULL
UInt				Gc_timestamp	gc_timestamp	=0L
ADDR				DB_vec		db_vec		=NULL
ADDR				DB_vec0		db_vec0		=NULL
struct RB_red_blk_node*		DB_root		db_root		=NULL
struct RB_red_blk_node*		DB_nil		db_nil		=NULL
#endif /* defined(YAPOR) || defined(THREADS) */


jmp_buf				gc_restore		Yap_gc_restore	void
struct array_entry*		dynamic_arrays		DynamicArrays	=NULL PtoArrayEAdjust
struct static_array_entry*	static_arrays		StaticArrays	=NULL PtoArraySAdjust
struct global_entry*		global_variables	GlobalVariables	=NULL PtoGlobalEAdjust
int				allow_restart		Yap_AllowRestart	=FALSE

// Thread Local Area for Fast Storage of Intermediate Compiled Code.
struct mem_blk*			cmem_first_block	Yap_CMemFirstBlock	=NULL
UInt				cmem_first_block_sz	Yap_CMemFirstBlockSz	=0L

// Thread Local Area for Labels.
Int*				label_first_array	Yap_LabelFirstArray	=NULL
UInt				label_first_array_sz	Yap_LabelFirstArraySz	=0L

// Ricardo's stuff
#if (defined(YAPOR) || defined(TABLING)) && defined(THREADS)
struct worker			worker			WORKER		void
#endif

#ifdef THREADS
struct thandle			thread_handle		ThreadHandle	InitThreadHandle	
#define FOREIGN_ThreadHandle(wid)  (Yap_WLocal[(wid)].thread_handle)		       						
#define MY_ThreadHandle	       (Yap_WLocal[worker_id].thread_handle)
#endif
// END WORKER LOCAL STUFF
END_WORKER_LOCAL

// Stuff that must be shared by all threads or workers
START_WORKER_SHARED

/* multi-thread support */
#if THREADS
/* number of threads and processes in system */
UInt		n_of_threads		NOfThreads	=1
/* number of threads created since start */
UInt		n_of_threads_created	NOfThreadsCreated =1
/* total run time for dead threads */
UInt  threads_total_time		ThreadsTotalTime =0L
#endif

// multi-thread/ORP support
#if defined(YAPOR) || defined(THREADS)
// protect long critical regions
lockvar		bgl			BGL		MkLock
#endif

// stack overflow expansion/gc control
int		allow_local_expansion	Yap_AllowLocalExpansion =TRUE
int		allow_global_expansion	Yap_AllowGlobalExpansion =TRUE
int		allow_trail_expansion	Yap_AllowTrailExpansion =TRUE
UInt		size_of_overflow	SizeOfOverflow		=0
// amount of space recovered in all garbage collections
UInt		agc_last_call		AGcLastCall		=0
// amount of space recovered in all garbage collections
UInt		agc_threshold		AGcThreshold		=10000
Agc_hook	agc_hook		AGCHook			=NULL

// readline
#if HAVE_LIBREADLINE
char		*readline_buf		ReadlineBuf		=NULL
char		*readline_pos		ReadlinePos		=0L
#endif

// Threads Array
#ifdef THREADS
lockvar		thread_handles_lock	ThreadHandlesLock	MkLock
#endif 

// Ricardo's stuff
#if defined(YAPOR) || defined(TABLING)
struct global_data	global			GLOBAL		void	
struct local_data	remote[MAX_WORKERS]	REMOTE		void
#endif

// initialization: tell whether the system has been initialised and by whom.
int		initialised		Yap_Initialised		=FALSE
int		initialised_from_pl	Yap_InitialisedFromPL	=FALSE
int		pl_argc			Yap_PL_Argc		=0
char	      **pl_argv			Yap_PL_Argv		=NULL

END_WORKER_SHARED