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);
20 void *eThread::wrapper(void *ptr)
22 eThread *p = (eThread*)ptr;
23 pthread_cleanup_push(thread_completed, (void*)p);
26 pthread_cleanup_pop(1);
31 : the_thread(0), m_alive(0)
35 int eThread::runAsync(int prio, int policy)
37 eDebug("before: %d", m_state.value());
38 /* the thread might already run. */
42 eDebug("after: %d", m_state.value());
43 assert(m_state.value() == 1); /* sync postconditions */
51 pthread_attr_init(&attr);
56 p.__sched_priority=prio;
57 pthread_attr_setschedpolicy(&attr, policy);
58 pthread_attr_setschedparam(&attr, &p);
61 if (pthread_create(&the_thread, &attr, wrapper, this))
63 pthread_attr_destroy(&attr);
65 eDebug("couldn't create new thread");
69 pthread_attr_destroy(&attr);
73 int eThread::run(int prio, int policy)
75 if (runAsync(prio, policy))
86 int eThread::sync(void)
89 m_state.down(); /* this might block */
91 assert(m_state.value() == 0);
93 return res; /* 0: thread is guaranteed not to run. 1: state unknown. */
96 void eThread::sendSignal(int sig)
99 pthread_kill(the_thread, sig);
101 eDebug("send signal to non running thread");
104 void eThread::kill(bool sendcancel)
106 if (!the_thread) /* already joined */
109 if (sync() && sendcancel)
111 eDebug("send cancel to thread");
112 pthread_cancel(the_thread);
114 eDebug("thread joined %d", pthread_join(the_thread, 0));
118 void eThread::hasStarted()
120 assert(!m_state.value());