//
// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// monitor --
//
// Author           : Thierry Delisle
// Created On       : Thd Feb 23 12:27:26 2017
// Last Modified By : Peter A. Buhr
// Last Modified On : Sat Oct  7 18:06:45 2017
// Update Count     : 10
//

#pragma once

#include <stddef.h>

#include <assert.h>
#include "invoke.h"
#include "stdlib"

trait is_monitor(dtype T) {
	monitor_desc * get_monitor( T & );
	void ^?{}( T & mutex );
};

static inline void ?{}(monitor_desc & this) {
	(this.lock){};
	(this.entry_queue){};
	(this.signal_stack){};
	this.owner         = NULL;
	this.recursion     = 0;
	this.mask.accepted = NULL;
	this.mask.clauses  = NULL;
	this.mask.size     = 0;
	this.dtor_node     = NULL;
}

// static inline int ?<?(monitor_desc* lhs, monitor_desc* rhs) {
// 	return ((intptr_t)lhs) < ((intptr_t)rhs);
// }

struct monitor_guard_t {
	monitor_desc ** m;
	int count;
	monitor_desc ** prev_mntrs;
	unsigned short  prev_count;
	fptr_t          prev_func;
};

void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() );
void ^?{}( monitor_guard_t & this );

struct monitor_dtor_guard_t {
	monitor_desc * m;
	monitor_desc ** prev_mntrs;
	unsigned short  prev_count;
	fptr_t          prev_func;
};

void ?{}( monitor_dtor_guard_t & this, monitor_desc ** m, void (*func)() );
void ^?{}( monitor_dtor_guard_t & this );

static inline forall( dtype T | sized(T) | { void ^?{}( T & mutex ); } )
void delete( T * th ) {
	^(*th){};
	free( th );
}

//-----------------------------------------------------------------------------
// Internal scheduling

struct __condition_criterion_t {
	bool ready;						//Whether or not the criterion is met (True if met)
	monitor_desc * target;				//The monitor this criterion concerns
	struct __condition_node_t * owner;		//The parent node to which this criterion belongs
	__condition_criterion_t * next;		//Intrusive linked list Next field
};

struct __condition_node_t {
	thread_desc * waiting_thread;			//Thread that needs to be woken when all criteria are met
	__condition_criterion_t * criteria; 	//Array of criteria (Criterions are contiguous in memory)
	unsigned short count;				//Number of criterions in the criteria
	__condition_node_t * next;			//Intrusive linked list Next field
	uintptr_t user_info;				//Custom user info accessible before signalling
};

struct __condition_blocked_queue_t {
	__condition_node_t * head;
	__condition_node_t ** tail;
};

void ?{}(__condition_node_t & this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info );
void ?{}(__condition_criterion_t & this );
void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner );

void ?{}( __condition_blocked_queue_t & );
void append( __condition_blocked_queue_t *, __condition_node_t * );
__condition_node_t * pop_head( __condition_blocked_queue_t * );

struct condition {
	__condition_blocked_queue_t blocked;	//Link list which contains the blocked threads as-well as the information needed to unblock them
	monitor_desc ** monitors;			//Array of monitor pointers (Monitors are NOT contiguous in memory)
	unsigned short monitor_count;			//Number of monitors in the array
};

static inline void ?{}( condition & this ) {
	this.monitors = NULL;
	this.monitor_count = 0;
}

static inline void ^?{}( condition & this ) {
	free( this.monitors );
}

void wait( condition * this, uintptr_t user_info = 0 );
bool signal( condition * this );
bool signal_block( condition * this );
static inline bool is_empty( condition * this ) { return !this->blocked.head; }
uintptr_t front( condition * this );

//-----------------------------------------------------------------------------
// External scheduling

struct __acceptable_t {
	__monitor_group_t;
	bool is_dtor;
};

void __waitfor_internal( const __waitfor_mask_t & mask, int duration );

// Local Variables: //
// mode: c //
// tab-width: 4 //
// End: //
