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 */
53 pthread_attr_init(&attr);
58 p.__sched_priority=prio;
59 pthread_attr_setschedpolicy(&attr, policy);
60 pthread_attr_setschedparam(&attr, &p);
63 if (pthread_create(&the_thread, &attr, wrapper, this))
65 pthread_attr_destroy(&attr);
67 eDebug("couldn't create new thread");
71 pthread_attr_destroy(&attr);
75 int eThread::run(int prio, int policy)
77 if (runAsync(prio, policy))
88 int eThread::sync(void)
91 m_state.down(); /* this might block */
93 assert(m_state.value() == 0);
95 return res; /* 0: thread is guaranteed not to run. 1: state unknown. */
98 void eThread::sendSignal(int sig)
101 pthread_kill(the_thread, sig);
103 eDebug("send signal to non running thread");
106 void eThread::kill(bool sendcancel)
108 if (!the_thread) /* already joined */
111 if (sync() && sendcancel)
113 eDebug("send cancel to thread");
114 pthread_cancel(the_thread);
116 eDebug("thread joined %d", pthread_join(the_thread, 0));
120 void eThread::hasStarted()
122 assert(!m_state.value());