/[svn]/linuxsampler/trunk/src/common/Thread.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/common/Thread.cpp

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

revision 63 by schoenebeck, Tue May 4 18:52:24 2004 UTC revision 1212 by schoenebeck, Tue May 29 23:59:36 2007 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6     *   Copyright (C) 2005 - 2007 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 22  Line 23 
23    
24  #include "Thread.h"  #include "Thread.h"
25    
26  Thread::Thread(bool RealTime, int PriorityMax, int PriorityDelta) {  namespace LinuxSampler {
27    
28    Thread::Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta) {
29        this->bLockedMemory     = LockMemory;
30      this->isRealTime        = RealTime;      this->isRealTime        = RealTime;
31      this->Running           = false;      this->Running           = false;
32      this->PriorityDelta     = PriorityDelta;      this->PriorityDelta     = PriorityDelta;
# Line 98  int Thread::StopThread() { Line 102  int Thread::StopThread() {
102      if (Running) {      if (Running) {
103          SignalStopThread();          SignalStopThread();
104          pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex);          pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex);
105            pthread_detach(__thread_id);
106      }      }
107      pthread_mutex_unlock(&__thread_state_mutex);      pthread_mutex_unlock(&__thread_state_mutex);
108      return 0;      return 0;
# Line 116  int Thread::SignalStopThread() { Line 121  int Thread::SignalStopThread() {
121  }  }
122    
123  /**  /**
124     * Returns @c true in case the thread is currently running.
125     */
126    bool Thread::IsRunning() {
127        return Running;
128    }
129    
130    /**
131   *  Sets the process SCHED_FIFO policy,  if max=1 then set at max priority,   *  Sets the process SCHED_FIFO policy,  if max=1 then set at max priority,
132   *  else use min priority. delta is added to the priority so that we can   *  else use min priority. delta is added to the priority so that we can
133   *  for example set 3 SCHED_FIFO tasks to different priorities by specifying   *  for example set 3 SCHED_FIFO tasks to different priorities by specifying
# Line 123  int Thread::SignalStopThread() { Line 135  int Thread::SignalStopThread() {
135   *  current priority).   *  current priority).
136   */   */
137  int Thread::SetSchedulingPriority() {  int Thread::SetSchedulingPriority() {
138    #if !defined(__APPLE__)
139      struct sched_param schp;      struct sched_param schp;
140    
141      if (!isRealTime) return 0;      if (!isRealTime) return 0;
142    
     if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) {  
         perror("WARNING, can't mlockall() memory!");  
     }  
   
143      /*      /*
144       * set the process to realtime privs       * set the process to realtime privs
145       */       */
# Line 143  int Thread::SetSchedulingPriority() { Line 152  int Thread::SetSchedulingPriority() {
152      }      }
153    
154      if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) {      if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) {
155          perror("sched_setscheduler");          perror("Thread: WARNING, can't assign realtime scheduling to thread!");
156          return -1;          return -1;
157      }      }
158    #endif
159        return 0;
160    }
161    
162    /**
163     * Locks the memory so it will not be swapped out by the operating system.
164     */
165    int Thread::LockMemory() {
166    #if !defined(__APPLE__)
167        if (!bLockedMemory) return 0;
168        if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) {
169            perror("Thread: WARNING, can't mlockall() memory!");
170            return -1;
171        }
172    #endif
173      return 0;      return 0;
174  }  }
175    
# Line 174  int Thread::Destructor() { Line 197  int Thread::Destructor() {
197      Running = false;      Running = false;
198      pthread_mutex_unlock(&__thread_state_mutex);      pthread_mutex_unlock(&__thread_state_mutex);
199      pthread_cond_broadcast(&__thread_exit_condition);      pthread_cond_broadcast(&__thread_exit_condition);
200        return 0;
201  }  }
202    
203  /// Callback function for the POSIX thread API  /// Callback function for the POSIX thread API
# Line 182  void* __pthread_launcher(void* thread) { Line 206  void* __pthread_launcher(void* thread) {
206      Thread* t;      Thread* t;
207      t = (Thread*) thread;      t = (Thread*) thread;
208      t->SetSchedulingPriority();      t->SetSchedulingPriority();
209        t->LockMemory();
210      t->EnableDestructor();      t->EnableDestructor();
211      t->Main();      t->Main();
212        return NULL;
213  }  }
214    
215  /// Callback function for the POSIX thread API  /// Callback function for the POSIX thread API
# Line 192  void __pthread_destructor(void* thread) Line 218  void __pthread_destructor(void* thread)
218      t = (Thread*) thread;      t = (Thread*) thread;
219      t->Destructor();      t->Destructor();
220  }  }
221    
222    } // namespace LinuxSampler

Legend:
Removed from v.63  
changed lines
  Added in v.1212

  ViewVC Help
Powered by ViewVC