/[svn]/linuxsampler/trunk/src/thread.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/thread.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 12 by schoenebeck, Wed Nov 5 14:47:10 2003 UTC revision 13 by schoenebeck, Fri Nov 21 15:07:23 2003 UTC
# Line 23  Line 23 
23  #include "thread.h"  #include "thread.h"
24    
25  Thread::Thread(bool RealTime, int PriorityMax, int PriorityDelta) {  Thread::Thread(bool RealTime, int PriorityMax, int PriorityDelta) {
26      this->isRealTime=RealTime;      this->isRealTime        = RealTime;
27      this->Running       = false;      this->Running           = false;
28      this->PriorityDelta = PriorityDelta;      this->PriorityDelta     = PriorityDelta;
29      this->PriorityMax   = PriorityMax;      this->PriorityMax       = PriorityMax;
30        __thread_destructor_key = 0;
31        pthread_mutex_init(&__thread_state_mutex, NULL);
32        pthread_cond_init(&__thread_exit_condition, NULL);
33  }  }
34    
35  Thread::~Thread() {  Thread::~Thread() {
36      if (this->Running) StopThread();      if (this->Running) StopThread();
37        pthread_cond_destroy(&__thread_exit_condition);
38        pthread_mutex_destroy(&__thread_state_mutex);
39  }  }
40    
41    /**
42     *  Start the thread. The Main() method is the entry point for the new
43     *  thread. You have to implement the Main() method in your subclass.
44     */
45  int Thread::StartThread() {  int Thread::StartThread() {
46      // Create and run the thread      // Create and run the thread
     this->Running = true;  
47      int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this);      int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this);
48      switch (res) {      switch (res) {
49          case 0: // Success          case 0: // Success
# Line 59  int Thread::StartThread() { Line 67  int Thread::StartThread() {
67      return res;      return res;
68  }  }
69    
70    /**
71     *  Stops the thread. This method will wait until the thread actually stopped
72     *  it's execution before it will return.
73     */
74  int Thread::StopThread() {  int Thread::StopThread() {
75        pthread_mutex_lock(&__thread_state_mutex);
76        if (Running) {
77            SignalStopThread();
78            pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex);
79        }
80        pthread_mutex_unlock(&__thread_state_mutex);
81        return 0;
82    }
83    
84    /**
85     *  Stops the thread. This method will signal to stop the thread and return
86     *  immediately. Note that the thread might still run when this method
87     *  returns!
88     */
89    int Thread::SignalStopThread() {
90      pthread_cancel(__thread_id);      pthread_cancel(__thread_id);
     Running = false;  
91      return 0;      return 0;
92  }  }
93    
# Line 100  int Thread::SetSchedulingPriority() { Line 126  int Thread::SetSchedulingPriority() {
126      return 0;      return 0;
127  }  }
128    
129    /**
130     *  Registers thread destructor callback function which will be executed when
131     *  the thread stops it's execution and sets the 'Running' flag to true. This
132     *  method will be called by the __pthread_launcher callback function, DO NOT
133     *  CALL THIS METHOD YOURSELF!
134     */
135    void Thread::EnableDestructor() {
136        pthread_mutex_lock(&__thread_state_mutex);
137        pthread_key_create(&__thread_destructor_key, __pthread_destructor);
138        pthread_setspecific(__thread_destructor_key, this);
139        Running = true;
140        pthread_mutex_unlock(&__thread_state_mutex);
141    }
142    
143    /**
144     *  Will be called by the kernel when the thread stops it's execution.
145     */
146    int Thread::Destructor() {
147        pthread_key_delete(__thread_destructor_key);
148        pthread_mutex_lock(&__thread_state_mutex);
149        Running = false;
150        pthread_mutex_unlock(&__thread_state_mutex);
151        pthread_cond_broadcast(&__thread_exit_condition);
152    }
153    
154  /// Callback function for the POSIX thread API  /// Callback function for the POSIX thread API
155  void* __pthread_launcher(void* thread) {  void* __pthread_launcher(void* thread) {
156      Thread* t;      Thread* t;
157      t = (Thread*) thread;      t = (Thread*) thread;
158        t->EnableDestructor();
159      t->SetSchedulingPriority();      t->SetSchedulingPriority();
160      t->Main();      t->Main();
161  };  }
162    
163    /// Callback function for the POSIX thread API
164    void __pthread_destructor(void* thread) {
165        Thread* t;
166        t = (Thread*) thread;
167        t->Destructor();
168    }

Legend:
Removed from v.12  
changed lines
  Added in v.13

  ViewVC Help
Powered by ViewVC