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",
|
"inherits": "default",
|
||||||
"cacheVariables": {
|
"cacheVariables": {
|
||||||
"FISHBONE_BUILD_TESTS": "ON",
|
"FISHBONE_BUILD_TESTS": "ON",
|
||||||
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
|
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
|
||||||
"CMAKE_BUILD_TYPE": "Debug"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,7 @@ namespace fishbone {
|
||||||
Impl* impl;
|
Impl* impl;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Callable = void(*)(fishbone::Thread* thread, void* arg);
|
Thread(void (*call)(fishbone::Thread* thread, void* arg), void* arg);
|
||||||
using CallableFunc = std::function<void(fishbone::Thread* thread)>;
|
|
||||||
|
|
||||||
Thread(Callable call, void* arg);
|
|
||||||
Thread(CallableFunc call);
|
|
||||||
~Thread();
|
~Thread();
|
||||||
|
|
||||||
void join();
|
void join();
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,9 @@
|
||||||
#include <fishbone/foundation.h>
|
#include <fishbone/foundation.h>
|
||||||
|
|
||||||
struct call_arg {
|
struct call_arg {
|
||||||
fishbone::Thread::Callable call;
|
void (*call)(fishbone::Thread* thread, void* arg);
|
||||||
fishbone::Thread* thread;
|
fishbone::Thread* thread;
|
||||||
void* arg;
|
void* arg;
|
||||||
};
|
|
||||||
|
|
||||||
struct call_arg2 {
|
|
||||||
fishbone::Thread::CallableFunc call;
|
|
||||||
fishbone::Thread* thread;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static int SDLCALL thread_call(void* data) {
|
static int SDLCALL thread_call(void* data) {
|
||||||
|
|
@ -23,21 +18,11 @@ static int SDLCALL thread_call(void* data) {
|
||||||
return 0;
|
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 {
|
struct fishbone::Thread::Impl {
|
||||||
SDL_Thread* thread;
|
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();
|
struct call_arg* data = new struct call_arg();
|
||||||
|
|
||||||
data->call = call;
|
data->call = call;
|
||||||
|
|
@ -48,16 +33,6 @@ fishbone::Thread::Thread(Callable call, void* arg) {
|
||||||
this->impl->thread = SDL_CreateThread(thread_call, "FBThread", data);
|
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() {
|
fishbone::Thread::~Thread() {
|
||||||
this->join();
|
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