24 |
#ifndef __LS_THREAD_H__ |
#ifndef __LS_THREAD_H__ |
25 |
#define __LS_THREAD_H__ |
#define __LS_THREAD_H__ |
26 |
|
|
27 |
//FIXME: jthis is a temorary solution because of problems with condition variables we use a polling lock in SignalStartThread() |
//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 |
#define WIN32_SIGNALSTARTTHREAD_WORKAROUND 1 |
30 |
|
#endif |
31 |
|
|
32 |
#include <iostream> |
#include <iostream> |
33 |
#include <stdio.h> |
#include <stdio.h> |
62 |
virtual void EnableDestructor(); //FIXME: should be private |
virtual void EnableDestructor(); //FIXME: should be private |
63 |
virtual int Destructor(); //FIXME: should be private |
virtual int Destructor(); //FIXME: should be private |
64 |
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 |
|
|
|
|
/** |
|
|
* allocates an aligned block of memory |
|
|
* allocated memory blocks need to be freed using freeAlignedMem() |
|
|
* @param boundary - the alignement boundary, usually a power of 2, eg 4 but it can be an arbitrary number between 1 and 128 |
|
|
* @param size - size in bytes to be allocated |
|
|
* @return pointer to the allocated memory block |
|
|
*/ |
|
65 |
|
|
66 |
|
/** |
67 |
|
* Allocates an aligned block of memory. Allocated memory blocks |
68 |
|
* need to be freed using freeAlignedMem(). |
69 |
|
* |
70 |
|
* @param boundary - the alignement boundary, usually a power of 2 |
71 |
|
* e.g. 4 but it can be an arbitrary number |
72 |
|
* between 1 and 128 |
73 |
|
* @param size - size in bytes to be allocated |
74 |
|
* @returns pointer to the allocated memory block |
75 |
|
*/ |
76 |
static void* allocAlignedMem(size_t boundary, size_t size) { |
static void* allocAlignedMem(size_t boundary, size_t size) { |
77 |
unsigned char *ptr = (unsigned char *)malloc(size+boundary); |
unsigned char *ptr = (unsigned char *)malloc(size+boundary); |
78 |
size_t offset = boundary - ((size_t)ptr % boundary); |
size_t offset = boundary - ((size_t)ptr % boundary); |
80 |
return (ptr + offset); |
return (ptr + offset); |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
84 |
* frees s a aligned block of memory allocated with allocAlignedMem() |
* Frees an aligned block of memory allocated with allocAlignedMem() |
85 |
* @param ptr - pointer to the memory block |
* |
86 |
*/ |
* @param ptr - pointer to the memory block |
87 |
|
*/ |
88 |
static void freeAlignedMem(void *ptr) { |
static void freeAlignedMem(void *ptr) { |
89 |
unsigned char *p = (unsigned char *)ptr; |
unsigned char *p = (unsigned char *)ptr; |
90 |
p -= p[-1]; |
p -= p[-1]; |
91 |
free(p); |
free(p); |
92 |
} |
} |
93 |
|
|
94 |
/** |
/** |
95 |
* locks a region of memory in physical RAM |
* Locks a region of memory in physical RAM. |
96 |
* @param addr - address of the memory block |
* |
97 |
* @param size- size of the memory block |
* @param addr - address of the memory block |
98 |
* @return true if the locking succeded, otherwise false |
* @param size - size of the memory block |
99 |
*/ |
* @return true if the locking succeded, otherwise false |
100 |
|
*/ |
101 |
static bool lockMemory(void *addr, size_t size) { |
static bool lockMemory(void *addr, size_t size) { |
102 |
#if defined(WIN32) |
#if defined(WIN32) |
103 |
return VirtualLock(addr, size); |
return VirtualLock(addr, size); |
105 |
return !mlock(addr, size); |
return !mlock(addr, size); |
106 |
#endif |
#endif |
107 |
} |
} |
108 |
|
|
109 |
/** |
/** |
110 |
* unlocks a region of memory in physical RAM |
* Unlocks a region of memory in physical RAM. |
111 |
* @param addr - address of the memory block |
* |
112 |
* @param size- size of the memory block |
* @param addr - address of the memory block |
113 |
* @return true if the unlocking succeded, otherwise false |
* @param size - size of the memory block |
114 |
*/ |
* @return true if the unlocking succeded, otherwise false |
115 |
|
*/ |
116 |
static bool unlockMemory(void *addr, size_t size) { |
static bool unlockMemory(void *addr, size_t size) { |
117 |
#if defined(WIN32) |
#if defined(WIN32) |
118 |
return VirtualUnlock(addr, size); |
return VirtualUnlock(addr, size); |
120 |
return !munlock(addr, size); |
return !munlock(addr, size); |
121 |
#endif |
#endif |
122 |
} |
} |
123 |
|
|
|
|
|
124 |
private: |
private: |
125 |
#if defined(WIN32) |
#if defined(WIN32) |
126 |
HANDLE hThread; |
HANDLE hThread; |