changeset 8:a15437a1e94c

Fix tableau3
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Fri, 25 Dec 2015 18:41:52 +0900
parents 171cc032eb29
children cef74c1054c1
files .hgignore dpp3.cbc dpp3.h dpp_common.h tableau3.cbc
diffstat 5 files changed, 117 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Fri Dec 25 18:31:20 2015 +0900
+++ b/.hgignore	Fri Dec 25 18:41:52 2015 +0900
@@ -8,3 +8,4 @@
 dpp2
 tableau
 tableau2
+tableau3
--- a/dpp3.cbc	Fri Dec 25 18:31:20 2015 +0900
+++ b/dpp3.cbc	Fri Dec 25 18:41:52 2015 +0900
@@ -11,7 +11,7 @@
     //printf("%d: putdown_lfork:%d\n", self->id, self->left_fork->id);
     self->right_fork->owner = NULL;
     self->left_fork->owner = NULL;
-    self->next = pickup_lfork;
+    self->next = PickUpLeftFork;
     goto scheduler(self, current_task);
 }
 
@@ -38,10 +38,10 @@
     if (self->right_fork->owner == NULL) {
 	//printf("%d: pickup_rfork:%d\n", self->id, self->right_fork->id);
 	self->right_fork->owner = self;
-	self->next = putdown_fork;
+	self->next = PutDownFork;
 	goto scheduler(self, current_task);
     } else {
-	self->next = pickup_rfork;
+	self->next = PickUpRightFork;
 	goto scheduler(self, current_task);
     }
 }
@@ -51,11 +51,40 @@
     if (self->left_fork->owner == NULL) {
 	//printf("%d: pickup_lfork:%d\n", self->id, self->left_fork->id);
 	self->left_fork->owner = self;
-	self->next = pickup_rfork;
+	self->next = PickUpRightFork;
 	goto scheduler(self, current_task);
     } else {
-	self->next = pickup_lfork;
+	self->next = PickUpLeftFork;
 	goto scheduler(self, current_task);
     }
 }
+
+__code eating(PhilsPtr self, TaskPtr current_task)
+{
+	printf("%d: eating\n", self->id);
+	self->next = PutDownRightFork;
+	goto scheduler(self, current_task);
+}
+
+__code putdown_lfork(PhilsPtr self, TaskPtr current_task)
+{
+	printf("%d: putdown_lfork:%d\n", self->id, self->left_fork->id);
+	self->left_fork->owner = NULL;
+	self->next = Thinking;
+	goto scheduler(self, current_task);
+}
+
+__code putdown_rfork(PhilsPtr self, TaskPtr current_task)
+{
+	printf("%d: putdown_rfork:%d\n", self->id, self->right_fork->id);
+	self->right_fork->owner = NULL;
+	self->next = PutDownLeftFork;
+	goto scheduler(self, current_task);
+}
+__code thinking(PhilsPtr self, TaskPtr current_task)
+{
+	printf("%d: thinking\n", self->id);
+	self->next = WaitLeftFork;
+	goto scheduler(self, current_task);
+}
 /* end */
--- a/dpp3.h	Fri Dec 25 18:31:20 2015 +0900
+++ b/dpp3.h	Fri Dec 25 18:41:52 2015 +0900
@@ -1,14 +1,19 @@
 #ifndef _DPP3_H_
 #define _DPP3_H_
-#define NULL (0)
 
 #include "dpp_common.h"
 #include "queue.h"
 #include "scheduler.h"
 
-extern __code putdown_lfork(PhilsPtr self, struct task * current_task);
-extern __code putdown_rfork(PhilsPtr self, struct task * current_task);
+extern __code putdown_fork(PhilsPtr self, TaskPtr current_task);
+extern __code putdown_lfork(PhilsPtr self, TaskPtr current_task);
+extern __code putdown_rfork(PhilsPtr self, TaskPtr current_task);
 extern __code pickup_rfork(PhilsPtr self, TaskPtr current_task);
 extern __code pickup_lfork(PhilsPtr self, TaskPtr current_task);
 
+extern __code eating(PhilsPtr self, TaskPtr current_task);
+extern __code hungry2(PhilsPtr self, TaskPtr current_task);
+extern __code hungry1(PhilsPtr self, TaskPtr current_task);
+extern __code thinking(PhilsPtr self, TaskPtr current_task);
+
 #endif
--- a/dpp_common.h	Fri Dec 25 18:31:20 2015 +0900
+++ b/dpp_common.h	Fri Dec 25 18:41:52 2015 +0900
@@ -13,6 +13,7 @@
     PickUpLeftFork,
     PickUpRightFork,
     Thinking,
+    PutDownFork
 };
 
 typedef struct phils {
--- a/tableau3.cbc	Fri Dec 25 18:31:20 2015 +0900
+++ b/tableau3.cbc	Fri Dec 25 18:41:52 2015 +0900
@@ -2,7 +2,7 @@
 ** Dining Philosophers Problem's scheduler
 **    with state developper as a tableau method
 
-** 連絡先: 琉球大学情報工学科 河野 真治  
+** 連絡先: 琉球大学情報工学科 河野 真治
 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
 **
 **    このソースのいかなる複写,改変,修正も許諾します。ただし、
@@ -12,10 +12,10 @@
 **    バイナリの配布の際にはversion messageを保存することを条件とします。
 **    このプログラムについては特に何の保証もしない、悪しからず。
 **
-**    Everyone is permitted to do anything on this program 
+**    Everyone is permitted to do anything on this program
 **    including copying, modifying, improving,
 **    as long as you don't try to pretend that you wrote it.
-**    i.e., the above copyright notice has to appear in all copies.  
+**    i.e., the above copyright notice has to appear in all copies.
 **    Binary distribution requires original version messages.
 **    You don't have to ask before copying, redistribution or publishing.
 **    THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
@@ -23,7 +23,7 @@
 */
 #include <stdlib.h>
 #include <time.h>
-#include "dpp2.h"
+#include "dpp3.h"
 #include "queue.h"
 #include "memory.h"
 #include "state_db.h"
@@ -31,7 +31,6 @@
 
 int NUM_PHILOSOPHER = 5;    /* A number of philosophers must be more than 2. */
 
-static code (*ret)(int);
 static void *env;
 
 static PhilsPtr phils_list = NULL;
@@ -43,6 +42,36 @@
 static StateNode st;
 static int always_flag;  // for LTL
 
+
+//FIXME
+__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);
+        case PutDownFork:
+            goto putdown_fork(phils, list);
+        default:
+            printf("invalid action¥n");
+            exit(1);
+    }
+    __code (*action) (PhilsPtr, TaskPtr) = thinking;
+    goto action(phils, list);
+}
 int
 list_length(TaskPtr list)
 {
@@ -81,7 +110,7 @@
  */
 
 
-code tableau(TaskPtr list)
+__code tableau(TaskPtr list)
 {
     StateDB out;
 
@@ -97,7 +126,7 @@
 		printf("Number of unique states %d\n", state_count());
 		memory_usage();
 		show_result(always_flag);
-		goto ret(0),env;
+        exit(0);
 	    }
 	    //printf("no more branch %d\n",count);
 	    depth--;
@@ -116,10 +145,11 @@
     }
     //printf("depth %d count %d\n", depth, count++);
     count++;
-    goto list->phils->next(list->phils,list);
+
+    goto do_action(list->phils, list);
 }
 
-code get_next_task_fifo(TaskPtr list)
+__code get_next_task_fifo(TaskPtr list)
 {
     TaskPtr t = list;
     TaskPtr e;
@@ -127,18 +157,18 @@
     if (max_step--<0) goto die("Simuration end.");
 
     list = list->next;
-    goto list->phils->next(list->phils,list);
+    goto do_action(list->phils, list);
 }
 
-code scheduler(PhilsPtr phils, TaskPtr list)
+__code scheduler(PhilsPtr phils, TaskPtr list)
 {
     goto check(&always_flag, phils, list);
     // goto next_next_task_fifo(list);
 }
 
-code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last);
+__code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last);
 
-code task_entry2(int count,PhilsPtr self, TaskPtr list,TaskPtr last, TaskPtr q)
+__code task_entry2(int count,PhilsPtr self, TaskPtr list,TaskPtr last, TaskPtr q)
 {
     if (!q) {
 	goto die("Can't allocate Task\n");
@@ -148,7 +178,7 @@
     }
 }
 
-code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last)
+__code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last)
 {
     StateDB out;
     /*
@@ -167,17 +197,17 @@
 	lookup_StateDB(&st, &state_db, &out);
 	task_iter = create_task_iterator(list,out,0);
 	// start first task
-	goto list->phils->next(list->phils,list);
+    goto do_action(list->phils, list);
     }
 }
 
-code task_entry0(int count, PhilsPtr self, TaskPtr list, TaskPtr last, TaskPtr q)
+__code task_entry0(int count, PhilsPtr self, TaskPtr list, TaskPtr last, TaskPtr q)
 {
     add_memory_range(q,sizeof(Task),&mem);
     goto task_entry1(count, self, q, q);
 }
 
-code init_final(PhilsPtr self)
+__code init_final(PhilsPtr self)
 {
     self->right = phils_list;
     phils_list->left = self;
@@ -189,7 +219,7 @@
     goto create_queue(1, self, 0, 0, task_entry0);
 }
 
-code init_phils2(PhilsPtr self, int count, int id)
+__code init_phils2(PhilsPtr self, int count, int id)
 {
     PhilsPtr tmp_self;
 
@@ -203,7 +233,7 @@
     tmp_self->left_fork = self->right_fork;
     tmp_self->right = NULL;
     tmp_self->left = self;
-    tmp_self->next = pickup_lfork;
+    tmp_self->next = PickUpLeftFork;
     add_memory_range(tmp_self,sizeof(Phils),&mem);
 
     count--;
@@ -216,7 +246,7 @@
     }
 }
 
-code init_fork2(PhilsPtr self, int count, int id)
+__code init_fork2(PhilsPtr self, int count, int id)
 {
     ForkPtr tmp_fork;
 
@@ -232,7 +262,7 @@
     goto init_phils2(self, count, id);
 }
 
-code init_phils1(ForkPtr fork, int count, int id)
+__code init_phils1(ForkPtr fork, int count, int id)
 {
     PhilsPtr self;
 
@@ -246,7 +276,7 @@
     self->left_fork = fork;
     self->right = NULL;
     self->left = NULL;
-    self->next = pickup_lfork;
+    self->next = PickUpLeftFork;
     add_memory_range(self,sizeof(Phils),&mem);
 
     count--;
@@ -255,7 +285,7 @@
     goto init_fork2(self, count, id);
 }
 
-code init_fork1(int count)
+__code init_fork1(int count)
 {
     ForkPtr fork;
     int id = 1;
@@ -271,16 +301,15 @@
     goto init_phils1(fork, count, id);
 }
 
-code die(char *err)
+__code die(char *err)
 {
     printf("%s\n", err);
-    goto ret(1), env;
+    exit(1);
 }
 
 int main(int ac, char *av[])
 {
-    ret = return;
-    env = environment;
+    env = __environment;
     // srand((unsigned)time(NULL));
     // srandom((unsigned long)time(NULL));
     reset_state_count();
@@ -290,7 +319,7 @@
 	NUM_PHILOSOPHER = atoi(av[1]);
 	if (NUM_PHILOSOPHER >10 ||NUM_PHILOSOPHER < 2) {
 	    printf("illegal number of philosopher = %d\n", NUM_PHILOSOPHER );
-	    return 1; 
+	    return 1;
 	}
 	//printf("number of philosopher = %d\n", NUM_PHILOSOPHER );
     }
@@ -298,4 +327,20 @@
     goto init_fork1(NUM_PHILOSOPHER);
 }
 
+/* waiting for right fork */
+__code hungry2(PhilsPtr self, TaskPtr current_task)
+{
+	printf("%d: hungry2\n", self->id);
+	self->next = PickUpRightFork;
+	goto scheduler(self, current_task);
+}
+
+/* waiting for left fork */
+__code hungry1(PhilsPtr self, TaskPtr current_task)
+{
+	printf("%d: hungry1\n", self->id);
+	self->next = PickUpLeftFork;
+	goto scheduler(self, current_task);
+}
+
 /* end */