1 |
schoenebeck |
53 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
schoenebeck |
56 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
persson |
2185 |
* Copyright (C) 2005 - 2011 Christian Schoenebeck * |
7 |
schoenebeck |
53 |
* * |
8 |
|
|
* 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 * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
schoenebeck |
1424 |
#ifndef __LS_THREAD_H__ |
25 |
|
|
#define __LS_THREAD_H__ |
26 |
schoenebeck |
53 |
|
27 |
schoenebeck |
1482 |
//FIXME: this is a temorary solution because of problems with condition variables we use a polling lock in SignalStartThread() |
28 |
|
|
#if defined(WIN32) |
29 |
senoner |
1481 |
#define WIN32_SIGNALSTARTTHREAD_WORKAROUND 1 |
30 |
schoenebeck |
1482 |
#endif |
31 |
senoner |
1481 |
|
32 |
schoenebeck |
53 |
#include <iostream> |
33 |
|
|
#include <stdio.h> |
34 |
|
|
#include <stdlib.h> |
35 |
senoner |
1481 |
|
36 |
|
|
#if defined(WIN32) |
37 |
|
|
#include <windows.h> |
38 |
|
|
#else |
39 |
schoenebeck |
53 |
#include <sched.h> |
40 |
|
|
#include <sys/mman.h> |
41 |
|
|
#include <memory.h> |
42 |
|
|
#include <pthread.h> |
43 |
senoner |
1481 |
#endif |
44 |
schoenebeck |
53 |
#include <errno.h> |
45 |
|
|
|
46 |
schoenebeck |
1221 |
#include "Condition.h" |
47 |
|
|
|
48 |
schoenebeck |
1212 |
namespace LinuxSampler { |
49 |
|
|
|
50 |
schoenebeck |
53 |
/// Abstract base class for classes that need to run in an own thread. |
51 |
|
|
class Thread { |
52 |
|
|
public: |
53 |
schoenebeck |
392 |
Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta); |
54 |
schoenebeck |
53 |
virtual ~Thread(); |
55 |
|
|
virtual int StartThread(); |
56 |
|
|
virtual int StopThread(); |
57 |
schoenebeck |
57 |
virtual int SignalStartThread(); |
58 |
schoenebeck |
53 |
virtual int SignalStopThread(); |
59 |
nagata |
1649 |
|
60 |
persson |
2185 |
void TestCancel(); |
61 |
nagata |
1649 |
|
62 |
schoenebeck |
1212 |
virtual bool IsRunning(); |
63 |
schoenebeck |
53 |
virtual int SetSchedulingPriority(); //FIXME: should be private |
64 |
schoenebeck |
392 |
virtual int LockMemory(); //FIXME: should be private |
65 |
schoenebeck |
53 |
virtual void EnableDestructor(); //FIXME: should be private |
66 |
|
|
virtual int Destructor(); //FIXME: should be private |
67 |
|
|
virtual int Main() = 0; ///< This method needs to be implemented by the descendant and is the entry point for the new thread. FIXME: should be protected |
68 |
senoner |
1481 |
|
69 |
schoenebeck |
1482 |
/** |
70 |
|
|
* Allocates an aligned block of memory. Allocated memory blocks |
71 |
|
|
* need to be freed using freeAlignedMem(). |
72 |
|
|
* |
73 |
|
|
* @param boundary - the alignement boundary, usually a power of 2 |
74 |
|
|
* e.g. 4 but it can be an arbitrary number |
75 |
|
|
* between 1 and 128 |
76 |
|
|
* @param size - size in bytes to be allocated |
77 |
|
|
* @returns pointer to the allocated memory block |
78 |
|
|
*/ |
79 |
senoner |
1481 |
static void* allocAlignedMem(size_t boundary, size_t size) { |
80 |
|
|
unsigned char *ptr = (unsigned char *)malloc(size+boundary); |
81 |
|
|
size_t offset = boundary - ((size_t)ptr % boundary); |
82 |
|
|
ptr[offset-1] = (unsigned char)offset; |
83 |
|
|
return (ptr + offset); |
84 |
|
|
} |
85 |
|
|
|
86 |
schoenebeck |
1482 |
/** |
87 |
|
|
* Frees an aligned block of memory allocated with allocAlignedMem() |
88 |
|
|
* |
89 |
|
|
* @param ptr - pointer to the memory block |
90 |
|
|
*/ |
91 |
senoner |
1481 |
static void freeAlignedMem(void *ptr) { |
92 |
|
|
unsigned char *p = (unsigned char *)ptr; |
93 |
|
|
p -= p[-1]; |
94 |
|
|
free(p); |
95 |
|
|
} |
96 |
schoenebeck |
1482 |
|
97 |
senoner |
1481 |
/** |
98 |
schoenebeck |
1482 |
* Locks a region of memory in physical RAM. |
99 |
|
|
* |
100 |
|
|
* @param addr - address of the memory block |
101 |
|
|
* @param size - size of the memory block |
102 |
|
|
* @return true if the locking succeded, otherwise false |
103 |
|
|
*/ |
104 |
senoner |
1481 |
static bool lockMemory(void *addr, size_t size) { |
105 |
|
|
#if defined(WIN32) |
106 |
|
|
return VirtualLock(addr, size); |
107 |
|
|
#else |
108 |
|
|
return !mlock(addr, size); |
109 |
|
|
#endif |
110 |
|
|
} |
111 |
schoenebeck |
1482 |
|
112 |
senoner |
1481 |
/** |
113 |
schoenebeck |
1482 |
* Unlocks a region of memory in physical RAM. |
114 |
|
|
* |
115 |
|
|
* @param addr - address of the memory block |
116 |
|
|
* @param size - size of the memory block |
117 |
|
|
* @return true if the unlocking succeded, otherwise false |
118 |
|
|
*/ |
119 |
senoner |
1481 |
static bool unlockMemory(void *addr, size_t size) { |
120 |
|
|
#if defined(WIN32) |
121 |
|
|
return VirtualUnlock(addr, size); |
122 |
|
|
#else |
123 |
|
|
return !munlock(addr, size); |
124 |
|
|
#endif |
125 |
|
|
} |
126 |
schoenebeck |
1482 |
|
127 |
schoenebeck |
53 |
private: |
128 |
senoner |
1481 |
#if defined(WIN32) |
129 |
|
|
HANDLE hThread; |
130 |
|
|
DWORD lpThreadId; |
131 |
|
|
#if defined(WIN32_SIGNALSTARTTHREAD_WORKAROUND) |
132 |
|
|
bool win32isRunning; |
133 |
|
|
#endif |
134 |
|
|
#else |
135 |
schoenebeck |
1221 |
pthread_attr_t __thread_attr; |
136 |
schoenebeck |
53 |
pthread_t __thread_id; |
137 |
|
|
pthread_key_t __thread_destructor_key; |
138 |
senoner |
1481 |
#endif |
139 |
schoenebeck |
1221 |
Condition RunningCondition; |
140 |
schoenebeck |
53 |
int PriorityMax; |
141 |
|
|
int PriorityDelta; |
142 |
|
|
bool isRealTime; |
143 |
schoenebeck |
1653 |
bool bLockedMemory; |
144 |
schoenebeck |
53 |
}; |
145 |
|
|
|
146 |
schoenebeck |
1212 |
} // namespace LinuxSampler |
147 |
|
|
|
148 |
schoenebeck |
1424 |
#endif // __LS_THREAD_H__ |