view scheduler.cbc @ 10:35d0358b3fe6

update Modern CbC Compiler
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 01 Jul 2019 22:18:03 +0900
parents 4a68716b7488
children 190dadd8405b
line wrap: on
line source

/*
** Dining Philosophers Problem's scheduler
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "dpp2.h"
#include "queue.h"

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

void *env;

__code (*get_next_task)(TaskPtr);
PhilsPtr phils_list = NULL;
int id = 1;    /* This is a counter variable to detect a process. */

static int max_step = 100;

/* 環状リストでもlengthを返す */
int list_length(TaskPtr list)
{
	int length;
	TaskPtr q;
	if (!list) return 0;
	q = list->next;

	for (length = 1; q && q != list; length++) {
	    q = q->next;
	}
	return length;
}

TaskPtr get_task(int num, TaskPtr list)
{
    while (num-- > 0) {
	list = list->next;
    }
    return list;
}

__code get_next_task_random(TaskPtr list)
{
	TaskPtr t = list;
	TaskPtr e;

	if (max_step--<0) goto die("Simuration end.");

	// list = list->next;
	list = get_task((random()%list_length(list)+1), list);
	goto do_action(list->phils,list);
}

__code do_action(PhilsPtr phils, TaskPtr list)
{
    switch (phils->next) {
        case PutDownLeftFork:
            goto putdown_lfork(phils, list);
        case PutDownRightFork:
            goto putdown_rfork(phils, list);
        case PickUpLeftFork:
            goto pickup_lfork(phils, list);
        case PickUpRightFork:
            goto pickup_rfork(phils, list);
        case Thinking:
            goto thinking(phils, list);
        case Eating:
            goto eating(phils, list);
        case WaitRightFork:
            goto hungry2(phils, list);
        case WaitLeftFork:
            goto hungry1(phils, list);
        default:
            printf("invalid action\n");
            exit(1);
    }
    __code (*action) (PhilsPtr, TaskPtr) = thinking;
    goto action(phils, list);
}

__code get_next_task_fifo(TaskPtr list)
{
	TaskPtr t = list;
	TaskPtr e;

	if (max_step--<0) goto die("Simuration end.");

	list = list->next;
	goto do_action(list->phils,list);
}

__code scheduler(PhilsPtr phils, TaskPtr list)
{
	goto get_next_task(list);
}

__code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last);

__code task_entry2(int count,PhilsPtr self, TaskPtr list,TaskPtr last, TaskPtr q)
{
	if (!q) {
		goto die("Can't allocate Task\n");
	} else {
		goto enqueue(count, self, list, last, q, task_entry1);
	}
}

__code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last)
{
printf("int count %d, PhilsPtr self %p, TaskPtr list %p, TaskPtr last %p\n", count, self, list, last);

	if (count++ < NUM_PHILOSOPHER) {
		self = self->left;
		goto create_queue(count,self,list,last,task_entry2);
	} else {
		last->next = list;
		goto scheduler(list->phils,list);
	}
}

__code task_entry0(int count, PhilsPtr self, TaskPtr list, TaskPtr last, TaskPtr q)
{
	goto task_entry1(count, self, q, q);
}

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

	goto create_queue(1, self, 0, 0, task_entry0);
}

__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);
    exit(1);
}

int main(int argc, char **argv)
{
	env = _CbC_environment;
	// srand((unsigned)time(NULL));
	// srandom((unsigned long)time(NULL));
	get_next_task = get_next_task_fifo;
	if (argc == 2) {
	    if (argv[1][0] == '-') {
	        if (argv[1][1] == 'r') {
		    printf("select random\n");
	            get_next_task = get_next_task_random;
	            srandom(time(NULL));
                } else {
		    printf("FIFO\n");
                    get_next_task = get_next_task_fifo;
	        }
	    }
	}

	goto init_fork1(NUM_PHILOSOPHER);
}

/* end */