view DPP/dpp.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 8bf48c0cb198
children
line wrap: on
line source

/*
** Program: Dining Philosophors Problem
** Author : Atsuki Shimoji
** Note   : This is a single running program.
*/
#include <stdio.h>
#include "dpp.h"

code putdown_lfork(PhilsPtr self)
{
	printf("%d: putdown_lfork:%d\n", self->id, self->left_fork->id);
	self->left_fork->owner = NULL;
	goto thinking(self);
}

code putdown_rfork(PhilsPtr self)
{
	printf("%d: putdown_rfork:%d\n", self->id, self->right_fork->id);
	self->right_fork->owner = NULL;
	goto putdown_lfork(self);
}

code eating(PhilsPtr self)
{
	printf("%d: eating\n", self->id);
	goto putdown_rfork(self);
}

/* waiting for right fork */
code hungry2(PhilsPtr self)
{
	printf("%d: hungry2\n", self->id);
	goto pickup_rfork(self);
}

/* waiting for left fork */
code hungry1(PhilsPtr self)
{
	printf("%d: hungry1\n", self->id);
	goto pickup_lfork(self);
}

code pickup_rfork(PhilsPtr self)
{
	if (self->right_fork->owner == NULL) {
		printf("%d: pickup_rfork:%d\n", self->id, self->right_fork->id);
		self->right_fork->owner = self;
		goto eating(self);
	} else {
		goto hungry2(self);
	}
}

code pickup_lfork(PhilsPtr self)
{
	if (self->left_fork->owner == NULL) {
		printf("%d: pickup_lfork:%d\n", self->id, self->left_fork->id);
		self->left_fork->owner = self;
		goto pickup_rfork(self);
	} else {
		goto hungry1(self);
	}
}

code thinking(PhilsPtr self)
{
	printf("%d: thinking\n", self->id);
	goto hungry1(self);
}