PULSAR  2.0.0
Parallel Ultra-Light Systolic Array Runtime
 All Data Structures Files Functions Typedefs Enumerations Macros Groups
icl_deque.c
Go to the documentation of this file.
1 
14 #include "icl_deque.h"
15 
17 
23 {
24  icl_deque_t *deque = (icl_deque_t*)malloc(sizeof(icl_deque_t));
25  if (deque == NULL)
26  return NULL;
27 
28  deque->list = icl_list_new();
29  if (deque->list == NULL) {
30  free(deque);
31  return NULL;
32  }
33  if (pthread_spin_init(&deque->spinlock, PTHREAD_PROCESS_PRIVATE) != 0) {
34  free(deque->list);
35  free(deque);
36  return NULL;
37  }
38  deque->size = 0;
39  return deque;
40 }
41 
43 
52 int icl_deque_destroy(icl_deque_t *deque, void (*free_func)(void*))
53 {
54  if (deque == NULL)
55  return -1;
56 
57  if (pthread_spin_destroy(&deque->spinlock) != 0)
58  return -1;
59 
60  if (icl_list_destroy(deque->list, free_func) != 0)
61  return -1;
62 
63  free(deque);
64  return 0;
65 }
66 
68 
76 {
77  if (deque == NULL)
78  return NULL;
79 
80  pthread_spin_lock(&deque->spinlock);
81  icl_node_t *node = icl_list_first(deque->list);
82  pthread_spin_unlock(&deque->spinlock);
83 
84  return node;
85 }
86 
88 
97 {
98  if (deque == NULL)
99  return NULL;
100 
101  pthread_spin_lock(&deque->spinlock);
102  icl_node_t *next = icl_list_next(deque->list, node);
103  pthread_spin_unlock(&deque->spinlock);
104 
105  return next;
106 }
107 
109 
118 {
119  if (deque == NULL || data == NULL)
120  return NULL;
121 
122  pthread_spin_lock(&deque->spinlock);
123  icl_node_t *node = icl_list_append(deque->list, data);
124  deque->size++;
125  pthread_spin_unlock(&deque->spinlock);
126 
127  return node;
128 }
129 
131 
140 {
141  if (deque == NULL || data == NULL)
142  return NULL;
143 
144  pthread_spin_lock(&deque->spinlock);
145  icl_node_t *node = icl_list_prepend(deque->list, data);
146  deque->size++;
147  pthread_spin_unlock(&deque->spinlock);
148 
149  return node;
150 }
151 
153 
164  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  deque->size--;
172  pthread_spin_unlock(&deque->spinlock);
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 }