/[svn]/linuxsampler/tags/start/thread.cpp
ViewVC logotype

Annotation of /linuxsampler/tags/start/thread.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (hide annotations) (download)
Sat Oct 25 20:24:32 2003 UTC (20 years, 6 months ago) by (unknown author)
File size: 4147 byte(s)
This commit was manufactured by cvs2svn to create tag 'start'.
1 schoenebeck 5 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck *
6     * *
7     * This program is free software; you can redistribute it and/or modify *
8     * it under the terms of the GNU General Public License as published by *
9     * the Free Software Foundation; either version 2 of the License, or *
10     * (at your option) any later version. *
11     * *
12     * This program is distributed in the hope that it will be useful, *
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15     * GNU General Public License for more details. *
16     * *
17     * You should have received a copy of the GNU General Public License *
18     * along with this program; if not, write to the Free Software *
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20     * MA 02111-1307 USA *
21     ***************************************************************************/
22    
23     #include "thread.h"
24    
25     Thread::Thread(bool RealTime, int PriorityMax, int PriorityDelta) {
26     this->isRealTime=RealTime;
27     this->Running = false;
28     this->PriorityDelta = PriorityDelta;
29     this->PriorityMax = PriorityMax;
30     }
31    
32     Thread::~Thread() {
33     if (this->Running) StopThread();
34     }
35    
36     int Thread::StartThread() {
37     // Create and run the thread
38     this->Running = true;
39     int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this);
40     switch (res) {
41     case 0: // Success
42     break;
43     case EAGAIN:
44     std::cerr << "Thread creation failed: System doesn't allow to create another thread."
45     << std::endl << std::flush;
46     this->Running = false;
47     break;
48     case EPERM:
49     std::cerr << "Thread creation failed: You're lacking permisssions to set required scheduling policy and parameters."
50     << std::endl << std::flush;
51     this->Running = false;
52     break;
53     default:
54     std::cerr << "Thread creation failed: Unknown cause."
55     << std::endl << std::flush;
56     this->Running = false;
57     break;
58     }
59     return res;
60     }
61    
62     int Thread::StopThread() {
63     pthread_cancel(__thread_id);
64     Running = false;
65     return 0;
66     }
67    
68     /**
69     * Sets the process SCHED_FIFO policy, if max=1 then set at max priority,
70     * else use min priority. delta is added to the priority so that we can
71     * for example set 3 SCHED_FIFO tasks to different priorities by specifying
72     * delta 0 , -1 , -2 ( 0 = highest priority because -1 is subtracted to the
73     * current priority).
74     */
75     int Thread::SetSchedulingPriority() {
76     struct sched_param schp;
77    
78     if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) {
79     perror("WARNING, can't mlockall() memory!");
80     }
81    
82     if(!isRealTime) return 0;
83    
84     /*
85     * set the process to realtime privs
86     */
87     memset(&schp, 0, sizeof(schp));
88     if (this->PriorityMax == 1) {
89     schp.sched_priority = sched_get_priority_max(SCHED_FIFO) + this->PriorityDelta;
90     }
91     if (this->PriorityMax == -1) {
92     schp.sched_priority = sched_get_priority_min(SCHED_FIFO) + this->PriorityDelta;
93     }
94    
95     if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) {
96     perror("sched_setscheduler");
97     return -1;
98     }
99    
100     return 0;
101     }
102    
103     /// Callback function for the POSIX thread API
104     void* __pthread_launcher(void* thread) {
105     Thread* t;
106     t = (Thread*) thread;
107     t->SetSchedulingPriority();
108     t->Main();
109     };

  ViewVC Help
Powered by ViewVC