This repository was archived by the owner on Nov 22, 2023. It is now read-only.
forked from gek169/seabass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlockstepthread.h
122 lines (103 loc) · 2.95 KB
/
lockstepthread.h
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Public Domain / CC0 Lock-Step Threading Implementation
Written by Gek (DMHSW) in 2020
Modified for the SEABASS STDLIB in 2023
This is CC0 public domain code.
requires -lpthread
*/
#ifndef LOCKSTEPTHREAD_H
#define LOCKSTEPTHREAD_H
#include <pthread.h>
#include <stdlib.h>
typedef struct {
pthread_mutex_t myMutex;
pthread_barrier_t myBarrier;
pthread_t myThread;
int isThreadLive;
int shouldKillThread;
int state;
void (*execute)(unsigned char*);
unsigned char* argument;
} lsthread;
static inline void init_lsthread(lsthread* t);
static inline void start_lsthread(lsthread* t);
static inline void kill_lsthread(lsthread* t);
static inline void destroy_lsthread(lsthread* t);
static inline void lock(lsthread* t);
static inline void step(lsthread* t);
static void* lsthread_func(void* me_void);
//function declarations
static inline void init_lsthread(lsthread* t){
//t->myMutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_init(&t->myMutex, NULL);
pthread_barrier_init(&t->myBarrier, NULL, 2);
t->isThreadLive = 0;
t->shouldKillThread = 0;
t->state = 0;
t->execute = NULL;
t->argument = NULL;
}
static inline void destroy_lsthread(lsthread* t){
pthread_mutex_destroy(&t->myMutex);
pthread_barrier_destroy(&t->myBarrier);
}
static inline void lock(lsthread* t){
if(t->state == 1)return;//if already locked, nono
if(!t->isThreadLive)return;
pthread_barrier_wait(&t->myBarrier);
if(pthread_mutex_lock(&t->myMutex))
exit(1);
t->state = 1;
}
static inline void step(lsthread* t){
if(t->state == -1)return; //if already stepping, nono
if(!t->isThreadLive)return;
if(pthread_mutex_unlock(&(t->myMutex)))
exit(1);
pthread_barrier_wait(&t->myBarrier);
t->state = -1;
}
static inline void kill_lsthread(lsthread* t){
if(!t->isThreadLive)return;
if(t->state != 1){
lock(t);
}
t->shouldKillThread = 1;
step(t);
pthread_join(t->myThread,NULL);
t->isThreadLive = 0;
t->shouldKillThread = 0;
}
static void* lsthread_func(void* me_void){
lsthread* me = (lsthread*) me_void;
if (!me)pthread_exit(NULL);
while (1) {
pthread_barrier_wait(&me->myBarrier);
pthread_mutex_lock(&me->myMutex);
if (!(me->shouldKillThread) && me->execute)
me->execute(me->argument);
else if(me->shouldKillThread){
pthread_mutex_unlock(&me->myMutex);
pthread_exit(NULL);
}
pthread_mutex_unlock(&me->myMutex);
pthread_barrier_wait(&me->myBarrier);
}
pthread_exit(NULL);
}
static inline void start_lsthread(lsthread* t){
if(t->isThreadLive)return;
t->isThreadLive = 1;
t->shouldKillThread = 0;
if(pthread_mutex_lock(&t->myMutex))
exit(1);
t->state = 1; //LOCKED
pthread_create(
&t->myThread,
NULL,
lsthread_func,
(void*)t
);
}
//end of implementation
#endif
//end of header