view DPP/main.cbc @ 33:3946f8d26710 draft default tip

add benchmarck/binary-trees
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 09 Apr 2013 16:41:30 +0900
parents 6695c97470f3
children
line wrap: on
line source

/*
** Dining Philosophers Problem's scheduler
*/
#include "dpp.h"

#define NUM_PHILOSOPHER 5    /* A number of philosophers must be more than 2. */

code (*ret)(int,void*);
void *env;

#define __environment _CbC_environment
#define __return _CbC_return

PhilsPtr phils_list = NULL;

code run(PhilsPtr self);
code init_final(PhilsPtr self);
code init_phils2(PhilsPtr self, int count, int id);
code init_fork2(PhilsPtr self, int count, int id);
code init_phils1(ForkPtr fork, int count, int id);
code init_fork1(int count);
code die(char *err);


code run(PhilsPtr self)
{
	goto thinking(self);
}

code init_final(PhilsPtr self)
{
	self->right = phils_list;
	self->right_fork = phils_list->left_fork;
	printf("init all\n");

	goto run(phils_list);
}

code init_phils2(PhilsPtr self, int count, int id)
{
	PhilsPtr tmp_self;

	tmp_self = (PhilsPtr)malloc(sizeof(Phils));
	if (!tmp_self) {
		goto die("Can't allocate Phils\n");
	}
	self->right = tmp_self;
	tmp_self->id = id;
	tmp_self->right_fork = NULL;
	tmp_self->left_fork = self->right_fork;
	tmp_self->right = NULL;
	tmp_self->left = self;
	tmp_self->next = thinking;

	count--;
	id++;

	if (count == 0) {
		goto init_final(tmp_self);
	} else {
		goto init_fork2(tmp_self, count, id);
	}
}

code init_fork2(PhilsPtr self, int count, int id)
{
	ForkPtr tmp_fork;

	tmp_fork = (ForkPtr)malloc(sizeof(Fork));
	if (!tmp_fork) {
		goto die("Can't allocate Fork\n");
	}
	tmp_fork->id = id;
	tmp_fork->owner = NULL;
	self->right_fork = tmp_fork;

	goto init_phils2(self, count, id);
}

code init_phils1(ForkPtr fork, int count, int id)
{
	PhilsPtr self;

	self = (PhilsPtr)malloc(sizeof(Phils));
	if (!self) {
		goto die("Can't allocate Phils\n");
	}
	phils_list = self;
	self->id = id;
	self->right_fork = NULL;
	self->left_fork = fork;
	self->right = NULL;
	self->left = NULL;
	self->next = thinking;

	count--;
	id++;

	goto init_fork2(self, count, id);
}

code init_fork1(int count)
{
	ForkPtr fork;
	int id = 1;

	fork = (ForkPtr)malloc(sizeof(Fork));
	if (!fork) {
		goto die("Can't allocate Fork\n");
	}
	fork->id = id;
	fork->owner = NULL;

	goto init_phils1(fork, count, id);
}

code die(char *err)
{
	printf("%s\n", err);
	//	goto ret(1), env;
	goto ret(1, env);
}

int main(void)
{
	ret = __return;
	env = __environment;

	goto init_fork1(NUM_PHILOSOPHER);
}

/* end */