Skip to content

Commit

Permalink
Use per-thread error storage (thanks to @chkothe)
Browse files Browse the repository at this point in the history
  • Loading branch information
tstenner committed Dec 3, 2020
1 parent c50f794 commit 232b51b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <mmsystem.h>
#endif

char last_error[512];
thread_local char last_error[512] = {0};

extern "C" {

Expand Down
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C" {
"Please do not compile this with a lslboost version older than 1.45 because the library would otherwise not be protocol-compatible with builds using other versions."
#endif

extern char last_error[512];
extern thread_local char last_error[512];

// the highest supported protocol version
// * 100 is the original version, supported by library versions 1.00+
Expand Down
19 changes: 18 additions & 1 deletion testing/test_ext_streaminfo.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
#include "catch.hpp"
#include <lsl_cpp.h>
#include <thread>

namespace {

TEST_CASE("streaminfo", "[streaminfo][basic]") {
CHECK_THROWS(lsl::stream_info("", "emptyname"));
CHECK(std::string("The name of a stream must be non-empty.") == lsl_last_error());
REQUIRE(std::string("The name of a stream must be non-empty.") == lsl_last_error());
}

TEST_CASE("multithreaded lsl_last_error", "[threading][basic]") {
CHECK_THROWS(lsl::stream_info("", "emptyname"));
std::thread([](){
CHECK_THROWS(lsl::stream_info("hasname","type", -1));
REQUIRE(std::string("The channel_count of a stream must be nonnegative.") == lsl_last_error());
}).join();
REQUIRE(std::string("The name of a stream must be non-empty.") == lsl_last_error());
}

/// Ensure that an overly long error message won't overflow the buffer
TEST_CASE("lsl_last_error size", "[basic]") {
std::string invalidquery(512, '\'');
CHECK_THROWS(lsl::resolve_stream(invalidquery, 1, 0.1));
REQUIRE(lsl_last_error()[512] == 0);
}
} // namespace

0 comments on commit 232b51b

Please sign in to comment.