25
25
#include < string.h>
26
26
#include < ucontext.h>
27
27
#include < unistd.h>
28
+ #include " ErrorMessage.h"
28
29
29
30
namespace System {
30
31
@@ -40,7 +41,7 @@ class MutextGuard {
40
41
MutextGuard (pthread_mutex_t & _mutex) : mutex(_mutex) {
41
42
auto ret = pthread_mutex_lock (&mutex);
42
43
if (ret != 0 ) {
43
- throw std::runtime_error (" failed to acquire mutex, errno= " + std::to_string (ret) + " : " + strerror (ret));
44
+ throw std::runtime_error (" pthread_mutex_lock failed, " + errorMessage (ret));
44
45
}
45
46
}
46
47
@@ -62,15 +63,15 @@ Dispatcher::Dispatcher() {
62
63
std::string message;
63
64
epoll = ::epoll_create1 (0 );
64
65
if (epoll == -1 ) {
65
- message = " epoll_create1() fail errno= " + std::to_string (errno );
66
+ message = " epoll_create1 failed, " + lastErrorMessage ( );
66
67
} else {
67
68
mainContext.ucontext = new ucontext_t ;
68
69
if (getcontext (reinterpret_cast <ucontext_t *>(mainContext.ucontext )) == -1 ) {
69
- message = " getcontext() fail errno= " + std::to_string (errno );
70
+ message = " getcontext failed, " + lastErrorMessage ( );
70
71
} else {
71
72
remoteSpawnEvent = eventfd (0 , O_NONBLOCK);
72
73
if (remoteSpawnEvent == -1 ) {
73
- message = " eventfd() fail errno= " + std::to_string (errno );
74
+ message = " eventfd failed, " + lastErrorMessage ( );
74
75
} else {
75
76
remoteSpawnEventContext.writeContext = nullptr ;
76
77
remoteSpawnEventContext.readContext = nullptr ;
@@ -80,7 +81,7 @@ Dispatcher::Dispatcher() {
80
81
remoteSpawnEventEpollEvent.data .ptr = &remoteSpawnEventContext;
81
82
82
83
if (epoll_ctl (epoll, EPOLL_CTL_ADD, remoteSpawnEvent, &remoteSpawnEventEpollEvent) == -1 ) {
83
- message = " epoll_ctl() failed, errno= " + std::to_string (errno );
84
+ message = " epoll_ctl failed, " + lastErrorMessage ( );
84
85
} else {
85
86
*reinterpret_cast <pthread_mutex_t *>(this ->mutex ) = pthread_mutex_t (PTHREAD_MUTEX_INITIALIZER);
86
87
@@ -155,7 +156,7 @@ void Dispatcher::clear() {
155
156
while (!timers.empty ()) {
156
157
int result = ::close (timers.top ());
157
158
if (result == -1 ) {
158
- throw std::runtime_error (" Dispatcher::clear, close failed, errno= " + std::to_string (errno ));
159
+ throw std::runtime_error (" Dispatcher::clear, close failed, " + lastErrorMessage ( ));
159
160
}
160
161
161
162
timers.pop ();
@@ -179,7 +180,7 @@ void Dispatcher::dispatch() {
179
180
uint64_t buf;
180
181
auto transferred = read (remoteSpawnEvent, &buf, sizeof buf);
181
182
if (transferred == -1 ) {
182
- throw std::runtime_error (" Dispatcher::dispatch() read(remoteSpawnEvent) fail errno= " + std::to_string (errno ));
183
+ throw std::runtime_error (" Dispatcher::dispatch, read(remoteSpawnEvent) failed, " + lastErrorMessage ( ));
183
184
}
184
185
185
186
MutextGuard guard (*reinterpret_cast <pthread_mutex_t *>(this ->mutex ));
@@ -206,15 +207,15 @@ void Dispatcher::dispatch() {
206
207
}
207
208
208
209
if (errno != EINTR) {
209
- throw std::runtime_error (" Dispatcher::dispatch() , epoll_wait() failed, errno= " + std::to_string (errno ));
210
+ throw std::runtime_error (" Dispatcher::dispatch, epoll_wait failed, " + lastErrorMessage ( ));
210
211
}
211
212
}
212
213
213
214
if (context != currentContext) {
214
215
ucontext_t * oldContext = static_cast <ucontext_t *>(currentContext->ucontext );
215
216
currentContext = context;
216
217
if (swapcontext (oldContext, static_cast <ucontext_t *>(context->ucontext )) == -1 ) {
217
- throw std::runtime_error (" Dispatcher::dispatch() swapcontext() failed, errno= " + std::to_string (errno ));
218
+ throw std::runtime_error (" Dispatcher::dispatch, swapcontext failed, " + lastErrorMessage ( ));
218
219
}
219
220
}
220
221
}
@@ -269,7 +270,7 @@ void Dispatcher::remoteSpawn(std::function<void()>&& procedure) {
269
270
uint64_t one = 1 ;
270
271
auto transferred = write (remoteSpawnEvent, &one, sizeof one);
271
272
if (transferred == - 1 ) {
272
- throw std::runtime_error (" Dispatcher::remoteSpawn, write() failed errno = " + std::to_string (errno ));
273
+ throw std::runtime_error (" Dispatcher::remoteSpawn, write failed, " + lastErrorMessage ( ));
273
274
}
274
275
}
275
276
@@ -308,7 +309,7 @@ void Dispatcher::yield() {
308
309
uint64_t buf;
309
310
auto transferred = read (remoteSpawnEvent, &buf, sizeof buf);
310
311
if (transferred == -1 ) {
311
- throw std::runtime_error (" Dispatcher::dispatch() read(remoteSpawnEvent) fail errno= " + std::to_string (errno ));
312
+ throw std::runtime_error (" Dispatcher::dispatch, read(remoteSpawnEvent) failed, " + lastErrorMessage ( ));
312
313
}
313
314
314
315
MutextGuard guard (*reinterpret_cast <pthread_mutex_t *>(this ->mutex ));
@@ -334,7 +335,7 @@ void Dispatcher::yield() {
334
335
}
335
336
} else {
336
337
if (errno != EINTR) {
337
- throw std::runtime_error (" Dispatcher::dispatch() , epoll_wait() failed, errno= " + std::to_string (errno ));
338
+ throw std::runtime_error (" Dispatcher::dispatch, epoll_wait failed, " + lastErrorMessage ( ));
338
339
}
339
340
}
340
341
}
@@ -353,7 +354,7 @@ NativeContext& Dispatcher::getReusableContext() {
353
354
if (firstReusableContext == nullptr ) {
354
355
ucontext_t * newlyCreatedContext = new ucontext_t ;
355
356
if (getcontext (newlyCreatedContext) == -1 ) { // makecontext precondition
356
- throw std::runtime_error (" Dispatcher::getReusableContext() , getcontext() fail errno= " + std::to_string (errno ));
357
+ throw std::runtime_error (" Dispatcher::getReusableContext, getcontext failed, " + lastErrorMessage ( ));
357
358
}
358
359
359
360
auto stackPointer = new uint8_t [STACK_SIZE];
@@ -365,7 +366,7 @@ NativeContext& Dispatcher::getReusableContext() {
365
366
366
367
ucontext_t * oldContext = static_cast <ucontext_t *>(currentContext->ucontext );
367
368
if (swapcontext (oldContext, newlyCreatedContext) == -1 ) {
368
- throw std::runtime_error (" Dispatcher::getReusableContext() swapcontext() failed, errno= " + std::to_string (errno ));
369
+ throw std::runtime_error (" Dispatcher::getReusableContext, swapcontext failed, " + lastErrorMessage ( ));
369
370
}
370
371
371
372
assert (firstReusableContext != nullptr );
@@ -393,7 +394,7 @@ int Dispatcher::getTimer() {
393
394
timerEvent.data .ptr = nullptr ;
394
395
395
396
if (epoll_ctl (getEpoll (), EPOLL_CTL_ADD, timer, &timerEvent) == -1 ) {
396
- throw std::runtime_error (" Dispatcher::getTimer() , epoll_ctl() failed, errno= " + std::to_string (errno ));
397
+ throw std::runtime_error (" Dispatcher::getTimer, epoll_ctl failed, " + lastErrorMessage ( ));
397
398
}
398
399
} else {
399
400
timer = timers.top ();
@@ -416,7 +417,7 @@ void Dispatcher::contextProcedure(void* ucontext) {
416
417
firstReusableContext = &context;
417
418
ucontext_t * oldContext = static_cast <ucontext_t *>(context.ucontext );
418
419
if (swapcontext (oldContext, static_cast <ucontext_t *>(currentContext->ucontext )) == -1 ) {
419
- throw std::runtime_error (" Dispatcher::contextProcedure() swapcontext() failed, errno= " + std::to_string (errno ));
420
+ throw std::runtime_error (" Dispatcher::contextProcedure, swapcontext failed, " + lastErrorMessage ( ));
420
421
}
421
422
422
423
for (;;) {
0 commit comments