1 #include <lib/base/thread.h>
5 #include <lib/base/eerror.h>
7 void eThread::thread_completed(void *ptr)
9 eThread *p = (eThread*) ptr;
12 /* recover state in case thread was cancelled before calling hasStarted */
19 void *eThread::wrapper(void *ptr)
21 eThread *p = (eThread*)ptr;
22 pthread_cleanup_push(thread_completed, (void*)p);
25 pthread_cleanup_pop(1);
30 : the_thread(0), m_alive(0)
34 int eThread::runAsync(int prio, int policy)
36 eDebug("before: %d", m_state.value());
37 /* the thread might already run. */
41 eDebug("after: %d", m_state.value());
42 ASSERT(m_state.value() == 1); /* sync postconditions */
45 ASSERT(m_state.value() == 0);
52 pthread_attr_init(&attr);
57 p.__sched_priority=prio;
58 pthread_attr_setschedpolicy(&attr, policy);
59 pthread_attr_setschedparam(&attr, &p);
62 if (pthread_create(&the_thread, &attr, wrapper, this))
64 pthread_attr_destroy(&attr);
66 eDebug("couldn't create new thread");
70 pthread_attr_destroy(&attr);
74 int eThread::run(int prio, int policy)
76 if (runAsync(prio, policy))
87 int eThread::sync(void)
90 int debug_val_before = m_state.value();
91 m_state.down(); /* this might block */
93 if (m_state.value() != 0)
94 eFatal("eThread::sync: m_state.value() == %d - was %d before", m_state.value(), debug_val_before);
95 ASSERT(m_state.value() == 0);
97 return res; /* 0: thread is guaranteed not to run. 1: state unknown. */
100 int eThread::sendSignal(int sig)
103 return pthread_kill(the_thread, sig);
105 eDebug("send signal to non running thread");
109 void eThread::kill(bool sendcancel)
111 if (!the_thread) /* already joined */
114 if (sync() && sendcancel)
116 eDebug("send cancel to thread");
117 pthread_cancel(the_thread);
119 eDebug("thread joined %d", pthread_join(the_thread, 0));
123 void eThread::hasStarted()
125 ASSERT(!m_state.value());