-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.h
More file actions
89 lines (80 loc) · 1.66 KB
/
Copy pathThread.h
File metadata and controls
89 lines (80 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef THREAD_H
#define THREAD_H
#include <pthread.h>
class Thread {
public:
Thread() : thread(0),exited(false),autodelete(false) {};
virtual ~Thread() {};
void start();
static int self();
void cancel(bool join);
bool isRunning();
void testcancel();
// delete *this after run() exits
void setAutodelete() {this->autodelete=true; };
int getId() { return this->thread; };
virtual const char *which() { return ""; };
private:
virtual void run()=0;
pthread_t thread;
static void *startupThread(void *ptr);
bool exited;
bool autodelete;
#ifdef ESP32
int cancelstate; // hat die esp32 lib nicht
#endif
int getMyId() { return pthread_self(); };
};
class Condition;
class Mutex {
public:
Mutex();
~Mutex();
void lock();
void unlock();
/// @return true if lock is successful
bool tryLock();
private:
pthread_mutex_t m;
#ifdef DEBUG_MUTEX
pthread_t lockedby;
#endif
friend class Condition;
};
class Lock {
public:
explicit Lock(Mutex &m) : m(m) { m.lock(); } ;
~Lock() { if(this->needunlock) m.unlock(); } ;
void unlock() {this->needunlock=false; m.unlock(); };
private:
Mutex &m;
bool needunlock=true;
};
class Condition {
public:
Condition();
void wait();
/// true=signal, false=timeout
bool timeoutWait(int timeout);
void signal();
private:
Mutex mutex;
pthread_cond_t cond; // ESP32 bug - geht nicht: = PTHREAD_COND_INITIALIZER;
};
/**
* threadspecific wrapper klasse
* muss global sein!
*/
class ThreadSpecific {
public:
ThreadSpecific();
virtual ~ThreadSpecific();
virtual void *get();
virtual void set(void *ptr);
// TODO: wird beim beenden vom thread aufgerufen
virtual void del();
private:
pthread_key_t key;
// pthread_once_t key_once;
};
#endif