From 482e41e4112c2fa9f8c2a067c77d8014b84c391e Mon Sep 17 00:00:00 2001 From: Florian Schmaus <flow@cs.fau.de> Date: Wed, 12 Jul 2017 15:28:07 +0200 Subject: [PATCH] Add DIE_MSG_ERRNO() macro --- emper/Common.cpp | 9 +++++++-- emper/Common.hpp | 7 ++++--- emper/Runtime.cpp | 16 ++++++++-------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/emper/Common.cpp b/emper/Common.cpp index 0f94fab5..fadfeb29 100644 --- a/emper/Common.cpp +++ b/emper/Common.cpp @@ -1,10 +1,15 @@ #include <stdio.h> #include <cstdlib> #include <iostream> +#include <cstdio> #include "Common.hpp" -void die(const char* message) { - std::cout << message << std::endl; +void die(const char* message, bool usePerror) { + if (usePerror) { + std::perror(message); + } else { + std::cout << message << std::endl; + } exit(EXIT_FAILURE); } diff --git a/emper/Common.hpp b/emper/Common.hpp index 29fcfac9..a93a8017 100644 --- a/emper/Common.hpp +++ b/emper/Common.hpp @@ -8,14 +8,15 @@ typedef std::function<void(void)> func_t; #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) -#define DIE die(__FILE__ ":" TOSTRING(__LINE__)) -#define DIE_MSG(x) die(__FILE__ ":" TOSTRING(__LINE__) "-" #x) +#define DIE die(__FILE__ ":" TOSTRING(__LINE__), false) +#define DIE_MSG(x) die(__FILE__ ":" TOSTRING(__LINE__) "-" #x, false) +#define DIE_MSG_ERRNO(x) die(__FILE__ ":" TOSTRING(__LINE__) "-" #x, true) #define likely(x) __builtin_expect(!!(x),1) #define unlikely(x) __builtin_expect(!!(x),0) #define ALIGN_TO_CACHE_LINE alignas(64) -void die(const char* message); +void die(const char* message, bool usePerror); typedef unsigned int WORD; diff --git a/emper/Runtime.cpp b/emper/Runtime.cpp index 7692c928..3f942ee6 100644 --- a/emper/Runtime.cpp +++ b/emper/Runtime.cpp @@ -52,7 +52,7 @@ Runtime::Runtime(workerid_t workerCount, RuntimeStrategy& strategy) : workerCoun CPU_SET(i % nprocs, &cpuset); errno = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset); - if (errno) DIE_MSG("pthread_attr_setaffinity_np() failed"); + if (errno) DIE_MSG_ERRNO("pthread_attr_setaffinity_np() failed"); // End non portable. // Load the worker ID into the worker ID map. @@ -60,16 +60,16 @@ Runtime::Runtime(workerid_t workerCount, RuntimeStrategy& strategy) : workerCoun auto thread_function = [](void* voidWorkerId) -> void* { return currentRuntime->workerLoop(voidWorkerId); }; errno = pthread_create(&threads[i], &attr, thread_function, &workerIds[i]); - if (errno) DIE_MSG("pthread_create() failed"); + if (errno) DIE_MSG_ERRNO("pthread_create() failed"); } } Runtime::~Runtime() { DBG("Runtime " << this << " is terminating"); for (workerid_t i = 0; i < workerCount; ++i) { - int res = pthread_cancel(threads[i]); - if (res) { - DIE_MSG("pthread_cancel() failed"); + errno = pthread_cancel(threads[i]); + if (errno) { + DIE_MSG_ERRNO("pthread_cancel() failed"); } } { @@ -84,9 +84,9 @@ void* Runtime::workerLoop(void* voidWorkerId) { LOGD("Worker loop started by thread " << syscall(SYS_gettid)); int oldType; - int res = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldType); - if (res) { - DIE_MSG("pthread_setcanceltype() failed"); + errno = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldType); + if (errno) { + DIE_MSG_ERRNO("pthread_setcanceltype() failed"); } // Initialze the workers PRNG seed. -- GitLab