//
// 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 3+ components:
//   Type
//   name in structured / global name
//   init code (optional)
//   restore code (optional)
//

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

// initialization: tell whether the system has been initialised and by whom.
int				Initialised				=FALSE
int				InitialisedFromPL			=FALSE
int				PL_Argc					=0
char**				PL_Argv					=NULL
bool				FAST_BOOT_FLAG				=false

// halt hooks
struct halt_hook*		HaltHooks				=NULL
fptr_t 				JIT_finalizer				=NULL

// stack overflow expansion/gc control
int				AllowLocalExpansion 			=TRUE
int				AllowGlobalExpansion 			=TRUE
int				AllowTrailExpansion 			=TRUE
UInt				SizeOfOverflow				=0

// amount of space recovered in all garbage collections
UInt				AGcThreshold				=10000
Agc_hook			AGCHook					=NULL

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

#if defined(YAPOR) || defined(THREADS)
// protect long critical regions
lockvar				BGL					MkLock
#endif

#if defined(YAPOR) || defined(TABLING)
struct global_optyap_data	optyap_data				void
#endif /* YAPOR || TABLING */

// whether Yap is responsible for signal handling

int             		PrologShouldHandleInterrupts		void

/* This is the guy who actually started the system, and who has the correct registers */
#if defined(THREADS)
pthread_t 			master_thread				void
struct thread_mbox*		named_mboxes		                =NULL
lockvar				mboxq_lock				MkLock
UInt				mbox_count				=0
struct swi_mutex*		WithMutex		                void
#endif /* THREADS */

// streams
struct stream_desc*		Stream					void
#if defined(THREADS)||defined(YAPOR)
lockvar				StreamDescLock                MkLock
#endif

// access to yap initial arguments
char**				argv					void
int           		 	argc					void

// extensions to Terms
#ifdef COROUTINING
/* array with the ops for your favourite extensions */
ext_op 				attas[attvars_ext+1]			void
#endif

// agc.c
int 				agc_calls				void
YAP_ULONG_LONG 			agc_collected				void
/* total time spent in GC */
Int 				tot_agc_time 				=0
/* number of heap objects in all garbage collections */
Int 				tot_agc_recovered 			=0

//arrays.c
#if HAVE_MMAP
struct MMAP_ARRAY_BLOCK* 	mmap_arrays 				=NULL
#endif


#ifdef DEBUG
//computils.c
char            		Option[20]				void
YP_FILE*			logfile					void
//init.c
//int  				output_msg 				=FALSE
#endif

#if defined(COFF)  || defined(A_OUT)
// loada_coff.c && load_aout.c
char 				Executable[YAP_FILENAME_MAX]		void
#endif

int				OpaqueHandlersCount			=0
struct YAP_opaque_handler_struct* 	OpaqueHandlers				=NULL

#if  __simplescalar__
char				pwd[YAP_FILENAME_MAX]			void
#endif

//udi.c
//struct udi_control_block 	RtreeCmd				void

const char*			 	RestoreFile				void

//gprof.c
Int 				ProfCalls				void
Int				ProfGCs 				void
Int				ProfHGrows 				void
Int				ProfSGrows 				void
Int				ProfMallocs 				void
Int				ProfIndexing 				void
Int				ProfOn 					void
Int				ProfOns					void
struct RB_red_blk_node* 	ProfilerRoot				void
struct RB_red_blk_node* 	ProfilerNil				void
char*				DIRNAME					=NULL
#if LOW_PROF
int				ProfilerOn				=FALSE
FILE*				FProf					=NULL
FILE*				FPreds					=NULL
#endif /* LOW_PROF */

// Mutexes
#if THREADS
struct swi_mutex*		FreeMutexes				=NULL
struct swi_mutex*		mutex_backbone				=NULL
lockvar				MUT_ACCESS				MkLock
#endif

char*				Home					=NULL

/* ISO char conversion: I will make no comments */
char*				CharConversionTable			=NULL
char*				CharConversionTable2			=NULL


/* max priority */
int 				MaxPriority				    =1200

/// alias table access
struct    AliasDescS*		FileAliases				=Yap_InitStandardAliases()
int				NOfFileAliases				void
int				SzOfFileAliases				void

struct vfs*			VFS					=Yap_InitAssetManager()

char*          cwd                         =NULL

END_GLOBAL_DATA