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);
63 eDebug("old thread joined %d", pthread_join(the_thread, 0));
67 if (pthread_create(&the_thread, &attr, wrapper, this))
69 pthread_attr_destroy(&attr);
71 eDebug("couldn't create new thread");
75 pthread_attr_destroy(&attr);
79 int eThread::run(int prio, int policy)
81 if (runAsync(prio, policy))
92 int eThread::sync(void)
95 int debug_val_before = m_state.value();
96 m_state.down(); /* this might block */
98 if (m_state.value() != 0)
99 eFatal("eThread::sync: m_state.value() == %d - was %d before", m_state.value(), debug_val_before);
100 ASSERT(m_state.value() == 0);
102 return res; /* 0: thread is guaranteed not to run. 1: state unknown. */
105 int eThread::sendSignal(int sig)
108 return pthread_kill(the_thread, sig);
110 eDebug("send signal to non running thread");
114 void eThread::kill(bool sendcancel)
116 if (!the_thread) /* already joined */
119 if (sync() && sendcancel)
121 eDebug("send cancel to thread");
122 pthread_cancel(the_thread);
124 eDebug("thread joined %d", pthread_join(the_thread, 0));
128 void eThread::hasStarted()
130 ASSERT(!m_state.value());