Merge branch 'bug_387_small_spinner_fix' into experimental
[enigma2.git] / 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 (pthread_create(&the_thread, &attr, wrapper, this))
63         {
64                 pthread_attr_destroy(&attr);
65                 m_alive = 0;
66                 eDebug("couldn't create new thread");
67                 return -1;
68         }
69         
70         pthread_attr_destroy(&attr);
71         return 0;
72 }                     
73
74 int eThread::run(int prio, int policy)
75 {
76         if (runAsync(prio, policy))
77                 return -1;
78         sync();
79         return 0;
80 }
81
82 eThread::~eThread()
83 {
84         kill();
85 }
86
87 int eThread::sync(void)
88 {
89         int res;
90         int debug_val_before = m_state.value();
91         m_state.down(); /* this might block */
92         res = m_alive;
93         if (m_state.value() != 0)
94                 eFatal("eThread::sync: m_state.value() == %d - was %d before", m_state.value(), debug_val_before);
95         ASSERT(m_state.value() == 0);
96         m_state.up();
97         return res; /* 0: thread is guaranteed not to run. 1: state unknown. */
98 }
99
100 int eThread::sendSignal(int sig)
101 {
102         if (m_alive)
103                 return pthread_kill(the_thread, sig);
104         else
105                 eDebug("send signal to non running thread");
106         return -1;
107 }
108
109 void eThread::kill(bool sendcancel)
110 {
111         if (!the_thread) /* already joined */
112                 return;
113
114         if (sync() && sendcancel)
115         {
116                 eDebug("send cancel to thread");
117                 pthread_cancel(the_thread);
118         }
119         eDebug("thread joined %d", pthread_join(the_thread, 0));
120         the_thread = 0;
121 }
122
123 void eThread::hasStarted()
124 {
125         ASSERT(!m_state.value());
126         m_started = 1;
127         m_state.up();
128 }