changeset 36:240c045ebab2

Add normal queue
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Fri, 15 May 2015 19:17:23 +0900
parents 2bdd8e2118c7
children a1fc4c670150
files src/CMakeLists.txt src/synchronizedQueue/synchronizedQueue.c src/synchronizedQueue/synchronizedQueueContext.c src/synchronizedQueue/synchronizedQueueContext.h
diffstat 4 files changed, 122 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/src/CMakeLists.txt	Tue May 05 21:16:04 2015 +0900
+++ b/src/CMakeLists.txt	Fri May 15 19:17:23 2015 +0900
@@ -14,3 +14,4 @@
 add_subdirectory(allocate)
 add_subdirectory(list)
 add_subdirectory(llrb)
+add_subdirectory(synchronizedQueue)
--- a/src/synchronizedQueue/synchronizedQueue.c	Tue May 05 21:16:04 2015 +0900
+++ b/src/synchronizedQueue/synchronizedQueue.c	Fri May 15 19:17:23 2015 +0900
@@ -1,21 +1,15 @@
+#include <stdlib.h>
+#include <stdio.h>
+
 #include "synchronizedQueueContext.h"
 
+#include "allocate.h"
 #include "origin_cs.h"
 
-#ifdef CLANG
-#define _CbC_retrun __return
-#define _CbC_environment __environment
-#endif
-
-#define NUM 100
-#define ALLOCATE_SIZE 1000000000
-
 extern __code initSynchronizedQueueContext(struct Context* context);
 
-static int num;
-
 __code code1(struct Context* context) {
-    context->data[Allocate]->allocate.size = sizeof(long);
+    context->data[Allocate]->allocate.size = sizeof(struct Element);
     context->data[Allocate]->allocate.next = Code2;
     goto meta(context, Allocator);
 }
@@ -24,63 +18,85 @@
     goto (context->code[next])(context);
 }
 
+__code meta_code2(struct Context* context, enum Code next) {
+    context->data[Queue]->queue.head = context->data[context->dataNum];
+    context->data[Queue]->queue.tail = context->data[Queue]->queue.head;
+    context->data[context->dataNum]->element.next = 0;
+    goto (context->code[next])(context);
+}
+
 __code code2(struct Context* context) {
-    context->data[1]->count = 0;
-    goto meta(context, Code3);
+    context->data[context->dataNum] -> element.value = 1024;
+    context->data[Allocate]->allocate.size = sizeof(struct Element);
+    context->data[Allocate]->allocate.next = Code3;
+    goto meta_code2(context, Allocator);
 }
 
 __code code3(struct Context* context) {
-    struct Allocate* allocate = &context->data[Allocate]->allocate;
-    long loop = context->data[1]->count;
-    if (loop == num) {
-        goto meta(context, Code4);
-    }
-    allocate->size = sizeof(struct Node);
-    allocate->after_put = Code3;
+    context->data[Allocate]->allocate.after_put = Code4;
+    context->data[context->dataNum] -> element.value = 10;
+    goto meta(context, Put);
+}
 
-    context->data[1]->count++;
-    goto meta(context, Put);
+__code meta_put(struct Context* context, enum Code next) {
+    context->data[Queue]->queue.tail->element.next = context->data[context->dataNum];
+    context->data[Queue]->queue.tail               = context->data[Queue]->queue.tail->element.next;
+    context->data[Queue]->queue.tail->element.next = 0;
+    goto (context->code[next])(context);
+}
+
+__code put(struct Context* context) {
+    goto meta_put(context, context->data[Allocate]->allocate.after_put);
 }
 
 
-__code put(struct Context* context) {
-    struct Allocate* allocate = &context->data[Allocate]->allocate;
-
-    // sem_p
-    pthread_mutext_lock(&allocate->queue_remain->mutex);
-    while(sem->value == 0) {
-        pthread_cond_wait(&allocate->queue_remain->cond, &allocate->queue_remain->mutex);
+__code meta_traverse(struct Context* context, enum Code next) {
+    printf("current value in queue is %d\n", context->data[Queue]->queue.current->element.value);
+    if (context->data[Queue]->queue.current->element.next) {
+        context->data[Queue]->queue.current = context->data[Queue]->queue.current->element.next;
+        goto meta_traverse(context, next);
     }
-    allocate->queue_remain->value--;
-    pthread_mutext_unlock(&allocate->queue_remain->mutex);
-    
-    struct ListContext* list_context = context->data[Allocate]-> list_context;
-    list_context->data[Allocate]->value = context->data[1]->count;
+    goto (context->code[next])(context);
+}
 
-    // sem_v
-    pthread_mutext_lock(&allocate->queue_count->mutex);
-    allocate->queue_count->value++;
-    pthare_cond_signal(&allocate->queue_count->cond);
-    pthread_mutext_unlock(&allocate->queue_count->mutex);
+__code traverse(struct Context* context) {
+    context->data[Queue]->queue.current = context->data[Queue]->queue.head;
+    goto meta_traverse(context, context->data[Allocate]->allocate.after_traverse);
+}
 
-    goto metaPut(context, );
+__code code4(struct Context* context) {
+    context->data[Allocate]->allocate.after_traverse = Code5;
+    goto meta(context, Traverse);
 }
 
-__code metaPut(struct Context* context, enum Code next){
-}
 
-__code take(struct Context* context) {
+__code code5(struct Context* context) {
+    context->data[Allocate]->allocate.after_get = Code6;
+    goto meta(context, Get);
 }
 
-__code cas(struct Context* context, enum Code next) {
+__code meta_get(struct Context* context, enum Code next) {
+    if (context->data[Queue]->queue.head == context->data[Queue]->queue.tail) {
+        printf("queue is empty\n");
+        goto (context->code[Exit])(context);
+    }
+    context->data[Queue]->queue.head = (context->data[Queue]->queue.head->element.next) ? context->data[Queue]->queue.head->element.next : 0;
+    goto (context->code[next])(context);
 }
 
-int main(int argc, char const* argv[]) {
-    num = (int)atoi(argv[1]);
-    struct Context* context = (struct Context*)malloc(sizeof(struct Context);
-    context->code = malloc(sizeof(__code*)*ALLOCATE_SIZE);
-    context->data = malloc(sizeof(union Data**)*ALLOCATE_SIZE);
-    context->heap = malloc(sizeof(union Data*)*ALLOCATE_SIZE);
+__code get(struct Context* context) {
+    goto meta_get(context, context->data[Allocate]->allocate.after_get);
+}
+
+__code code6(struct Context* context) {
+    printf("after delete\n");
+    context->data[Allocate]->allocate.after_traverse = Exit;
+    goto meta(context, Traverse);
+}
+
+
+int main() {
+    struct Context* context = (struct Context*)malloc(sizeof(struct Context));
     initSynchronizedQueueContext(context);
     goto start_code(context, Code1);
 }
--- a/src/synchronizedQueue/synchronizedQueueContext.c	Tue May 05 21:16:04 2015 +0900
+++ b/src/synchronizedQueue/synchronizedQueueContext.c	Fri May 15 19:17:23 2015 +0900
@@ -1,31 +1,46 @@
+#include <stdlib.h>
+
 #include "synchronizedQueueContext.h"
 
 extern __code code1(struct Context*);
 extern __code code2(struct Context*);
 extern __code code3(struct Context*);
+extern __code code4(struct Context*);
+extern __code code5(struct Context*);
+extern __code code6(struct Context*);
 extern __code meta(struct Context*);
 extern __code allocate(struct Context*);
 extern __code put(struct Context*);
-extern __code take(struct Context*);
+extern __code traverse(struct Context*);
+extern __code get(struct Context*);
 extern __code exit_code(struct Context*);
 
 __code initSynchronizedQueueContext(struct Context* context) {
-    context->codeSize = 3;
+    context->dataSize   = sizeof(union Data)*ALLOCATE_SIZE;
+    context->code       = malloc(sizeof(__code*)*ALLOCATE_SIZE);
+    context->data       = malloc(sizeof(union Data*)*ALLOCATE_SIZE);
+    context->heap_start = malloc(context->dataSize);
+
+    context->codeNum         = Exit;
     context->code[Code1]     = code1;
     context->code[Code2]     = code2;
     context->code[Code3]     = code3;
+    context->code[Code4]     = code4;
+    context->code[Code5]     = code5;
+    context->code[Code6]     = code6;
     context->code[Allocator] = allocate;
     context->code[Put]       = put;
-    context->code[Take]      = take;
+    context->code[Traverse]  = traverse;
+    context->code[Get]       = get;
     context->code[Exit]      = exit_code;
-    context->dataSize        = 0;
-    context->heap           += sizeof(struct Allocate);
-    context->data[Allocate]->list_context = malloc(sizeof(union Data**)*ALLOCATE_SIZE);
-    initListContext(context->data[Allocate]->list_context);
+
+    context->heap = context->heap_start;
+
+    context->data[Allocate] = context->heap;
+    context->heap          += sizeof(struct Allocate);
+
+    context->data[Queue]    = context->heap;
+    context->heap          += sizeof(struct Queue);
+
+    context->dataNum = Queue;
 }
-
-__code initListContext(struct ListContext* context) {
-    context->data     = malloc(sizeof(union ListData **)*ALLOCATE);
-    context->heap     = malloc(sizeof(union ListData *)*ALLOCATE);
-    context->dataSize = 0;
-}
--- a/src/synchronizedQueue/synchronizedQueueContext.h	Tue May 05 21:16:04 2015 +0900
+++ b/src/synchronizedQueue/synchronizedQueueContext.h	Fri May 15 19:17:23 2015 +0900
@@ -1,61 +1,54 @@
-/* Context definition for synchronized queue example */
+/* Context definition for  list example */
 
-#include <pthread.h>
+#define ALLOCATE_SIZE 100
 
 enum Code {
     Code1,
     Code2,
     Code3,
+    Code4,
+    Code5,
+    Code6,
     Allocator,
     Put,
-    Take,
+    Traverse,
+    Get,
     Exit,
 };
 
 enum UniqueData {
     Allocate,
+    Queue,
 };
 
 struct Context {
-    int codeSize;
+    int codeNum;
     __code (**code) (struct Context *);
+    void* heap_start;
     void* heap;
-    int dataSize;
-    union Data **data;
-};
-
-struct Sem {
-    volatile int value;
-    pthread_mutex_t mutex;
-    pthread_cond_t cond;
-
+    long dataSize;
+    int dataNum;
+    union Data* head;
+    union Data** data;
 };
 
 union Data {
     long count;
-    struct ListContext {
-        int codeSize;
-        __code (**code) (struct Context *);
-        void* heap;
-        int dataSize;
-        struct ListData {
-            int value;
-            struct ListAllocate {
-                long size;
-                void* ds;
-                void* next;
-            } allocate;
-        } **data; 
-    } *list_context;
-
+    struct Queue {
+        union Data* head;
+        union Data* tail;
+        union Data* current;
+        int   dataSize;
+    } queue;
+    struct Element {
+        int value;
+        union Data* next;
+    } element;
     struct Allocate {
         long size;
         enum Code next;
         enum Code after_put;
-        enum Code after_take;
-        struct Sem *queue_remain;
-        struct Sem *queue_count;
-        union Data* front;
-        union Data* back;
+        enum Code after_get;
+        enum Code after_traverse;
     } allocate;
-}
+};