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

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

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

revision 1424 by schoenebeck, Sun Oct 14 22:00:17 2007 UTC revision 1649 by nagata, Fri Jan 25 15:06:02 2008 UTC
# Line 24  Line 24 
24  #ifndef __LS_THREAD_H__  #ifndef __LS_THREAD_H__
25  #define __LS_THREAD_H__  #define __LS_THREAD_H__
26    
27    //FIXME: this is a temorary solution because of problems with condition variables we use a polling lock in SignalStartThread()
28    #if defined(WIN32)
29    #define WIN32_SIGNALSTARTTHREAD_WORKAROUND 1
30    #endif
31    
32  #include <iostream>  #include <iostream>
33  #include <stdio.h>  #include <stdio.h>
34  #include <stdlib.h>  #include <stdlib.h>
35    
36    #if defined(WIN32)
37    #include <windows.h>
38    #else
39  #include <sched.h>  #include <sched.h>
40  #include <sys/mman.h>  #include <sys/mman.h>
41  #include <memory.h>  #include <memory.h>
42  #include <pthread.h>  #include <pthread.h>
43    #endif
44  #include <errno.h>  #include <errno.h>
45    
46  #include "Condition.h"  #include "Condition.h"
# Line 46  class Thread { Line 56  class Thread {
56          virtual int  StopThread();          virtual int  StopThread();
57          virtual int  SignalStartThread();          virtual int  SignalStartThread();
58          virtual int  SignalStopThread();          virtual int  SignalStopThread();
59    
60                    inline int TestCancel() {
61    #if CONFIG_PTHREAD_TESTCANCEL
62                            pthread_testcancel();
63    #endif
64                            return 0;
65                    }
66    
67          virtual bool IsRunning();          virtual bool IsRunning();
68          virtual int  SetSchedulingPriority(); //FIXME: should be private          virtual int  SetSchedulingPriority(); //FIXME: should be private
69          virtual int  LockMemory();            //FIXME: should be private          virtual int  LockMemory();            //FIXME: should be private
70          virtual void EnableDestructor();      //FIXME: should be private          virtual void EnableDestructor();      //FIXME: should be private
71          virtual int  Destructor();            //FIXME: should be private          virtual int  Destructor();            //FIXME: should be private
72          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          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
73    
74            /**
75             * Allocates an aligned block of memory. Allocated memory blocks
76             * need to be freed using freeAlignedMem().
77             *
78             * @param boundary - the alignement boundary, usually a power of 2
79             *                   e.g. 4 but it can be an arbitrary number
80             *                   between 1 and 128
81             * @param size     - size in bytes to be allocated
82             * @returns  pointer to the allocated memory block
83             */
84            static void* allocAlignedMem(size_t boundary, size_t size) {
85                unsigned char *ptr = (unsigned char *)malloc(size+boundary);
86                size_t offset = boundary - ((size_t)ptr % boundary);
87                ptr[offset-1] = (unsigned char)offset;
88                return (ptr + offset);
89            }
90    
91            /**
92             * Frees an aligned block of memory allocated with allocAlignedMem()
93             *
94             * @param ptr - pointer to the memory block
95             */
96            static void freeAlignedMem(void *ptr) {
97                unsigned char *p = (unsigned char *)ptr;
98                p -= p[-1];
99                free(p);
100            }
101    
102            /**
103             * Locks a region of memory in physical RAM.
104             *
105             * @param addr - address of the memory block
106             * @param size - size of the memory block
107             * @return true if the locking succeded, otherwise false
108             */
109            static bool lockMemory(void *addr, size_t size) {
110                #if defined(WIN32)
111                return VirtualLock(addr, size);
112                #else
113                return !mlock(addr, size);
114                #endif
115            }
116    
117            /**
118             * Unlocks a region of memory in physical RAM.
119             *
120             * @param addr - address of the memory block
121             * @param size - size of the memory block
122             * @return true if the unlocking succeded, otherwise false
123             */
124            static bool unlockMemory(void *addr, size_t size) {
125                #if defined(WIN32)
126                return VirtualUnlock(addr, size);
127                #else
128                return !munlock(addr, size);
129                #endif
130            }
131    
132      private:      private:
133        #if defined(WIN32)
134            HANDLE hThread;
135            DWORD lpThreadId;
136            #if defined(WIN32_SIGNALSTARTTHREAD_WORKAROUND)
137            bool win32isRunning;
138            #endif
139        #else
140          pthread_attr_t  __thread_attr;          pthread_attr_t  __thread_attr;
141          pthread_t       __thread_id;          pthread_t       __thread_id;
142          pthread_key_t   __thread_destructor_key;          pthread_key_t   __thread_destructor_key;
143        #endif
144          Condition       RunningCondition;          Condition       RunningCondition;
145          int             PriorityMax;          int             PriorityMax;
146          int             PriorityDelta;          int             PriorityDelta;
147          bool            isRealTime;          bool            isRealTime;
148          bool            bLockedMemory;          bool            bLockedMemory;  
149  };  };
150    
151  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.1424  
changed lines
  Added in v.1649

  ViewVC Help
Powered by ViewVC