1 #include <lib/base/thread.h>
6 #include <lib/base/eerror.h>
8 void eThread::thread_completed(void *ptr)
10 eThread *p = (eThread*) ptr;
14 if (!p->m_state.value())
17 assert(p->m_state.value() == 1);
23 void *eThread::wrapper(void *ptr)
25 eThread *p = (eThread*)ptr;
26 pthread_cleanup_push(thread_completed, (void*)p);
29 pthread_cleanup_pop(1);
34 : the_thread(0), m_alive(0)
38 int eThread::runAsync(int prio, int policy)
40 eDebug("before: %d", m_state.value());
41 /* the thread might already run. */
45 eDebug("after: %d", m_state.value());
46 assert(m_state.value() == 1); /* sync postconditions */
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 m_state.down(); /* this might block */
94 assert(m_state.value() == 0);
96 return res; /* 0: thread is guaranteed not to run. 1: state unknown. */
99 void eThread::sendSignal(int sig)
102 pthread_kill(the_thread, sig);
104 eDebug("send signal to non running thread");
107 void eThread::kill(bool sendcancel)
109 if (!the_thread) /* already joined */
112 if (sync() && sendcancel)
114 eDebug("send cancel to thread");
115 pthread_cancel(the_thread);
117 eDebug("thread joined %d", pthread_join(the_thread, 0));
121 void eThread::hasStarted()
123 assert(!m_state.value());