//
// 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.
//
// kernel --
//
// Author           : Thierry Delisle
// Created On       : Tue Jan 17 12:27:26 2017
// Last Modified By : Peter A. Buhr
// Last Modified On : Sat Jul 22 09:58:39 2017
// Update Count     : 2
//

#pragma once

#include <stdbool.h>

#include "invoke.h"

extern "C" {
#include <pthread.h>
}

//-----------------------------------------------------------------------------
// Locks
void lock      ( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, spin if already acquired
void lock_yield( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, yield repeatedly if already acquired
bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, return false if already acquired
void unlock    ( spinlock * );                        // Unlock the spinlock

struct semaphore {
	spinlock lock;
	int count;
	__thread_queue_t waiting;
};

void  ?{}(semaphore * this, int count = 1);
void ^?{}(semaphore * this);
void P(semaphore * this);
void V(semaphore * this);


//-----------------------------------------------------------------------------
// Cluster
struct cluster {
	spinlock ready_queue_lock;                      // Ready queue locks
	__thread_queue_t ready_queue;                   // Ready queue for threads
	unsigned long long int preemption;              // Preemption rate on this cluster
};

void ?{}(cluster * this);
void ^?{}(cluster * this);

//-----------------------------------------------------------------------------
// Processor
enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule };

//TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI)
struct FinishAction {
	FinishOpCode action_code;
	thread_desc * thrd;
	spinlock * lock;
	spinlock ** locks;
	unsigned short lock_count;
	thread_desc ** thrds;
	unsigned short thrd_count;
};
static inline void ?{}(FinishAction * this) {
	this->action_code = No_Action;
	this->thrd = NULL;
	this->lock = NULL;
}
static inline void ^?{}(FinishAction * this) {}

// Processor
// Wrapper around kernel threads
struct processor {
	// Main state
	struct processorCtx_t * runner;                 // Coroutine ctx who does keeps the state of the processor
	cluster * cltr;                                 // Cluster from which to get threads
	pthread_t kernel_thread;                        // Handle to pthreads

	// Termination
	volatile bool do_terminate;                     // Set to true to notify the processor should terminate
	semaphore terminated;                           // Termination synchronisation

	// RunThread data
	struct FinishAction finish;                     // Action to do after a thread is ran

	// Preemption data
	struct alarm_node_t * preemption_alarm;         // Node which is added in the discrete event simulaiton
	bool pending_preemption;                        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible

#ifdef __CFA_DEBUG__
	char * last_enable;                             // Last function to enable preemption on this processor
#endif
};

void ?{}(processor * this);
void ?{}(processor * this, cluster * cltr);
void ^?{}(processor * this);

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