//                              -*- 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.
//
// threads --
//
// Author           : Thierry Delisle
// Created On       : Tue Jan 17 12:27:26 2016
// Last Modified By : Thierry Delisle
// Last Modified On : --
// Update Count     : 0
//

#ifndef THREADS_H
#define THREADS_H

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

#include "coroutines"

//-----------------------------------------------------------------------------
// Coroutine trait
// Anything that implements this trait can be resumed.
// Anything that is resumed is a coroutine.
trait is_thread(dtype T | sized(T)) {
      void main(T* this);
      thread* get_thread(T* this);
};

#define DECL_THREAD(X) static inline thread* get_thread(X* this) { return &this->t; } void main(X* this);

forall( dtype T | sized(T) | is_thread(T) )
static inline coroutine* get_coroutine(T* this) {
	return &get_thread(this)->c;
}

static inline coroutine* get_coroutine(thread* this) {
	return &this->c;
}

thread * this_thread(void);

//-----------------------------------------------------------------------------
// Ctors and dtors
void ?{}(thread* this);
void ^?{}(thread* this);

//-----------------------------------------------------------------------------
// thread runner
// Structure that actually start and stop threads
forall( dtype T | sized(T) | is_thread(T) )
struct scoped {
	T handle;
};

forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
void ?{}( scoped(T)* this );

forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
void ?{}( scoped(T)* this, P params );

forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } )
void ^?{}( scoped(T)* this );

void yield();

#endif //THREADS_H

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