-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunix_system.cpp
270 lines (228 loc) · 7.75 KB
/
unix_system.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "unix_system.h"
#include <dirent.h>
#include <limits.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <algorithm>
#include <condition_variable>
#include <functional>
#include <iostream>
#include <mutex>
#include <sstream>
#include <fmt/core.h>
#include "logging_policy.h"
namespace pivid {
namespace {
inline ErrnoOr<int> run_sys(std::function<int()> f) {
ErrnoOr<int> ret;
ret.value = f();
ret.err = errno;
if (ret.value < 0 && ret.err == EINTR) return run_sys(f);
return (ret.value >= 0) ? ErrnoOr<int>{0, ret.value} : ret;
}
class FileDescriptorDef : public FileDescriptor {
public:
FileDescriptorDef(int fd) : fd(fd) {}
virtual ~FileDescriptorDef() final { ::close(fd); }
virtual int raw_fd() const final { return fd; }
virtual ErrnoOr<int> read(void* buf, size_t len) final {
return run_sys([&] {return ::read(fd, buf, len);});
}
virtual ErrnoOr<int> write(void const* buf, size_t len) final {
return run_sys([&] {return ::write(fd, buf, len);});
}
virtual ErrnoOr<int> ioctl(uint32_t nr, void* buf) final {
return run_sys([&] {return ::ioctl(fd, nr, buf);});
}
virtual ErrnoOr<std::shared_ptr<void>> mmap(
size_t len, int prot, int flags, off_t off
) final {
void* const mem = ::mmap(nullptr, len, prot, flags, fd, off);
if (mem == MAP_FAILED) return {errno, {}};
return {0, {mem, [len](void* m) {::munmap(m, len);}}};
}
private:
int fd = -1;
};
class SyncFlagDef : public SyncFlag {
public:
SyncFlagDef(clockid_t clockid) {
pthread_condattr_t attr;
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, clockid);
pthread_cond_init(&cond, &attr);
pthread_condattr_destroy(&attr);
}
virtual ~SyncFlagDef() final { pthread_cond_destroy(&cond); }
virtual void set() final {
pthread_mutex_lock(&mutex);
if (!wake_flag) {
wake_flag = true;
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
}
virtual void sleep() final {
pthread_mutex_lock(&mutex);
while (!wake_flag)
pthread_cond_wait(&cond, &mutex);
wake_flag = false;
pthread_mutex_unlock(&mutex);
}
virtual bool sleep_until(double t) final {
struct timespec ts;
ts.tv_sec = t;
ts.tv_nsec = (t - ts.tv_sec) * 1e9;
pthread_mutex_lock(&mutex);
while (!wake_flag) {
if (pthread_cond_timedwait(&cond, &mutex, &ts) == ETIMEDOUT) {
pthread_mutex_unlock(&mutex);
return false;
}
}
wake_flag = false;
pthread_mutex_unlock(&mutex);
return true;
}
private:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond;
bool wake_flag = false;
};
class UnixSystemDef : public UnixSystem {
public:
virtual double clock(clockid_t clockid) const final {
struct timespec ts;
clock_gettime(clockid, &ts);
return ts.tv_sec + 1e-9 * ts.tv_nsec;
}
virtual std::unique_ptr<SyncFlag> make_flag(clockid_t clockid) const final {
return std::make_unique<SyncFlagDef>(clockid);
}
virtual ErrnoOr<std::vector<std::string>> ls(
std::string const& dir
) const final {
std::unique_ptr<DIR, int (*)(DIR*)> dp(opendir(dir.c_str()), closedir);
if (!dp) return {errno, {}};
ErrnoOr<std::vector<std::string>> ret;
while (dirent* ent = readdir(dp.get()))
ret.value.push_back(ent->d_name);
std::sort(ret.value.begin(), ret.value.end());
return ret;
}
virtual ErrnoOr<struct stat> stat(std::string const& path) const final {
ErrnoOr<struct stat> ret;
ret.err = run_sys([&] {return ::stat(path.c_str(), &ret.value);}).err;
return ret;
}
virtual ErrnoOr<std::string> realpath(std::string const& path) const final {
char buf[PATH_MAX];
if (!::realpath(path.c_str(), buf)) return {errno, {}};
return {0, buf};
}
virtual ErrnoOr<std::unique_ptr<FileDescriptor>> open(
std::string const& path, int flags, mode_t mode
) final {
auto const r = run_sys([&] {return ::open(path.c_str(), flags, mode);});
if (r.value < 0) return {r.err ? r.err : EBADF, {}};
return {0, adopt(r.value)};
}
virtual std::unique_ptr<FileDescriptor> adopt(int raw_fd) final {
return std::make_unique<FileDescriptorDef>(raw_fd);
}
virtual ErrnoOr<pid_t> spawn(
std::string const& command,
std::vector<std::string> const& argv,
posix_spawn_file_actions_t const* actions,
posix_spawnattr_t const* attr,
std::optional<std::vector<std::string>> const& envp
) final {
auto const c_vector = [](std::vector<std::string> const& vec) {
std::vector<char*> ret;
for (auto& s : vec) ret.push_back(const_cast<char*>(s.c_str()));
return ret;
};
pid_t pid = 0;
auto const r = run_sys([&] {
return ::posix_spawnp(
&pid, command.c_str(), actions, attr,
c_vector(argv).data(),
envp ? c_vector(*envp).data() : environ
);
});
return {r.err, pid};
}
virtual ErrnoOr<siginfo_t> wait(idtype_t idtype, id_t id, int flags) final {
siginfo_t s = {};
auto const r = run_sys([&] { return ::waitid(idtype, id, &s, flags); });
return {r.err, s};
}
};
} // namespace
std::shared_ptr<UnixSystem> global_system() {
static const auto system = std::make_shared<UnixSystemDef>();
return system;
}
double parse_realtime(std::string const& s) {
size_t end;
double const d = std::stod(s, &end);
if (!s.empty() && end >= s.size())
return d;
int year = 0, mon = 0, day = 0, hr = 0, min = 0, sec = 0, tzh = 0, tzm = 0;
char sep = '\0', tzsep = '\0';
char frac[21] = "";
int const scanned = sscanf(
s.c_str(), "%d-%d-%d%c %d:%d:%d%20[,.0-9]%c%d:%d",
&year, &mon, &day, &sep, &hr, &min, &sec, frac, &tzsep, &tzh, &tzm
);
CHECK_ARG(scanned >= 7, "Bad date format: \"{}\"", s);
CHECK_ARG(sep == 'T' || sep == ' ', "Bad date separator: \"{}\"", s);
struct tm parts = {};
parts.tm_sec = sec;
parts.tm_min = min;
parts.tm_hour = hr;
parts.tm_mday = day;
parts.tm_mon = mon - 1;
parts.tm_year = year - 1900;
time_t const tt = timegm(&parts);
CHECK_ARG(tt != (time_t) -1, "Date overflow: \"{}\"", s);
double extra = 0.0;
if (scanned >= 8 && frac[0] != '\0') {
CHECK_ARG(frac[0] == '.' || frac[0] == ',', "Bad fraction: \"{}\"", s);
frac[0] = '.';
extra = atof(frac);
}
int offset = 0;
if (scanned >= 9) {
if (tzsep == 'Z' || tzsep == 'z') {
CHECK_ARG(scanned == 9, "Bad UTC date: \"{}\"", s);
} else {
CHECK_ARG(tzsep == '+' || tzsep == '-', "Bad TZ format: \"{}\"", s);
CHECK_ARG(scanned == 11, "Bad TZ offset: \"{}\"", s);
offset = (tzsep == '-' ? -1 : 1) * (tzh * 3600 + tzm * 60);
}
}
return tt + extra - offset;
}
std::string format_realtime(double t) {
struct tm parts = {};
time_t tt = t;
gmtime_r(&tt, &parts);
return fmt::format(
"{}-{:02d}-{:02d} {:02d}:{:02d}:{:06.3f}Z",
parts.tm_year + 1900, parts.tm_mon + 1, parts.tm_mday,
parts.tm_hour, parts.tm_min, parts.tm_sec + (t - tt)
);
}
std::string abbrev_realtime(double t) {
struct tm parts = {};
time_t tt = t;
gmtime_r(&tt, &parts);
return fmt::format(
"{:02d}:{:02d}:{:06.3f}Z",
parts.tm_hour, parts.tm_min, parts.tm_sec + (t - tt)
);
}
} // namespace pivid