view src/parallel_execution/examples/DPP2/PhilsImpl.cbc @ 845:feb874340610

use local variable
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Thu, 21 Jan 2021 17:01:16 +0900
parents 49408bce514c
children 348d6f20134e
line wrap: on
line source

#include "../../../context.h"
#include <stdio.h>
#interface "Phils.h"
#interface "Fork.h"
#interface "Worker.h"
#interface "AtomicT_int.h"
#interface "TaskManager.h"


// ----
// typedef struct PhilsImpl <Self, Isa> impl Phils {
//   __code next(...);
//   atomic Leftfork;
//   atomic Rightfork;
//   int self;
// } PhilsImpl;
// ----

Phils* createPhilsImpl(struct Context* context, int id, AtomicT_int* right, AtomicT_int* left) {
    struct Phils* phils  = new Phils();
    struct PhilsImpl* phils_impl = new PhilsImpl();
    phils->phils = (union Data*)phils_impl;
    phils_impl->Leftfork = left;
    phils_impl->Rightfork = right;
    phils_impl->self = id;
    phils->putdown_lfork = C_putdown_lfork_PhilsImpl;
    phils->putdown_rfork = C_putdown_rfork_PhilsImpl;
    phils->eating = C_eating_PhilsImpl;
    phils->pickup_rfork = C_pickup_rfork_PhilsImpl;
    phils->pickup_lfork = C_pickup_lfork_PhilsImpl;
    phils->thinking = C_thinking_PhilsImpl;
    return phils;
}

__code putdown_lfork_PhilsImpl(struct PhilsImpl* phils, __code next(...)) {
    struct AtomicT_int* left_fork = phils->Leftfork;
    goto left_fork->set(-1, putdown_rfork_PhilsImpl);

}

__code putdown_rfork_PhilsImpl(struct PhilsImpl* phils, __code next(...)) {
    struct AtomicT_int* right_fork = phils->Rightfork;
    goto right_fork->set(-1, putdown_lfork_PhilsImpl);
}

__code thinking_PhilsImpl(struct PhilsImpl* phils, struct Fork* fork, __code next(...)) {
    printf("%d: thinking\n", phils->self);
    goto pickup_lfork(phils->self);
}

__code pickup_rfork_PhilsImpl(struct PhilsImpl* phils, __code next(...)) {
    struct AtomicT_int* right_fork = phils->Rightfork;
    goto right_fork->checkAndSet(&phils->id, -1, pickup_lfork, pickup_rfork);
}

__code pickup_lfork_PhilsImpl(struct PhilsImpl* phils, __code next(...)) {
    struct AtomicT_int* left_fork = phils->Leftfork;
    goto left_fork->checkAndSet(id, -1, pickup_rfork, eating);

}

__code eating_PhilsImpl(struct PhilsImpl* phils, __code next(...)) {
    printf("%d: eating\n", phils->self);
    goto putdown_rfork();
}