annotate TaskQueue.c @ 0:5b089096921f

first commit.
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Fri, 18 Dec 2009 21:57:05 +0900
parents
children 803d6bf22e6d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include"TaskList.h"
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 typedef struct _TaskQueue {
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 TaskList *head;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 TaskList *tail;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 int length;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 } TaskQueue;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 TaskQueue *
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 newTaskQueue()
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 {
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 TaskQueue *queue;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 /* TODO: mallocはあとで独自実装に書き直し! */
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 malloc(sizeof(struct _TaskQueue));
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 queue->head = NULL;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 queue->tail = NULL;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 queue->length = 0;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 return queue;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 }
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 void
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 queueAddFirst(TaskQueue *queue, Task *)
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 {
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 TaskList *oldhead = queue->head;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 TaskList *newlist;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 newlist = malloc(sizeof(struct _TaskList));
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 if (oldhead) {
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 oldhead->prev = newlist;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 }
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 newlist->next = oldhead;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 newlist->prev = NULL;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 queue->head = newlist;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 queue->length++;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 return ;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 }
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 void
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 queueAddLast(TaskQueue *queue, Task *task)
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 {
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 TaskList *oldtail = queue->tail;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 TaskList *newlist;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 newlist = malloc(sizeof(struct _TaskList));
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 if (oldtail) {
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 oldtail->next = newlist;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 }
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 newlist->next = NULL;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 newlist->prev = oldtail;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 queue->tail = newlist;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 queue->length++;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 return ;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 }
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 Task *
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 queuePollFirst(TaskQueue *queue)
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 {
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 TaskList *first = queue->head;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 TaskList *second;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 Task *task;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 if (!first) return NULL;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 second = first->next;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 task = first->task;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 free(first);
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 second->prev = NULL;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 queue->head = second;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 queue->length--;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 return task;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 }
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 Task *
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 queuePollLast(TaskQueue *queue)
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 {
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 TaskList *first = queue->tail;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 TaskList *second;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 Task *task;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 if (!first) return NULL;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 second = first->prev;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 task = first->task;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 free(first);
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 second->next = NULL;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 queue->tail = second;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 queue->length--;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 return task;
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 }
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
5b089096921f first commit.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92