home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / guicast / thread.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  2KB  |  121 lines

  1. #include <sys/wait.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "thread.h"
  6.  
  7.  
  8. Thread::Thread(int synchronous, int realtime)
  9. {
  10.     this->synchronous = synchronous; 
  11.     this->realtime = realtime;  
  12.     tid = (pthread_t)-1;
  13.     thread_running = 0;
  14. }
  15.  
  16. Thread::~Thread()
  17. {
  18. }
  19.  
  20. void* Thread::entrypoint(void *parameters)
  21. {
  22.     Thread *pt = static_cast<Thread *>(parameters);
  23. // allow thread to be cancelled in the middle of something
  24.     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
  25.     pt->run();
  26.     pt->thread_running = 0;
  27.     return NULL;
  28. }
  29.  
  30. void Thread::start()
  31. {
  32.     pthread_attr_t  attr;
  33.     struct sched_param param;
  34.  
  35.     pthread_attr_init(&attr);
  36.  
  37.     thread_running = 1;
  38. // caused SEGFLT when reading from files
  39.     if(!synchronous) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  40.  
  41.     if(realtime)
  42.     {
  43.         if(pthread_attr_setschedpolicy(&attr, SCHED_RR) < 0)
  44.             printf("Couldn't set realtime thread.\n");
  45.         param.sched_priority = 50;
  46.         if(pthread_attr_setschedparam(&attr, ¶m) < 0)
  47.             printf("Couldn't set realtime thread.\n");;
  48.     }
  49.     
  50.     pthread_create(&tid, &attr, Thread::entrypoint, this);
  51. }
  52.  
  53. int Thread::end(pthread_t tid)           // need to join after this if synchronous
  54. {
  55.     if(tid >= 0) pthread_cancel(tid);
  56.     return 0;
  57. }
  58.  
  59. int Thread::end()           // need to join after this if synchronous
  60. {
  61.     cancel();
  62.     return 0;
  63. }
  64.  
  65. int Thread::cancel()
  66. {
  67.     if(tid >= 0) pthread_cancel(tid);
  68.     if(!synchronous) tid = (pthread_t)-1;
  69.     return 0;
  70. }
  71.  
  72. int Thread::join()   // join this thread
  73. {
  74.     if(tid >= 0) pthread_join(tid, 0); 
  75.     tid = (pthread_t)-1;
  76.     return 0;
  77. }
  78.  
  79. int Thread::enable_cancel()
  80. {
  81.     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  82.     return 0;
  83. }
  84.  
  85. int Thread::disable_cancel()
  86. {
  87.     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  88.     return 0;
  89. }
  90.  
  91. int Thread::exit_thread()
  92. {
  93.      pthread_exit(0);
  94.     if(!synchronous) tid = (pthread_t)-1;
  95.     return 0;
  96. }
  97.  
  98.  
  99. int Thread::suspend_thread()
  100. {
  101.     if(tid >= 0) pthread_kill(tid, SIGSTOP);
  102.     return 0;
  103. }
  104.  
  105. int Thread::continue_thread()
  106. {
  107.     if(tid >= 0) pthread_kill(tid, SIGCONT);
  108.     return 0;
  109. }
  110.  
  111. int Thread::set_realtime()
  112. {
  113.     realtime = 1;
  114.     return 0;
  115. }
  116.  
  117. int Thread::running()
  118. {
  119.     return thread_running;
  120. }
  121.