view dpp2.cbc @ 12:7f2db1e1bf2f default tip

use CBC_COMPILER environment val
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Thu, 04 Jul 2019 18:53:38 +0900
parents 4a68716b7488
children
line wrap: on
line source

/*
** Program: Dining Philosophors Problem
** Author : Atsuki Shimoji
*/

#include "dpp2.h"
#include "queue.h"
#include "scheduler.h"

__code putdown_lfork(PhilsPtr self, TaskPtr current_task)
{
	printf("%d: putdown_lfork:%d\n", self->id, self->left_fork->id);
	self->left_fork->owner = NULL;
	self->next = Thinking;
	goto scheduler(self, current_task);
}

__code putdown_rfork(PhilsPtr self, TaskPtr current_task)
{
	printf("%d: putdown_rfork:%d\n", self->id, self->right_fork->id);
	self->right_fork->owner = NULL;
	self->next = PutDownLeftFork;
	goto scheduler(self, current_task);
}

__code eating(PhilsPtr self, TaskPtr current_task)
{
	printf("%d: eating\n", self->id);
	self->next = PutDownRightFork;
	goto scheduler(self, current_task);
}

/* waiting for right fork */
__code hungry2(PhilsPtr self, TaskPtr current_task)
{
	printf("%d: hungry2\n", self->id);
	self->next = PickUpRightFork;
	goto scheduler(self, current_task);
}

/* waiting for left fork */
__code hungry1(PhilsPtr self, TaskPtr current_task)
{
	printf("%d: hungry1\n", self->id);
	self->next = PickUpLeftFork;
	goto scheduler(self, current_task);
}

__code pickup_rfork(PhilsPtr self, TaskPtr current_task)
{
	if (self->right_fork->owner == NULL) {
		printf("%d: pickup_rfork:%d\n", self->id, self->right_fork->id);
		self->right_fork->owner = self;
		self->next = Eating;
		goto scheduler(self, current_task);
	} else {
		self->next = WaitRightFork;
		goto scheduler(self, current_task);
	}
}

__code pickup_lfork(PhilsPtr self, TaskPtr current_task)
{
	if (self->left_fork->owner == NULL) {
		printf("%d: pickup_lfork:%d\n", self->id, self->left_fork->id);
		self->left_fork->owner = self;
		self->next = PickUpRightFork;
		goto scheduler(self, current_task);
	} else {
		self->next = WaitRightFork;
		goto scheduler(self, current_task);
	}
}

__code thinking(PhilsPtr self, TaskPtr current_task)
{
	printf("%d: thinking\n", self->id);
	self->next = WaitLeftFork;
	goto scheduler(self, current_task);
}

/* end */