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

Annotation of /linuxsampler/trunk/src/common/Thread.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1481 - (hide annotations) (download) (as text)
Wed Nov 14 23:42:15 2007 UTC (16 years, 5 months ago) by senoner
File MIME type: text/x-c++hdr
File size: 5610 byte(s)
* win32 port work in progress:
* - implemented win32 support in the following classes:
* Thread, Condition, Mutex, Path, LscpServer
* - lscp.y use DONTCARE instead of VOID
*  (a win32 symbol defined)
* - completed win32 editor plugin loader

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 schoenebeck 1212 * Copyright (C) 2005 - 2007 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 senoner 1481 //FIXME: jthis is a temorary solution because of problems with condition variables we use a polling lock in SignalStartThread()
28     #define WIN32_SIGNALSTARTTHREAD_WORKAROUND 1
29    
30 schoenebeck 53 #include <iostream>
31     #include <stdio.h>
32     #include <stdlib.h>
33 senoner 1481
34     #if defined(WIN32)
35     #include <windows.h>
36     #else
37 schoenebeck 53 #include <sched.h>
38     #include <sys/mman.h>
39     #include <memory.h>
40     #include <pthread.h>
41 senoner 1481 #endif
42 schoenebeck 53 #include <errno.h>
43    
44 schoenebeck 1221 #include "Condition.h"
45    
46 schoenebeck 1212 namespace LinuxSampler {
47    
48 schoenebeck 53 /// Abstract base class for classes that need to run in an own thread.
49     class Thread {
50     public:
51 schoenebeck 392 Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta);
52 schoenebeck 53 virtual ~Thread();
53     virtual int StartThread();
54     virtual int StopThread();
55 schoenebeck 57 virtual int SignalStartThread();
56 schoenebeck 53 virtual int SignalStopThread();
57 schoenebeck 1212 virtual bool IsRunning();
58 schoenebeck 53 virtual int SetSchedulingPriority(); //FIXME: should be private
59 schoenebeck 392 virtual int LockMemory(); //FIXME: should be private
60 schoenebeck 53 virtual void EnableDestructor(); //FIXME: should be private
61     virtual int Destructor(); //FIXME: should be private
62     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
63 senoner 1481
64     /**
65     * allocates an aligned block of memory
66     * allocated memory blocks need to be freed using freeAlignedMem()
67     * @param boundary - the alignement boundary, usually a power of 2, eg 4 but it can be an arbitrary number between 1 and 128
68     * @param size - size in bytes to be allocated
69     * @return pointer to the allocated memory block
70     */
71    
72     static void* allocAlignedMem(size_t boundary, size_t size) {
73     unsigned char *ptr = (unsigned char *)malloc(size+boundary);
74     size_t offset = boundary - ((size_t)ptr % boundary);
75     ptr[offset-1] = (unsigned char)offset;
76     return (ptr + offset);
77     }
78    
79     /**
80     * frees s a aligned block of memory allocated with allocAlignedMem()
81     * @param ptr - pointer to the memory block
82     */
83     static void freeAlignedMem(void *ptr) {
84     unsigned char *p = (unsigned char *)ptr;
85     p -= p[-1];
86     free(p);
87     }
88    
89     /**
90     * locks a region of memory in physical RAM
91     * @param addr - address of the memory block
92     * @param size- size of the memory block
93     * @return true if the locking succeded, otherwise false
94     */
95     static bool lockMemory(void *addr, size_t size) {
96     #if defined(WIN32)
97     return VirtualLock(addr, size);
98     #else
99     return !mlock(addr, size);
100     #endif
101     }
102    
103     /**
104     * unlocks a region of memory in physical RAM
105     * @param addr - address of the memory block
106     * @param size- size of the memory block
107     * @return true if the unlocking succeded, otherwise false
108     */
109     static bool unlockMemory(void *addr, size_t size) {
110     #if defined(WIN32)
111     return VirtualUnlock(addr, size);
112     #else
113     return !munlock(addr, size);
114     #endif
115     }
116    
117    
118 schoenebeck 53 private:
119 senoner 1481 #if defined(WIN32)
120     HANDLE hThread;
121     DWORD lpThreadId;
122     #if defined(WIN32_SIGNALSTARTTHREAD_WORKAROUND)
123     bool win32isRunning;
124     #endif
125     #else
126 schoenebeck 1221 pthread_attr_t __thread_attr;
127 schoenebeck 53 pthread_t __thread_id;
128     pthread_key_t __thread_destructor_key;
129 senoner 1481 #endif
130 schoenebeck 1221 Condition RunningCondition;
131 schoenebeck 53 int PriorityMax;
132     int PriorityDelta;
133     bool isRealTime;
134 schoenebeck 392 bool bLockedMemory;
135 schoenebeck 53 };
136    
137 schoenebeck 1212 } // namespace LinuxSampler
138    
139 schoenebeck 1424 #endif // __LS_THREAD_H__

  ViewVC Help
Powered by ViewVC