view main.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 35d0358b3fe6
children
line wrap: on
line source

/*
** Dining Philosophers Problem's scheduler
*/
#include "dpp.h"
__code die(char *err);
__code init_fork2(PhilsPtr self, int count, int id);

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

void *env;

PhilsPtr phils_list = NULL;

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

int main(void)
{
    env = _CbC_environment;

    goto init_fork1(NUM_PHILOSOPHER);
}

/* end */