1 #include <lib/base/thread.h>
5 #include <lib/base/eerror.h>
7 void eThread::thread_completed(void *ptr)
9 eThread *p = (eThread*) ptr;
13 if (!p->m_state.value())
16 ASSERT(p->m_state.value() == 1);
22 void *eThread::wrapper(void *ptr)
24 eThread *p = (eThread*)ptr;
25 pthread_cleanup_push(thread_completed, (void*)p);
28 pthread_cleanup_pop(1);
33 : the_thread(0), m_alive(0)
37 int eThread::runAsync(int prio, int policy)
39 eDebug("before: %d", m_state.value());
40 /* the thread might already run. */
44 eDebug("after: %d", m_state.value());
45 ASSERT(m_state.value() == 1); /* sync postconditions */
48 ASSERT(m_state.value() == 0);
54 pthread_attr_init(&attr);
59 p.__sched_priority=prio;
60 pthread_attr_setschedpolicy(&attr, policy);
61 pthread_attr_setschedparam(&attr, &p);
64 if (pthread_create(&the_thread, &attr, wrapper, this))
66 pthread_attr_destroy(&attr);
68 eDebug("couldn't create new thread");
72 pthread_attr_destroy(&attr);
76 int eThread::run(int prio, int policy)
78 if (runAsync(prio, policy))
89 int eThread::sync(void)
92 int debug_val_before = m_state.value();
93 m_state.down(); /* this might block */
95 if (m_state.value() != 0)
96 eFatal("eThread::sync: m_state.value() == %d - was %d before", m_state.value(), debug_val_before);
97 ASSERT(m_state.value() == 0);
99 return res; /* 0: thread is guaranteed not to run. 1: state unknown. */
102 int eThread::sendSignal(int sig)
105 return pthread_kill(the_thread, sig);
107 eDebug("send signal to non running thread");
111 void eThread::kill(bool sendcancel)
113 if (!the_thread) /* already joined */
116 if (sync() && sendcancel)
118 eDebug("send cancel to thread");
119 pthread_cancel(the_thread);
121 eDebug("thread joined %d", pthread_join(the_thread, 0));
125 void eThread::hasStarted()
127 ASSERT(!m_state.value());