summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--raul/Thread.hpp6
-rw-r--r--src/Thread.cpp5
-rw-r--r--test/thread_test.cpp17
3 files changed, 19 insertions, 9 deletions
diff --git a/raul/Thread.hpp b/raul/Thread.hpp
index 0d6c094..0eb3a9e 100644
--- a/raul/Thread.hpp
+++ b/raul/Thread.hpp
@@ -55,8 +55,10 @@ public:
/** Wait until the thread exits. */
virtual void join();
- /** Set the scheduling policy for this thread. */
- virtual void set_scheduling(bool realtime, unsigned priority);
+ /** Set the scheduling policy for this thread.
+ * @return True on success.
+ */
+ virtual bool set_scheduling(bool realtime, unsigned priority);
/** Return the name of this thread. */
const std::string& name() const { return _name; }
diff --git a/src/Thread.cpp b/src/Thread.cpp
index 34d7b65..5287beb 100644
--- a/src/Thread.cpp
+++ b/src/Thread.cpp
@@ -116,7 +116,7 @@ Thread::join()
pthread_join(_impl->pthread, NULL);
}
-void
+bool
Thread::set_scheduling(bool realtime, unsigned priority)
{
sched_param sp;
@@ -128,9 +128,10 @@ Thread::set_scheduling(bool realtime, unsigned priority)
% (realtime ? "realtime" : "normal")
% sp.sched_priority);
} else {
- LOG(info) << (fmt("Unable to set scheduling policy (%1%)\n")
+ LOG(warn) << (fmt("Unable to set scheduling policy (%1%)\n")
% strerror(result));
}
+ return !result;
}
} // namespace Raul
diff --git a/test/thread_test.cpp b/test/thread_test.cpp
index 95bdb76..00539eb 100644
--- a/test/thread_test.cpp
+++ b/test/thread_test.cpp
@@ -16,6 +16,7 @@
#include <iostream>
+#include "raul/log.hpp"
#include "raul/AtomicInt.hpp"
#include "raul/Semaphore.hpp"
#include "raul/Thread.hpp"
@@ -29,18 +30,24 @@ Raul::AtomicInt n_errors(0);
class Waiter : public Raul::Thread {
public:
- Waiter(Semaphore& sem) : Raul::Thread("Waiter"), _sem(sem) {}
+ Waiter(Semaphore& sem) : Raul::Thread("Waiter"), _sem(sem) {
+ if (set_scheduling(true, 10)) {
+ Raul::warn << "Set priority on non-existent thread" << endl;
+ }
+ }
private:
void _run() {
- set_scheduling(true, 10);
+ if (!set_scheduling(true, 10)) {
+ Raul::error << "Failed to set priority" << endl;
+ }
var = 41;
cout << "[Waiter] Waiting for signal..." << endl;
_sem.wait();
cout << "[Waiter] Received signal, exiting" << endl;
var = 42;
if (var != 42) {
- cerr << "[Waiter] error: var != 42" << endl;
+ Raul::error << "[Waiter] var != 42" << endl;
++n_errors;
}
}
@@ -53,7 +60,7 @@ main()
{
Thread& main_thread = Thread::get("Main");
if (main_thread.name() != "Main") {
- cerr << "error: Main thread name is not 'Main'" << endl;
+ Raul::error << "[Main] Thread name is not 'Main'" << endl;
return 1;
}
@@ -72,7 +79,7 @@ main()
cout << "[Main] Exiting" << endl;
if (var != 24) {
- cerr << "[Main] error: var != 24" << endl;
+ Raul::error << "[Main] var != 24" << endl;
++n_errors;
}