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" |
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 |
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) { |
77 |
|
unsigned char *ptr = (unsigned char *)malloc(size+boundary); |
78 |
|
size_t offset = boundary - ((size_t)ptr % boundary); |
79 |
|
ptr[offset-1] = (unsigned char)offset; |
80 |
|
return (ptr + offset); |
81 |
|
} |
82 |
|
|
83 |
|
/** |
84 |
|
* Frees an aligned block of memory allocated with allocAlignedMem() |
85 |
|
* |
86 |
|
* @param ptr - pointer to the memory block |
87 |
|
*/ |
88 |
|
static void freeAlignedMem(void *ptr) { |
89 |
|
unsigned char *p = (unsigned char *)ptr; |
90 |
|
p -= p[-1]; |
91 |
|
free(p); |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
|
* Locks a region of memory in physical RAM. |
96 |
|
* |
97 |
|
* @param addr - address of the memory block |
98 |
|
* @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) { |
102 |
|
#if defined(WIN32) |
103 |
|
return VirtualLock(addr, size); |
104 |
|
#else |
105 |
|
return !mlock(addr, size); |
106 |
|
#endif |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
|
* Unlocks a region of memory in physical RAM. |
111 |
|
* |
112 |
|
* @param addr - address of the memory block |
113 |
|
* @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) { |
117 |
|
#if defined(WIN32) |
118 |
|
return VirtualUnlock(addr, size); |
119 |
|
#else |
120 |
|
return !munlock(addr, size); |
121 |
|
#endif |
122 |
|
} |
123 |
|
|
124 |
private: |
private: |
125 |
|
#if defined(WIN32) |
126 |
|
HANDLE hThread; |
127 |
|
DWORD lpThreadId; |
128 |
|
#if defined(WIN32_SIGNALSTARTTHREAD_WORKAROUND) |
129 |
|
bool win32isRunning; |
130 |
|
#endif |
131 |
|
#else |
132 |
pthread_attr_t __thread_attr; |
pthread_attr_t __thread_attr; |
133 |
pthread_t __thread_id; |
pthread_t __thread_id; |
134 |
pthread_key_t __thread_destructor_key; |
pthread_key_t __thread_destructor_key; |
135 |
|
#endif |
136 |
Condition RunningCondition; |
Condition RunningCondition; |
137 |
int PriorityMax; |
int PriorityMax; |
138 |
int PriorityDelta; |
int PriorityDelta; |