// ************************************************************************** //
//                                                                            //
//    eses                   eses                                             //
//   eses                     eses                                            //
//  eses    eseses  esesese    eses   Embedded Systems Group                  //
//  ese    ese  ese ese         ese                                           //
//  ese    eseseses eseseses    ese   Department of Computer Science          //
//  eses   eses          ese   eses                                           //
//   eses   eseses  eseseses  eses    University of Kaiserslautern            //
//    eses                   eses                                             //
//                                                                            //
// ************************************************************************** //

package DiningPhilosophers;

module Philosopher (
    /* basic state transitions */
    event bool ? trigger_hungry,
    event bool ? done,
    
    /* request and gain access */
    event bool ! request_left_fork,
    event bool ! request_right_fork,
    event bool ? gain_access_left_fork,
    event bool ? gain_access_right_fork,
    event bool ? release_forks,
    
    /* gain and release for access */
    event bool ! take_left_fork,
    event bool ! take_right_fork,
    event bool ! release_left_fork,
    event bool ! release_right_fork
) {
    loop {
    
        thinking: await (trigger_hungry); // state: thinking
        
        do {
            bool has_left, has_right;
            abort {
                {
                    emit (request_left_fork);
                    hungry1: await (gain_access_left_fork); // state: hungry 1
                    emit (take_left_fork);
                    has_left = true;
                } || {
                    emit (request_right_fork);
                    hungry2: await (gain_access_right_fork); // state: hungry 2
                    emit (take_right_fork);
                    has_right = true;
                }
            } when (release_forks);
            
            if (release_forks) {
                if (has_left)
                    emit (release_left_fork);
                if (has_right)
                    emit (release_right_fork);
            }
        } while (release_forks);
        
        eating: await (done); // state: eating
        
        emit (release_left_fork);
        emit (release_right_fork);
    }
}