view src/parallel_execution/examples/DPP2/PhilsImpl.cbc @ 875:9f1b993f5283

fix
author ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
date Sat, 23 Jan 2021 18:17:35 +0900
parents 504a40546808
children 8c89ee7dd6b3
line wrap: on
line source

#include "../../../context.h"
#include <stdio.h>
#impl "Phils.h" for "PhilsImpl.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_lforkPhilsImpl;
    phils->putdown_rfork = C_putdown_rforkPhilsImpl;
    phils->eating = C_eatingPhilsImpl;
    phils->pickup_rfork = C_pickup_rforkPhilsImpl;
    phils->pickup_lfork = C_pickup_lforkPhilsImpl;
    phils->thinking = C_thinkingPhilsImpl;
    return phils;
}

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

}

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

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

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

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

}

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