PULSAR  1.0.0
Parallel Ultra Light Systolic Array Runtime
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
icl_deque.c
Go to the documentation of this file.
1 
15 #include "icl_deque.h"
16 
18 
24 {
25  icl_deque_t *deque = (icl_deque_t*)malloc(sizeof(icl_deque_t));
26  if (deque == NULL)
27  return NULL;
28 
29  deque->list = icl_list_new();
30  if (deque->list == NULL) {
31  free(deque);
32  return NULL;
33  }
34  if (pthread_spin_init(&deque->spinlock, PTHREAD_PROCESS_PRIVATE) != 0) {
35  free(deque->list);
36  free(deque);
37  return NULL;
38  }
39  deque->size = 0;
40  return deque;
41 }
42 
44 
53 int icl_deque_destroy(icl_deque_t *deque, void (*free_func)(void*))
54 {
55  if (deque == NULL)
56  return -1;
57 
58  if (pthread_spin_destroy(&deque->spinlock) != 0)
59  return -1;
60 
61  if (icl_list_destroy(deque->list, free_func) != 0)
62  return -1;
63 
64  free(deque);
65  return 0;
66 }
67 
69 
77 {
78  if (deque == NULL)
79  return NULL;
80 
81  pthread_spin_lock(&deque->spinlock);
82  icl_node_t *node = icl_list_first(deque->list);
83  pthread_spin_unlock(&deque->spinlock);
84 
85  return node;
86 }
87 
89 
98 {
99  if (deque == NULL)
100  return NULL;
101 
102  pthread_spin_lock(&deque->spinlock);
103  icl_node_t *next = icl_list_next(deque->list, node);
104  pthread_spin_unlock(&deque->spinlock);
105 
106  return next;
107 }
108 
110 
119 {
120  if (deque == NULL || data == NULL)
121  return NULL;
122 
123  pthread_spin_lock(&deque->spinlock);
124  icl_node_t *node = icl_list_append(deque->list, data);
125  deque->size++;
126  pthread_spin_unlock(&deque->spinlock);
127 
128  return node;
129 }
130 
132 
141 {
142  if (deque == NULL || data == NULL)
143  return NULL;
144 
145  pthread_spin_lock(&deque->spinlock);
146  icl_node_t *node = icl_list_prepend(deque->list, data);
147  deque->size++;
148  pthread_spin_unlock(&deque->spinlock);
149 
150  return node;
151 }
152 
154 
164 int icl_deque_delete(icl_deque_t *deque, icl_node_t *node, void (*free_func)(void*))
165 {
166  if (deque == NULL || node == NULL)
167  return -1;
168 
169  pthread_spin_lock(&deque->spinlock);
170  if (icl_list_delete(deque->list, node, free_func) == 0) {
171  pthread_spin_unlock(&deque->spinlock);
172  deque->size--;
173  return 0;
174  }
175  else {
176  pthread_spin_unlock(&deque->spinlock);
177  return -1;
178  }
179 }
180 
182 
190 {
191  if (deque == NULL)
192  return -1;
193 
194  pthread_spin_lock(&deque->spinlock);
195  int size = deque->size;
196  pthread_spin_unlock(&deque->spinlock);
197  return size;
198 }
icl_node_t * icl_deque_append(icl_deque_t *deque, void *data)
Insert the node at the end of the deque.
Definition: icl_deque.c:118
icl_list_t * icl_list_prepend(icl_list_t *head, void *data)
Insert a node at the beginning of this list.
Definition: icl_list.c:312
icl_list_t * icl_list_new()
Create new linked list.
Definition: icl_list.c:23
icl_deque_t * icl_deque_new()
deque constructor
Definition: icl_deque.c:23
int icl_deque_size(icl_deque_t *deque)
Return the deque size.
Definition: icl_deque.c:189
int icl_list_destroy(icl_list_t *head, void(*free_function)(void *))
Frees the resources associated with this linked list.
Definition: icl_list.c:173
icl_node_t * icl_deque_next(icl_deque_t *deque, icl_node_t *node)
Get the next node in the deque.
Definition: icl_deque.c:97
icl_node_t * icl_deque_first(icl_deque_t *deque)
Get the first node in the deque.
Definition: icl_deque.c:76
icl_list_t * icl_list_append(icl_list_t *head, void *data)
Insert a node at the end of this list.
Definition: icl_list.c:326
icl_list_t * icl_list_first(icl_list_t *head)
Get the first item in this linked list.
Definition: icl_list.c:221
icl_list_t * list
Definition: icl_deque.h:30
int icl_deque_destroy(icl_deque_t *deque, void(*free_func)(void *))
deque destructor
Definition: icl_deque.c:53
int icl_deque_delete(icl_deque_t *deque, icl_node_t *node, void(*free_func)(void *))
Delete the node from the deque.
Definition: icl_deque.c:164
int icl_list_delete(icl_list_t *head, icl_list_t *pos, void(*free_function)(void *))
Delete the specified node.
Definition: icl_list.c:83
pthread_spinlock_t spinlock
Definition: icl_deque.h:29
icl_node_t * icl_deque_prepend(icl_deque_t *deque, void *data)
Insert the node at the front of the deque.
Definition: icl_deque.c:140
icl_list_t * icl_list_next(icl_list_t *head, icl_list_t *pos)
Get the node following the specified node.
Definition: icl_list.c:254