21 #include "SheafSystem/thread.h" 26 #include "SheafSystem/assert_contract.h" 29 #include "SheafSystem/runnable.h" 32 #include "SheafSystem/singly_linked_list.h" 34 singly_linked_list<thread *> *
48 if (_thread_list == 0)
49 _thread_list =
new singly_linked_list<thread *>;
51 int old_list_size = _thread_list->size();
53 _thread_list->push_front(
this);
58 ensure(_thread_list->size() == old_list_size+1);
59 ensure(! is_alive(
this));
65 thread(runnable* xrunnable)
70 require(xrunnable != 0);
75 _runnable = xrunnable;
77 if (_thread_list == 0)
78 _thread_list =
new singly_linked_list<thread *>;
80 int old_list_size = _thread_list->size();
82 _thread_list->push_front(
this);
87 ensure(_thread_list->size() == old_list_size+1);
88 ensure(! is_alive(
this));
100 require(_thread_list != 0);
113 result = (_thread_list != 0);
133 int rtn_code = pthread_join(pthread,0);
137 ensure(rtn_code == 0);
161 int rtncode = pthread_create(&pthread,
162 reinterpret_cast<pthread_attr_t *>(0),
164 reinterpret_cast<void *>(
this));
168 ensure(rtncode == 0);
175 static_run(thread* This)
181 require(! is_alive(This));
185 pthread_cleanup_push(cleanup,This);
187 This->_is_alive =
true;
189 if (This->_runnable == 0)
192 This->_runnable->run();
194 This->_is_alive =
false;
196 pthread_cleanup_pop(1);
200 ensure(! is_alive(This));
208 sleep(
unsigned int seconds)
220 is_alive(thread* other)
238 if(_thread_list != 0)
241 singly_linked_list<thread *>::iterator it = _thread_list->begin();
242 singly_linked_list<thread *>::iterator end = _thread_list->end();
244 for (; it != end && *it != other; ++it)
247 if (it != end && (*it)->_is_alive)
267 pthread_t
self = pthread_self();
271 singly_linked_list<thread *> *list = sheaf::thread::_thread_list;
276 singly_linked_list<thread *>::iterator it = list->begin();
277 singly_linked_list<thread *>::iterator end = list->end();
279 for (; it != end && (pthread_equal(
self,(*it)->pthread) == 0); ++it)
297 cleanup(thread* This)
303 require(! is_alive(This));
307 singly_linked_list<thread *>::iterator it = _thread_list->begin();
308 singly_linked_list<thread *>::iterator end = _thread_list->end();
310 for (; it != end && *it != This; ++it)
313 assertion(it != end);
315 This->_thread_list->erase(it);
332 singly_linked_list<thread *> *list = sheaf::thread::_thread_list;
334 result = list->size();
356 int rtncode = pthread_cancel(pthread);
360 ensure(rtncode == 0);