//                              -*- Mode: CFA -*-
//
// 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 : Thierry Delisle
// Last Modified On : --
// Update Count     : 0
//

#ifndef MONITOR_H
#define MONITOR_H

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

struct __monitor_t {
	spinlock lock;
	thread * owner;
	simple_thread_list entry_queue;
	unsigned int recursion;
};

static inline void ?{}(__monitor_t * this) {
	this->owner = 0;
	this->recursion = 0;
}

//Basic entering routine
void enter(__monitor_t *);
void leave(__monitor_t *);

//Array entering routine
void enter(__monitor_t **, int count);
void leave(__monitor_t **, int count);

struct monitor_guard_t {
	__monitor_t ** m;
	int count;
};

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

static inline void ?{}( monitor_guard_t * this, __monitor_t ** m ) {
	this->m = m;
	this->count = 1;
	enter( *this->m );
}

static inline void ?{}( monitor_guard_t * this, __monitor_t ** m, int count ) {
	this->m = m;
	this->count = count;
	qsort(this->m, count);
	enter( this->m, this->count );
}

static inline void ^?{}( monitor_guard_t * this ) {
	leave( this->m, this->count );
}


#endif //MONITOR_H