diff --git a/include/maxscale/monitor.hh b/include/maxscale/monitor.hh index 72158bc7a..c4bc377b9 100644 --- a/include/maxscale/monitor.hh +++ b/include/maxscale/monitor.hh @@ -15,8 +15,8 @@ #include #include +#include #include -#include #include namespace maxscale @@ -209,10 +209,10 @@ protected: private: std::atomic m_thread_running; /**< Thread state. Only visible inside MonitorInstance. */ - int32_t m_shutdown; /**< Non-zero if the monitor should shut down. */ - bool m_checked; /**< Whether server access has been checked. */ - Semaphore m_semaphore; /**< Semaphore for synchronizing with monitor thread. */ - int64_t m_loop_called; /**< When was the loop called the last time. */ + int32_t m_shutdown; /**< Non-zero if the monitor should shut down. */ + bool m_checked; /**< Whether server access has been checked. */ + mxb::Semaphore m_semaphore; /**< Semaphore for synchronizing with monitor thread. */ + int64_t m_loop_called; /**< When was the loop called the last time. */ bool pre_run() final; void post_run() final; diff --git a/include/maxscale/worker.hh b/include/maxscale/worker.hh index a3e76bb58..651f56784 100644 --- a/include/maxscale/worker.hh +++ b/include/maxscale/worker.hh @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -31,8 +32,6 @@ namespace maxscale { -class Semaphore; - struct WORKER_STATISTICS { WORKER_STATISTICS() @@ -728,7 +727,7 @@ public: * The semaphore can be used for waiting for the task to be finished. * * @code - * Semaphore sem; + * mxb::Semaphore sem; * MyTask task; * * pWorker->execute(&task, &sem); @@ -737,7 +736,7 @@ public: * MyResult& result = task.result(); * @endcode */ - bool execute(Task* pTask, Semaphore* pSem, enum execute_mode_t mode); + bool execute(Task* pTask, mxb::Semaphore* pSem, enum execute_mode_t mode); bool execute(Task* pTask, enum execute_mode_t mode) { @@ -765,7 +764,7 @@ public: * * @return True, if task was posted to the worker */ - bool execute(std::function func, Semaphore* pSem, enum execute_mode_t mode); + bool execute(std::function func, mxb::Semaphore* pSem, enum execute_mode_t mode); bool execute(std::function func, enum execute_mode_t mode) { diff --git a/maxutils/maxbase/include/maxbase/assert.h b/maxutils/maxbase/include/maxbase/assert.h index 55320a773..60e9203ca 100644 --- a/maxutils/maxbase/include/maxbase/assert.h +++ b/maxutils/maxbase/include/maxbase/assert.h @@ -31,7 +31,7 @@ MXB_BEGIN_DECLS #define mxb_assert_message(exp,message) do { if(!(exp)){ \ const char *debug_expr = #exp; \ - MXS_ERROR("debug assert at %s:%d failed: %s (%s)\n", (char*)__FILE__, __LINE__, message, debug_expr); \ + MXB_ERROR("debug assert at %s:%d failed: %s (%s)\n", (char*)__FILE__, __LINE__, message, debug_expr); \ fprintf(stderr, "debug assert at %s:%d failed: %s (%s)\n", (char*)__FILE__, __LINE__, message, debug_expr); \ raise(SIGABRT);} } while (false) diff --git a/include/maxscale/semaphore.hh b/maxutils/maxbase/include/maxbase/semaphore.hh similarity index 94% rename from include/maxscale/semaphore.hh rename to maxutils/maxbase/include/maxbase/semaphore.hh index 91b0b58fb..6b72d9022 100644 --- a/include/maxscale/semaphore.hh +++ b/maxutils/maxbase/include/maxbase/semaphore.hh @@ -12,19 +12,19 @@ * Public License. */ -#include +#include #include #include +#include #include -#include -namespace maxscale +namespace maxbase { class Semaphore { - Semaphore(const Semaphore&); - Semaphore& operator = (const Semaphore&); + Semaphore(const Semaphore&) = delete; + Semaphore& operator = (const Semaphore&) = delete; public: enum signal_approach_t @@ -48,8 +48,8 @@ public: initial_count = SEM_VALUE_MAX; } - ss_debug(int rc =) sem_init(&m_sem, 0, initial_count); - ss_dassert(rc == 0); + MXB_AT_DEBUG(int rc =) sem_init(&m_sem, 0, initial_count); + mxb_assert(rc == 0); } /** @@ -63,11 +63,11 @@ public: #ifdef SS_DEBUG int count; int rc = sem_getvalue(&m_sem, &count); - ss_dassert(rc == 0); - ss_dassert(count == 0); + mxb_assert(rc == 0); + mxb_assert(count == 0); #endif - ss_debug(rc =) sem_destroy(&m_sem); - ss_dassert(rc == 0); + MXB_AT_DEBUG(rc =) sem_destroy(&m_sem); + mxb_assert(rc == 0); } /** @@ -83,11 +83,11 @@ public: bool post() const { int rc = sem_post(&m_sem); - ss_dassert((rc == 0) || (errno == EOVERFLOW)); + mxb_assert((rc == 0) || (errno == EOVERFLOW)); #ifdef SS_DEBUG if ((rc != 0) && (errno == EOVERFLOW)) { - ss_info_dassert(!true, "Semaphore overflow; indicates endless loop."); + mxb_assert_message(!true, "Semaphore overflow; indicates endless loop."); } #endif return rc == 0; @@ -116,7 +116,7 @@ public: } while ((rc != 0) && ((errno == EINTR) && (signal_approach == IGNORE_SIGNALS))); - ss_dassert((rc == 0) || ((errno == EINTR) && (signal_approach == HONOUR_SIGNALS))); + mxb_assert((rc == 0) || ((errno == EINTR) && (signal_approach == HONOUR_SIGNALS))); return rc == 0; } @@ -181,7 +181,7 @@ public: } while ((rc != 0) && ((errno == EINTR) && (signal_approach == IGNORE_SIGNALS))); - ss_dassert((rc == 0) || + mxb_assert((rc == 0) || (errno == EAGAIN) || ((errno == EINTR) && (signal_approach == HONOUR_SIGNALS))); @@ -217,7 +217,7 @@ public: } while ((rc != 0) && ((errno == EINTR) && (signal_approach == IGNORE_SIGNALS))); - ss_dassert((rc == 0) || + mxb_assert((rc == 0) || (errno == ETIMEDOUT) || ((errno == EINTR) && (signal_approach == HONOUR_SIGNALS))); diff --git a/maxutils/maxbase/src/CMakeLists.txt b/maxutils/maxbase/src/CMakeLists.txt index f41846ca3..5ffbeb76f 100644 --- a/maxutils/maxbase/src/CMakeLists.txt +++ b/maxutils/maxbase/src/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(maxbase STATIC eventcount.cc log.cc logger.cc + semaphore.cc stopwatch.cc string.cc stacktrace.cc diff --git a/server/core/semaphore.cc b/maxutils/maxbase/src/semaphore.cc similarity index 82% rename from server/core/semaphore.cc rename to maxutils/maxbase/src/semaphore.cc index 7342e245c..6e540fae4 100644 --- a/server/core/semaphore.cc +++ b/maxutils/maxbase/src/semaphore.cc @@ -11,10 +11,10 @@ * Public License. */ -#include +#include #include -namespace maxscale +namespace maxbase { //static @@ -22,12 +22,12 @@ void Semaphore::get_current_timespec(time_t seconds, long nseconds, timespec* pTs) { - ss_dassert(nseconds <= 999999999); + mxb_assert(nseconds <= 999999999); timespec& ts = *pTs; - ss_debug(int rc = ) clock_gettime(CLOCK_REALTIME, &ts); - ss_dassert(rc == 0); + MXB_AT_DEBUG(int rc = ) clock_gettime(CLOCK_REALTIME, &ts); + mxb_assert(rc == 0); ts.tv_sec += seconds; diff --git a/maxutils/maxbase/src/test/CMakeLists.txt b/maxutils/maxbase/src/test/CMakeLists.txt index 680701a94..411c6a217 100644 --- a/maxutils/maxbase/src/test/CMakeLists.txt +++ b/maxutils/maxbase/src/test/CMakeLists.txt @@ -2,3 +2,7 @@ add_executable(test_mxb_log test_log.cc) target_link_libraries(test_mxb_log maxbase) add_test(test_mxb_log test_mxb_log) +add_executable(test_semaphore test_semaphore.cc) +target_link_libraries(test_semaphore maxbase pthread) +add_test(test_semaphore test_semaphore) + diff --git a/server/core/test/test_semaphore.cc b/maxutils/maxbase/src/test/test_semaphore.cc similarity index 76% rename from server/core/test/test_semaphore.cc rename to maxutils/maxbase/src/test/test_semaphore.cc index 3d215f6b3..eb895117f 100644 --- a/server/core/test/test_semaphore.cc +++ b/maxutils/maxbase/src/test/test_semaphore.cc @@ -18,14 +18,14 @@ #undef NDEBUG #endif -#include -#include -#include -#include +#include #include -#include +#include +#include +#include +#include -using namespace maxscale; +using namespace maxbase; using namespace std; namespace @@ -38,18 +38,18 @@ void test_simple() cout << "Waiting for semaphore with a count of 1." << endl; rv = sem1.wait(); - ss_dassert(rv); + mxb_assert(rv); cout << "Waited" << endl; Semaphore sem2(3); cout << "Waiting 3 times for semaphore with a count of 3." << endl; rv = sem2.wait(); - ss_dassert(rv); + mxb_assert(rv); rv = sem2.wait(); - ss_dassert(rv); + mxb_assert(rv); rv = sem2.wait(); - ss_dassert(rv); + mxb_assert(rv); cout << "Waited" << endl; sem2.post(); @@ -58,11 +58,11 @@ void test_simple() cout << "Waiting 3 times for semaphore with a count of 3." << endl; rv = sem2.wait(); - ss_dassert(rv); + mxb_assert(rv); rv = sem2.wait(); - ss_dassert(rv); + mxb_assert(rv); rv = sem2.wait(); - ss_dassert(rv); + mxb_assert(rv); cout << "Waited" << endl; sem2.post(); @@ -84,8 +84,8 @@ void test_simple() rv = sem3.timedwait(4); finished = time(NULL); diff = finished - started; - ss_dassert(!rv); - ss_dassert((diff >= 2) && (diff <= 4)); + mxb_assert(!rv); + mxb_assert((diff >= 2) && (diff <= 4)); cout << "Waited." << endl; cout << "Waiting 1 second for semaphore with a count of 0..." << endl; @@ -93,8 +93,8 @@ void test_simple() rv = sem3.timedwait(0, 999999999); finished = time(NULL); diff = finished - started; - ss_dassert(!rv); - ss_dassert((diff >= 0) && (diff <= 2)); + mxb_assert(!rv); + mxb_assert((diff >= 0) && (diff <= 2)); cout << "Waited." << endl; } @@ -112,7 +112,7 @@ void* thread_main(void* pArg) void test_threads() { const int n_threads = 10; - pthread_t threads[n_threads]; + std::thread threads[n_threads]; Semaphore sem; @@ -120,8 +120,7 @@ void test_threads() for (int i = 0; i < n_threads; ++i) { - int rc = pthread_create(&threads[i], NULL, thread_main, &sem); - ss_dassert(rc == 0); + threads[i] = std::thread(thread_main, &sem); } cout << "Waiting for threads." << endl; @@ -132,13 +131,13 @@ void test_threads() for (int i = 0; i < n_threads; ++i) { - pthread_join(threads[i], NULL); + threads[i].join(); } cout << "Joined." << endl; } -void* send_signal(void*) +void send_signal() { cout << "Sleeping 2 seconds." << endl; sleep(2); @@ -146,8 +145,6 @@ void* send_signal(void*) cout << "Sending signal" << endl; kill(getpid(), SIGTERM); cout << "Sent signal" << endl; - - return NULL; } void sighandler(int s) @@ -160,11 +157,7 @@ void test_signal() signal(SIGTERM, sighandler); - pthread_t thread; - int rc; - - rc = pthread_create(&thread, NULL, send_signal, NULL); - ss_dassert(rc == 0); + std::thread thread(send_signal); bool waited; @@ -173,21 +166,20 @@ void test_signal() cout << "Waited" << endl; // Should return false and errno should be EINTR. - ss_dassert(!waited && (errno == EINTR)); + mxb_assert(!waited && (errno == EINTR)); - pthread_join(thread, NULL); + thread.join(); - rc = pthread_create(&thread, NULL, send_signal, NULL); - ss_dassert(rc == 0); + thread = std::thread(send_signal); cout << "Waiting" << endl; waited = sem.timedwait(4, Semaphore::IGNORE_SIGNALS); cout << "Waited" << endl; // Should return false and errno should be ETIMEDOUT. - ss_dassert(!waited && (errno == ETIMEDOUT)); + mxb_assert(!waited && (errno == ETIMEDOUT)); - pthread_join(thread, NULL); + thread.join(); } } diff --git a/server/core/CMakeLists.txt b/server/core/CMakeLists.txt index 8f175ba89..fd41c4f51 100644 --- a/server/core/CMakeLists.txt +++ b/server/core/CMakeLists.txt @@ -40,7 +40,6 @@ add_library(maxscale-common SHARED router.cc routingworker.cc secrets.cc - semaphore.cc server.cc service.cc session.cc diff --git a/server/core/dcb.cc b/server/core/dcb.cc index f1a6765ff..6fafbe798 100644 --- a/server/core/dcb.cc +++ b/server/core/dcb.cc @@ -48,12 +48,10 @@ #include #include #include -#include #include #include #include #include -#include #include #include "internal/modules.h" @@ -63,7 +61,6 @@ using maxscale::RoutingWorker; using maxscale::Worker; using maxscale::WorkerTask; -using maxscale::Semaphore; //#define DCB_LOG_EVENT_HANDLING #if defined(DCB_LOG_EVENT_HANDLING) diff --git a/server/core/internal/routingworker.hh b/server/core/internal/routingworker.hh index 488f3ae3d..d8368589c 100644 --- a/server/core/internal/routingworker.hh +++ b/server/core/internal/routingworker.hh @@ -185,7 +185,7 @@ public: * directly without going through the message loop of the worker, * otherwise the task is delivered via the message loop. */ - static size_t broadcast(Task* pTask, Semaphore* pSem = NULL); + static size_t broadcast(Task* pTask, mxb::Semaphore* pSem = NULL); /** * Posts a task to all workers for execution. diff --git a/server/core/resource.cc b/server/core/resource.cc index 86ec15145..3fa9cae4a 100644 --- a/server/core/resource.cc +++ b/server/core/resource.cc @@ -23,7 +23,6 @@ #include #include #include -#include #include #include diff --git a/server/core/routingworker.cc b/server/core/routingworker.cc index 01c629d42..7176c8412 100644 --- a/server/core/routingworker.cc +++ b/server/core/routingworker.cc @@ -27,7 +27,6 @@ #include #include #include -#include #include #include @@ -39,10 +38,10 @@ #define WORKER_ABSENT_ID -1 +using maxbase::Semaphore; using maxscale::RoutingWorker; using maxscale::WorkerLoad; using maxscale::Closer; -using maxscale::Semaphore; using std::vector; using std::stringstream; @@ -1202,7 +1201,7 @@ json_t* mxs_rworker_to_json(const char* zHost, int id) { Worker* target = RoutingWorker::get(id); WorkerInfoTask task(zHost, id + 1); - mxs::Semaphore sem; + Semaphore sem; target->execute(&task, &sem, mxs::Worker::EXECUTE_AUTO); sem.wait(); diff --git a/server/core/server.cc b/server/core/server.cc index c2c3f2405..0172aa7d8 100644 --- a/server/core/server.cc +++ b/server/core/server.cc @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include @@ -54,7 +53,6 @@ #include "internal/service.hh" -using maxscale::Semaphore; using maxscale::RoutingWorker; using maxscale::Worker; using maxscale::WorkerTask; diff --git a/server/core/test/CMakeLists.txt b/server/core/test/CMakeLists.txt index 10d3570c1..4c90afce6 100644 --- a/server/core/test/CMakeLists.txt +++ b/server/core/test/CMakeLists.txt @@ -18,7 +18,6 @@ add_executable(test_maxscalepcre2 test_maxscalepcre2.cc) add_executable(test_modulecmd test_modulecmd.cc) add_executable(test_modutil test_modutil.cc) add_executable(test_poll test_poll.cc) -add_executable(test_semaphore test_semaphore.cc) add_executable(test_server test_server.cc) add_executable(test_service test_service.cc) add_executable(test_spinlock test_spinlock.cc) @@ -49,7 +48,6 @@ target_link_libraries(test_maxscalepcre2 maxscale-common) target_link_libraries(test_modulecmd maxscale-common) target_link_libraries(test_modutil maxscale-common) target_link_libraries(test_poll maxscale-common) -target_link_libraries(test_semaphore maxscale-common) target_link_libraries(test_server maxscale-common) target_link_libraries(test_service maxscale-common) target_link_libraries(test_spinlock maxscale-common) @@ -79,7 +77,6 @@ add_test(test_maxscalepcre2 test_maxscalepcre2) add_test(test_modulecmd test_modulecmd) add_test(test_modutil test_modutil) add_test(test_poll test_poll) -add_test(test_semaphore test_semaphore) add_test(test_server test_server) add_test(test_service test_service) add_test(test_spinlock test_spinlock) diff --git a/server/core/worker.cc b/server/core/worker.cc index 9cfb1a42b..3b0cb10d4 100644 --- a/server/core/worker.cc +++ b/server/core/worker.cc @@ -24,7 +24,6 @@ #include #include -#include #define WORKER_ABSENT_ID -1 @@ -411,7 +410,7 @@ Worker* Worker::get_current() return this_thread.pCurrent_worker; } -bool Worker::execute(Task* pTask, Semaphore* pSem, enum execute_mode_t mode) +bool Worker::execute(Task* pTask, mxb::Semaphore* pSem, enum execute_mode_t mode) { // No logging here, function must be signal safe. bool rval = true; @@ -469,7 +468,7 @@ bool Worker::post_disposable(DisposableTask* pTask, enum execute_mode_t mode) return posted; } -bool Worker::execute(function func, Semaphore* pSem, execute_mode_t mode) +bool Worker::execute(function func, mxb::Semaphore* pSem, execute_mode_t mode) { class CustomTask : public maxscale::WorkerTask @@ -510,13 +509,13 @@ bool Worker::execute(function func, Semaphore* pSem, execute_mode_t mod bool Worker::call(Task& task, execute_mode_t mode) { - Semaphore sem; + mxb::Semaphore sem; return execute(&task, &sem, mode) && sem.wait(); } bool Worker::call(function func, execute_mode_t mode) { - Semaphore sem; + mxb::Semaphore sem; return execute(func, &sem, mode) && sem.wait(); } @@ -620,7 +619,7 @@ void Worker::handle_message(MessageQueue& queue, const MessageQueue::Message& ms case MXS_WORKER_MSG_TASK: { Task *pTask = reinterpret_cast(msg.arg1()); - Semaphore* pSem = reinterpret_cast(msg.arg2()); + mxb::Semaphore* pSem = reinterpret_cast(msg.arg2()); pTask->execute(*this); diff --git a/server/modules/monitor/mariadbmon/mariadbmon.cc b/server/modules/monitor/mariadbmon/mariadbmon.cc index 0634bae55..4d3c97d3e 100644 --- a/server/modules/monitor/mariadbmon/mariadbmon.cc +++ b/server/modules/monitor/mariadbmon/mariadbmon.cc @@ -25,7 +25,6 @@ #include #include #include -#include #include // TODO: For monitor_add_parameters #include "../../../core/internal/monitor.h"