aboutsummaryrefslogtreecommitdiff
path: root/lib/base/thread.cpp
diff options
context:
space:
mode:
authorAndreas Monzner <andreas.monzner@multimedia-labs.de>2005-06-08 12:38:15 +0000
committerAndreas Monzner <andreas.monzner@multimedia-labs.de>2005-06-08 12:38:15 +0000
commit62b8a649fcae500c983215fac2e5202916c0195f (patch)
tree09cda62e0d1be6d3b1db4f3761ac418ad01c50b6 /lib/base/thread.cpp
parent94284f21b07f1756120e8b6f5dd53e485a9ff66d (diff)
downloadenigma2-62b8a649fcae500c983215fac2e5202916c0195f.tar.gz
enigma2-62b8a649fcae500c983215fac2e5202916c0195f.zip
merge some code with enigma code
Diffstat (limited to 'lib/base/thread.cpp')
-rw-r--r--lib/base/thread.cpp77
1 files changed, 62 insertions, 15 deletions
diff --git a/lib/base/thread.cpp b/lib/base/thread.cpp
index b75378b8..fa09691a 100644
--- a/lib/base/thread.cpp
+++ b/lib/base/thread.cpp
@@ -1,40 +1,87 @@
#include <lib/base/thread.h>
+
#include <stdio.h>
+#include <unistd.h>
#include <lib/base/eerror.h>
+void eThread::thread_completed(void *ptr)
+{
+ eThread *p = (eThread*) ptr;
+ eDebug("thread has completed..");
+ p->alive=0;
+ p->thread_finished();
+}
+
void *eThread::wrapper(void *ptr)
{
- ((eThread*)ptr)->thread();
+ eThread *p = (eThread*)ptr;
+ p->alive=1;
+ pthread_cleanup_push( thread_completed, (void*)p );
+ p->thread();
pthread_exit(0);
+ pthread_cleanup_pop(0);
}
eThread::eThread()
+ :alive(0)
{
- alive=0;
}
-void eThread::run()
+void eThread::run( int prio, int policy )
{
- alive=1;
- pthread_create(&the_thread, 0, wrapper, this);
-}
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ if (prio||policy)
+ {
+ struct sched_param p;
+ p.__sched_priority=prio;
+ pthread_attr_setschedpolicy(&attr, policy );
+ pthread_attr_setschedparam(&attr, &p);
+ }
+ pthread_create(&the_thread, &attr, wrapper, this);
+ usleep(1000);
+ int timeout=20;
+ while(!alive && timeout--)
+ {
+ eDebug("waiting for thread start...");
+ usleep(1000*10);
+ }
+ if ( !timeout )
+ eDebug("thread couldn't be started !!!");
+}
eThread::~eThread()
{
- if (alive)
+ if ( alive )
kill();
}
-void eThread::kill()
+void eThread::sendSignal(int sig)
{
- alive=0;
- eDebug("waiting for thread shutdown");
- pthread_join(the_thread, 0);
- eDebug("ok");
+ if ( alive )
+ pthread_kill( the_thread, sig );
+ else
+ eDebug("send signal to non running thread");
}
-void eThread::sendSignal(int sig)
+void eThread::kill(bool hard)
{
- if (alive)
- pthread_kill(the_thread, sig);
+ if ( !alive )
+ {
+ eDebug("kill.. but thread don't running");
+ return;
+ }
+
+ if ( hard )
+ {
+ eDebug("killing the thread...");
+ pthread_cancel(the_thread);
+ alive=0;
+ }
+ else
+ {
+ eDebug("waiting for thread shutdown...");
+ pthread_join(the_thread, 0);
+ eDebug("ok");
+ }
}