Commit 7c8d7a83 by FritzFlorian

WIP: Add minimal example to trigger tsan bugs.

parent f347849f
Pipeline #1401 failed with stages
in 37 seconds
add_executable(playground main.cpp) add_executable(playground
barrier.h barrier.cpp
main.cpp)
# Example for adding the library to your app (as a cmake project dependency) # Example for adding the library to your app (as a cmake project dependency)
target_link_libraries(playground context_switcher) target_link_libraries(playground context_switcher Threads::Threads)
#include "barrier.h"
barrier::barrier(const unsigned int count) : barrier_{} {
pthread_barrier_init(&barrier_, nullptr, count);
}
barrier::~barrier() {
pthread_barrier_destroy(&barrier_);
}
void barrier::wait() {
pthread_barrier_wait(&barrier_);
}
#ifndef PLS_BARRIER_H
#define PLS_BARRIER_H
#include <pthread.h>
/**
* Provides standard barrier behaviour.
* `count` threads have to call `wait()` before any of the `wait()` calls returns,
* thus blocking all threads until everyone reached the barrier.
*
* PORTABILITY:
* Current implementation is based on pthreads.
*/
class barrier {
pthread_barrier_t barrier_;
public:
explicit barrier(unsigned int count);
~barrier();
void wait();
};
#endif //PLS_BARRIER_H
#include <utility> #include <utility>
#include <cstdio> #include <cstdio>
#include <thread>
#include "barrier.h"
#include "context_switcher/context_switcher.h" #include "context_switcher/context_switcher.h"
using namespace context_switcher; using namespace context_switcher;
using namespace std; using namespace std;
...@@ -10,17 +14,40 @@ const size_t STACK_SIZE = 512 * 32; ...@@ -10,17 +14,40 @@ const size_t STACK_SIZE = 512 * 32;
const size_t NUM_STACKS = 4; const size_t NUM_STACKS = 4;
char custom_stacks[NUM_STACKS][STACK_SIZE]; char custom_stacks[NUM_STACKS][STACK_SIZE];
volatile int result;
int main() { int main() {
context_switcher::continuation cont_t1, cont_main;
barrier bar{2};
int error = 0;
auto t1 = std::thread([&]() {
while (true) {
bar.wait();
auto cont = enter_context(custom_stacks[0], STACK_SIZE, [&](continuation &&cont) {
error++;
cont_t1 = std::move(cont);
bar.wait();
error++;
return std::move(cont_main);
});
}
});
int count = 0;
while (true) { while (true) {
context_switcher::continuation cont = enter_context(custom_stacks[0], STACK_SIZE, [](continuation &&main_cont) { count++;
// main_cont = context_switcher::switch_context(std::move(main_cont)); if (count % 100 == 0) {
return std::move(main_cont); printf("%d\n", count);
}
bar.wait();
auto cont = enter_context(custom_stacks[1], STACK_SIZE, [&](continuation &&cont) {
error++;
cont_main = std::move(cont);
bar.wait();
error++;
return std::move(cont_t1);
}); });
}
// cont = context_switcher::switch_context(std::move(cont));
};
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment