TranscodingSetup : fix misspelling name.
[vuplus_dvbapp] / lib / base / thread.cpp
1 #include <lib/base/thread.h>
2
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <lib/base/eerror.h>
6
7 void eThread::thread_completed(void *ptr)
8 {
9         eThread *p = (eThread*) ptr;
10         p->m_alive = 0;
11
12                 /* recover state in case thread was cancelled before calling hasStarted */
13         if (!p->m_started)
14                 p->hasStarted();
15
16         p->thread_finished();
17 }
18
19 void *eThread::wrapper(void *ptr)
20 {
21         eThread *p = (eThread*)ptr;
22         pthread_cleanup_push(thread_completed, (void*)p);
23         p->thread();
24         pthread_exit(0);
25         pthread_cleanup_pop(1);
26         return 0;
27 }
28
29 eThread::eThread()
30         : the_thread(0), m_alive(0)
31 {
32 }
33
34 int eThread::runAsync(int prio, int policy)
35 {
36         eDebug("before: %d", m_state.value());
37                 /* the thread might already run. */
38         if (sync())
39                 return -1;
40         
41         eDebug("after: %d", m_state.value());
42         ASSERT(m_state.value() == 1); /* sync postconditions */
43         ASSERT(!m_alive);
44         m_state.down();
45         ASSERT(m_state.value() == 0);
46         
47         m_alive = 1;
48         m_started = 0;
49
50                 /* start thread. */
51         pthread_attr_t attr;
52         pthread_attr_init(&attr);
53         
54         if (prio || policy)
55         {
56                 struct sched_param p;
57                 p.__sched_priority=prio;
58                 pthread_attr_setschedpolicy(&attr, policy);
59                 pthread_attr_setschedparam(&attr, &p);
60         }
61
62         if (the_thread) {
63                 eDebug("old thread joined %d", pthread_join(the_thread, 0));
64                 the_thread = 0;
65         }
66
67         if (pthread_create(&the_thread, &attr, wrapper, this))
68         {
69                 pthread_attr_destroy(&attr);
70                 m_alive = 0;
71                 eDebug("couldn't create new thread");
72                 return -1;
73         }
74         
75         pthread_attr_destroy(&attr);
76         return 0;
77 }
78
79 int eThread::run(int prio, int policy)
80 {
81         if (runAsync(prio, policy))
82                 return -1;
83         sync();
84         return 0;
85 }
86
87 eThread::~eThread()
88 {
89         kill();
90 }
91
92 int eThread::sync(void)
93 {
94         int res;
95         int debug_val_before = m_state.value();
96         m_state.down(); /* this might block */
97         res = m_alive;
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);
101         m_state.up();
102         return res; /* 0: thread is guaranteed not to run. 1: state unknown. */
103 }
104
105 int eThread::sendSignal(int sig)
106 {
107         if (m_alive)
108                 return pthread_kill(the_thread, sig);
109         else
110                 eDebug("send signal to non running thread");
111         return -1;
112 }
113
114 void eThread::kill(bool sendcancel)
115 {
116         if (!the_thread) /* already joined */
117                 return;
118
119         if (sync() && sendcancel)
120         {
121                 eDebug("send cancel to thread");
122                 pthread_cancel(the_thread);
123         }
124         eDebug("thread joined %d", pthread_join(the_thread, 0));
125         the_thread = 0;
126 }
127
128 void eThread::hasStarted()
129 {
130         ASSERT(!m_state.value());
131         m_started = 1;
132         m_state.up();
133 }