ISAAC
Overview :: Library Doc :: Server Doc :: JSON Commands

In Situ Animation of Accelerated Computations

ThreadList.hpp
Go to the documentation of this file.
1 /* This file is part of ISAAC.
2  *
3  * ISAAC is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * ISAAC is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with ISAAC. If not, see <www.gnu.org/licenses/>. */
15 
16 #pragma once
17 
18 #include <stdlib.h>
19 #include <unistd.h>
20 
29 template <typename T> class ThreadList
30 {
31  public:
33  {
34  T t;
37  typedef struct ThreadListContainer_struct *ThreadListContainer_ptr;
39  {
40  pthread_mutex_init (&remove_mutex, NULL);
41  front = NULL;
42  back = NULL;
43  l = 0;
44  }
45  void push_back(T t)
46  {
47  ThreadListContainer_ptr ptr = (ThreadListContainer_ptr)malloc(sizeof(ThreadListContainer));
48  ptr->t = t;
49  ptr->next = NULL;
50  pthread_mutex_lock (&remove_mutex);
51  if (back)
52  back->next = ptr;
53  else
54  front = ptr;
55  back = ptr;
56  l++;
57  pthread_mutex_unlock (&remove_mutex);
58  }
60  {
61  T t = NULL;
62  if (front)
63  {
64  t = front->t;
65  pthread_mutex_lock (&remove_mutex);
66  //delete front
67  ThreadListContainer_ptr next = front->next;
68  free(front);
69  front = next;
70  if (front == NULL)
71  back = NULL;
72  l--;
73  pthread_mutex_unlock (&remove_mutex);
74  }
75  return t;
76  }
77  int length()
78  {
79  return l;
80  }
82  {
83  while (front)
84  pop_front();
85  pthread_mutex_destroy(&remove_mutex);
86  }
87  ThreadListContainer_ptr getFront()
88  {
89  return front;
90  }
91  T remove(ThreadListContainer_ptr ptr)
92  {
93  if (ptr == NULL)
94  return NULL;
95  T t = NULL;
96  //Search before
97  ThreadListContainer_ptr before = NULL;
98  if (ptr != front)
99  {
100  before = front;
101  while (before)
102  {
103  if (before->next == ptr)
104  break;
105  before = before->next;
106  }
107  if (before == NULL)
108  return NULL;
109  }
110  pthread_mutex_lock (&remove_mutex);
111  if (before)
112  before->next = ptr->next;
113  else
114  front = ptr->next;
115  if (ptr == back)
116  back = before;
117  pthread_mutex_unlock (&remove_mutex);
118  t = ptr->t;
119  free(ptr);
120  l--;
121  return t;
122  }
123  //private:
124  volatile ThreadListContainer_ptr front;
125  volatile ThreadListContainer_ptr back;
126  pthread_mutex_t remove_mutex;
127  int l;
128 };
pthread_mutex_t remove_mutex
Definition: ThreadList.hpp:126
ThreadListContainer_ptr getFront()
Definition: ThreadList.hpp:87
struct ThreadListContainer_struct * next
Definition: ThreadList.hpp:35
int length()
Definition: ThreadList.hpp:77
struct ThreadListContainer_struct * ThreadListContainer_ptr
Definition: ThreadList.hpp:37
void push_back(T t)
Definition: ThreadList.hpp:45
volatile ThreadListContainer_ptr back
Definition: ThreadList.hpp:125
volatile ThreadListContainer_ptr front
Definition: ThreadList.hpp:124
struct ThreadList::ThreadListContainer_struct ThreadListContainer