forked from TunSafe/TunSafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunsafe_threading.cpp
165 lines (139 loc) · 3.49 KB
/
tunsafe_threading.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: AGPL-1.0-only
// Copyright (C) 2018 Ludvig Strigeus <info@tunsafe.com>. All Rights Reserved.
#include "stdafx.h"
#include "tunsafe_threading.h"
#include <stdlib.h>
#include <assert.h>
#if defined(OS_POSIX)
Thread::Thread() {
thread_ = 0;
}
Thread::~Thread() {
assert(thread_ == 0);
}
static void *ThreadMainStatic(void *x) {
Thread::Runner *t = (Thread::Runner*)x;
t->ThreadMain();
return 0;
}
void Thread::StartThread(Runner *runner) {
assert(thread_ == 0);
if (pthread_create(&thread_, NULL, &ThreadMainStatic, runner) != 0)
tunsafe_die("pthread_create failed");
}
void Thread::StopThread() {
if (thread_) {
void *x;
pthread_join(thread_, &x);
thread_ = 0;
}
}
void Thread::DetachThread() {
if (thread_) {
pthread_detach(thread_);
thread_ = 0;
}
}
bool Thread::is_started() {
return thread_ != 0;
}
void ConditionVariable::WaitTimed(Mutex *mutex, int millis) {
#if !defined(OS_MACOSX)
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += (millis / 1000);
ts.tv_nsec += (millis % 1000) * 1000000;
if (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
ts.tv_sec++;
}
pthread_cond_timedwait(&condvar_, &mutex->lock_, &ts);
#else
struct timespec ts;
ts.tv_sec = millis / 1000;
ts.tv_nsec = (millis % 1000) * 1000000;
pthread_cond_timedwait_relative_np(&condvar_, &mutex->lock_, &ts);
#endif
}
#endif // defined(OS_POSIX)
#if defined(OS_WIN)
Thread::Thread() {
thread_ = 0;
}
Thread::~Thread() {
assert(thread_ == 0);
}
static DWORD WINAPI ThreadMainStatic(void *x) {
Thread::Runner *t = (Thread::Runner*)x;
t->ThreadMain();
return 0;
}
void Thread::StartThread(Runner *runner) {
assert(thread_ == 0);
DWORD thread_id;
thread_ = CreateThread(NULL, 0, &ThreadMainStatic, (LPVOID)runner, 0, &thread_id);
}
void Thread::StopThread() {
if (thread_) {
WaitForSingleObject(thread_, INFINITE);
CloseHandle(thread_);
thread_ = 0;
}
}
void Thread::DetachThread() {
if (thread_) {
CloseHandle(thread_);
thread_ = 0;
}
}
bool Thread::is_started() {
return thread_ != 0;
}
#endif
MultithreadedDelayedDelete::MultithreadedDelayedDelete() {
table_ = NULL;
num_threads_ = 0;
}
MultithreadedDelayedDelete::~MultithreadedDelayedDelete() {
assert(curr_.size() == 0);
assert(next_.size() == 0);
assert(to_delete_.size() == 0);
free(table_);
}
void MultithreadedDelayedDelete::Configure(uint32 num_threads) {
assert(table_ == NULL);
num_threads_ = num_threads;
table_ = (CheckpointData*)calloc(sizeof(CheckpointData), num_threads);
}
void MultithreadedDelayedDelete::Add(DoDeleteFunc *func, void *param) {
if (num_threads_ == 0) {
func(param);
return;
}
lock_.Acquire();
Entry e = {func, param};
curr_.push_back(e);
lock_.Release();
}
void MultithreadedDelayedDelete::Checkpoint(uint32 thread_id) {
table_[thread_id].value.store(1);
}
void MultithreadedDelayedDelete::MainCheckpoint() {
// Wait for all threads to signal that they reached the checkpoint
for (size_t i = 0; i < num_threads_; i++) {
if (table_[i].value.load() == 0)
return;
}
// All threads reached the checkpoint, clear the values
for (size_t i = 0; i < num_threads_; i++)
table_[i].value.store(0);
// Swap curr and next, and delete all nexts.
lock_.Acquire();
std::swap(curr_, next_);
std::swap(curr_, to_delete_);
lock_.Release();
for (auto it = to_delete_.begin(); it != to_delete_.end(); ++it) {
it->func(it->param);
}
to_delete_.clear();
}