Compare commits
No commits in common. "ee8ff117128cc0b553ea05c44ddd9989ee1baab7" and "61db6ed694d5ef8ea41acd61b797bbad634b653f" have entirely different histories.
ee8ff11712
...
61db6ed694
6 changed files with 7 additions and 96 deletions
|
|
@ -17,8 +17,7 @@
|
|||
"inherits": "default",
|
||||
"cacheVariables": {
|
||||
"FISHBONE_BUILD_TESTS": "ON",
|
||||
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
|
||||
"CMAKE_BUILD_TYPE": "Debug"
|
||||
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
|
||||
}
|
||||
}
|
||||
],
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
|
|
|||
|
|
@ -14,11 +14,7 @@ namespace fishbone {
|
|||
Impl* impl;
|
||||
|
||||
public:
|
||||
using Callable = void(*)(fishbone::Thread* thread, void* arg);
|
||||
using CallableFunc = std::function<void(fishbone::Thread* thread)>;
|
||||
|
||||
Thread(Callable call, void* arg);
|
||||
Thread(CallableFunc call);
|
||||
Thread(void (*call)(fishbone::Thread* thread, void* arg), void* arg);
|
||||
~Thread();
|
||||
|
||||
void join();
|
||||
|
|
|
|||
|
|
@ -3,14 +3,9 @@
|
|||
#include <fishbone/foundation.h>
|
||||
|
||||
struct call_arg {
|
||||
fishbone::Thread::Callable call;
|
||||
fishbone::Thread* thread;
|
||||
void* arg;
|
||||
};
|
||||
|
||||
struct call_arg2 {
|
||||
fishbone::Thread::CallableFunc call;
|
||||
fishbone::Thread* thread;
|
||||
void (*call)(fishbone::Thread* thread, void* arg);
|
||||
fishbone::Thread* thread;
|
||||
void* arg;
|
||||
};
|
||||
|
||||
static int SDLCALL thread_call(void* data) {
|
||||
|
|
@ -23,21 +18,11 @@ static int SDLCALL thread_call(void* data) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int SDLCALL thread_call2(void* data) {
|
||||
struct call_arg2* arg = (struct call_arg2*)data;
|
||||
|
||||
arg->call(arg->thread);
|
||||
|
||||
delete arg;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct fishbone::Thread::Impl {
|
||||
SDL_Thread* thread;
|
||||
};
|
||||
|
||||
fishbone::Thread::Thread(Callable call, void* arg) {
|
||||
fishbone::Thread::Thread(void (*call)(fishbone::Thread* thread, void* arg), void* arg) {
|
||||
struct call_arg* data = new struct call_arg();
|
||||
|
||||
data->call = call;
|
||||
|
|
@ -48,16 +33,6 @@ fishbone::Thread::Thread(Callable call, void* arg) {
|
|||
this->impl->thread = SDL_CreateThread(thread_call, "FBThread", data);
|
||||
}
|
||||
|
||||
fishbone::Thread::Thread(CallableFunc call) {
|
||||
struct call_arg2* data = new struct call_arg2();
|
||||
|
||||
data->call = call;
|
||||
data->thread = this;
|
||||
|
||||
this->impl = new fishbone::Thread::Impl();
|
||||
this->impl->thread = SDL_CreateThread(thread_call2, "FBThread", data);
|
||||
}
|
||||
|
||||
fishbone::Thread::~Thread() {
|
||||
this->join();
|
||||
}
|
||||
|
|
|
|||
1
engine/tests/src/placeholder.cc
Normal file
1
engine/tests/src/placeholder.cc
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
#include "fishbone/thread.h"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
|
||||
struct thread_arg {
|
||||
int& a;
|
||||
};
|
||||
|
||||
TEST_CASE("Thread") {
|
||||
int a = 0;
|
||||
|
||||
fishbone::Thread th([&a](auto) {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
a++;
|
||||
}
|
||||
});
|
||||
|
||||
th.join();
|
||||
REQUIRE(a == 100);
|
||||
|
||||
a = 0;
|
||||
|
||||
thread_arg arg { a };
|
||||
fishbone::Thread th2([](auto, void* data) {
|
||||
thread_arg& arg = *static_cast<thread_arg*>(data);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
arg.a++;
|
||||
}
|
||||
}, &arg);
|
||||
|
||||
th.join();
|
||||
REQUIRE(a == 100);
|
||||
}
|
||||
|
||||
TEST_CASE("Mutex") {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
int a = 0;
|
||||
fishbone::Mutex m;
|
||||
|
||||
fishbone::Thread::CallableFunc func = [&a, &m](auto) {
|
||||
m.lock();
|
||||
for (int j = 0; j < 2; j++) {
|
||||
int b = a;
|
||||
usleep((60 * rand()) / RAND_MAX);
|
||||
a = ++b;
|
||||
}
|
||||
m.unlock();
|
||||
};
|
||||
|
||||
fishbone::Thread th1(func), th2(func);
|
||||
|
||||
th1.join();
|
||||
th2.join();
|
||||
REQUIRE(a == 4);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue