ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Seamstress.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Seamstress.cpp
1 #include "Seamstress.h"
2 #include "Needle.h"
3 
4 #include <cstddef>
5 #include <memory>
6 
7 using namespace std;
8 
9 
10 namespace SeamStress
11 {
12  Seamstress::Seamstress()
13  {
14  gotime = false;
15  end = false;
16  queue_end = false;
17  running = false;
18  started = false;
19  pthread_mutexattr_init(&mattr);
20  pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
21  pthread_mutex_init(&mutex, &mattr);
22  pthread_cond_init(&cond, NULL);
23  pthread_cond_init(&waitcond, NULL);
24  pthread_attr_init(&attr);
25  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
26  }
27 
28 
29  Seamstress::Seamstress(const Seamstress &/*ss*/)
30  {
31  gotime = false;
32  end = false;
33  queue_end = false;
34  running = false;
35  started = false;
36  pthread_mutexattr_init(&mattr);
37  pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
38  pthread_mutex_init(&mutex, &mattr);
39  pthread_cond_init(&cond, NULL);
40  pthread_cond_init(&waitcond, NULL);
41  pthread_attr_init(&attr);
42  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
43  }
44 
45 
46  Seamstress::~Seamstress()
47  {
48  pthread_mutex_lock(&mutex);
49  while(started==true)
50  {
51  pthread_cond_wait(&waitcond, &mutex);
52  }
53  pthread_mutex_unlock(&mutex);
54 
55  pthread_attr_destroy(&attr);
56  pthread_mutex_destroy(&mutex);
57  pthread_mutexattr_destroy(&mattr);
58  pthread_cond_destroy(&cond);
59  pthread_cond_destroy(&waitcond);
60  }
61 
62 
63  void *Seamstress::prepare(void *arg)
64  {
65  Seamstress *seamstress = (Seamstress*)arg;
66  while((seamstress->end)==false)
67  {
68  pthread_mutex_lock(&(seamstress->mutex));
69  while(seamstress->gotime==false)
70  {
71  pthread_cond_wait(&(seamstress->cond), &(seamstress->mutex));
72  }
73  if(seamstress->queue_end == true)
74  {
75  seamstress->end = true;
76  pthread_mutex_unlock(&(seamstress->mutex));
77  continue;
78  }
79  pthread_mutex_unlock(&(seamstress->mutex));
80 
81 
82  seamstress->gotime = false;
83  (*(seamstress->needle))(seamstress->thread);
84 
85 
86  pthread_mutex_lock(&(seamstress->mutex));
87  seamstress->running = false;
88  if(seamstress->queue_end==true){seamstress->end=true;}
89  pthread_mutex_unlock(&(seamstress->mutex));
90  pthread_cond_signal(&(seamstress->waitcond));
91  }
92  pthread_mutex_lock(&(seamstress->mutex));
93  seamstress->started = false;
94  seamstress->running = false;
95  pthread_mutex_unlock(&(seamstress->mutex));
96  pthread_cond_signal(&(seamstress->waitcond));
97  pthread_exit(NULL);
98  }
99 
100 
102  {
103  pthread_mutex_lock(&mutex);
104  if(started==false)
105  {
106  end = false;
107  gotime = false;
108  queue_end = false;
109  running = false;
110  started = true;
111  pthread_mutex_unlock(&mutex);
112  pthread_create(&pthread, &attr, &Seamstress::prepare, this);
113  }
114  else
115  {
116  pthread_mutex_unlock(&mutex);
117  }
118  }
119 
120 
121  void Seamstress::stop()
122  {
123  pthread_mutex_lock(&mutex);
124  queue_end = true;
125  gotime = true;
126  pthread_mutex_unlock(&mutex);
127  pthread_cond_signal(&cond);
128  }
129 
130 
131  void Seamstress::sew()
132  {
133  pthread_mutex_lock(&mutex);
134  gotime = true;
135  running = true;
136  pthread_mutex_unlock(&mutex);
137  pthread_cond_signal(&cond);
138  }
139 
140 
141  void Seamstress::rest()
142  {
143  pthread_mutex_lock(&mutex);
144  while(running==true)
145  {
146  pthread_cond_wait(&waitcond, &mutex);
147  }
148  pthread_mutex_unlock(&mutex);
149  }
150 
151 
152  void Seamstress::init_vector(unsigned long int N, vector<Seamstress> &vec)
153  {
154  vec.clear();
155  Seamstress st;
156 
157  for(unsigned int i=0;i<N;i++)
158  {
159  vec.push_back(st);
160  }
161  for(unsigned int i=0;i<N;i++)
162  {
163  vec[i].start();
164  }
165  }
166 
167 
168  vector<Seamstress*>* Seamstress::create_vector(unsigned long int N)
169  {
170  vector<Seamstress*>* vec = new vector<Seamstress*>();
171  for(unsigned long int i=0;i<N;i++){vec->push_back(new Seamstress());}
172  for(unsigned long int i=0;i<N;i++){(*vec)[i]->start();}
173  return vec;
174  }
175 }
176 
177