get rid of pthread usage (use new octastd threading)

master
Daniel Kolesa 2016-01-23 22:38:59 +00:00
parent 4618694303
commit 20d423b594
3 changed files with 28 additions and 76 deletions

View File

@ -10,7 +10,7 @@ OB_CXXFLAGS += -std=c++14 -I. -I$(CUBESCRIPT_PATH) -I$(OCTASTD_PATH) -pthread
all: obuild all: obuild
obuild: $(FILES) obuild: $(FILES)
$(CXX) $(CXXFLAGS) $(OB_CXXFLAGS) $(LDFLAGS) -o obuild $(FILES) $(CXX) $(CXXFLAGS) $(OB_CXXFLAGS) $(LDFLAGS) -lstdthreads -o obuild $(FILES)
.cc.o: .cc.o:
$(CXX) $(CXXFLAGS) $(OB_CXXFLAGS) -c -o $@ $< $(CXX) $(CXXFLAGS) $(OB_CXXFLAGS) -c -o $@ $<

100
main.cc
View File

@ -1,5 +1,3 @@
#include <pthread.h>
#include <ostd/types.hh> #include <ostd/types.hh>
#include <ostd/functional.hh> #include <ostd/functional.hh>
#include <ostd/string.hh> #include <ostd/string.hh>
@ -9,6 +7,9 @@
#include <ostd/filesystem.hh> #include <ostd/filesystem.hh>
#include <ostd/platform.hh> #include <ostd/platform.hh>
#include <ostd/utility.hh> #include <ostd/utility.hh>
#include <ostd/thread.hh>
#include <ostd/mutex.hh>
#include <ostd/condition.hh>
#include <cubescript.hh> #include <cubescript.hh>
@ -20,6 +21,10 @@ using ostd::Map;
using ostd::String; using ostd::String;
using ostd::Uint32; using ostd::Uint32;
using ostd::slice_until; using ostd::slice_until;
using ostd::Thread;
using ostd::UniqueLock;
using ostd::Mutex;
using ostd::Condition;
using cscript::CsState; using cscript::CsState;
using cscript::TvalRange; using cscript::TvalRange;
@ -28,55 +33,6 @@ using cscript::Bytecode;
/* thread pool */ /* thread pool */
struct Mutex {
Mutex() {
pthread_mutex_init(&mtx, nullptr);
locked = false;
}
~Mutex() {
while (locked) unlock();
pthread_mutex_destroy(&mtx);
}
void lock() {
pthread_mutex_lock(&mtx);
locked = true;
}
void unlock() {
locked = false;
pthread_mutex_unlock(&mtx);
}
pthread_mutex_t mtx;
volatile bool locked;
};
struct Cond {
Cond() {
pthread_cond_init(&cnd, nullptr);
}
~Cond() {
pthread_cond_destroy(&cnd);
}
void signal() {
pthread_cond_signal(&cnd);
}
void broadcast() {
pthread_cond_broadcast(&cnd);
}
void wait(Mutex &m) {
pthread_cond_wait(&cnd, &m.mtx);
}
pthread_cond_t cnd;
};
struct Task { struct Task {
ostd::Function<void()> cb; ostd::Function<void()> cb;
Task *next = nullptr; Task *next = nullptr;
@ -98,10 +54,10 @@ struct ThreadPool {
bool init(ostd::Size size) { bool init(ostd::Size size) {
running = true; running = true;
for (ostd::Size i = 0; i < size; ++i) { for (ostd::Size i = 0; i < size; ++i) {
pthread_t tid; Thread tid([this]() { run(); });
if (pthread_create(&tid, nullptr, thr_func, this)) if (!tid)
return false; return false;
thrs.push(tid); thrs.push(ostd::move(tid));
} }
return true; return true;
} }
@ -111,27 +67,26 @@ struct ThreadPool {
running = false; running = false;
mtx.unlock(); mtx.unlock();
cond.broadcast(); cond.broadcast();
for (pthread_t &tid: thrs.iter()) { for (Thread &tid: thrs.iter()) {
void *ret; tid.join();
pthread_join(tid, &ret);
cond.broadcast(); cond.broadcast();
} }
} }
void run() { void run() {
for (;;) { for (;;) {
mtx.lock(); UniqueLock<Mutex> l(mtx);
while (running && (tasks == nullptr)) while (running && (tasks == nullptr))
cond.wait(mtx); cond.wait(l);
if (!running) { if (!running) {
mtx.unlock(); l.unlock();
pthread_exit(nullptr); ostd::this_thread::exit();
} }
Task *t = tasks; Task *t = tasks;
tasks = t->next; tasks = t->next;
if (last_task == t) if (last_task == t)
last_task = nullptr; last_task = nullptr;
mtx.unlock(); l.unlock();
t->cb(); t->cb();
delete t; delete t;
} }
@ -150,9 +105,9 @@ struct ThreadPool {
} }
private: private:
Cond cond; Condition cond;
Mutex mtx; Mutex mtx;
Vector<pthread_t> thrs; Vector<Thread> thrs;
Task *tasks; Task *tasks;
Task *last_task; Task *last_task;
volatile bool running; volatile bool running;
@ -259,28 +214,25 @@ struct ObState: CsState {
RuleCounter(): cond(), mtx(), counter(0), result(0) {} RuleCounter(): cond(), mtx(), counter(0), result(0) {}
void wait() { void wait() {
mtx.lock(); UniqueLock<Mutex> l(mtx);
while (counter) while (counter)
cond.wait(mtx); cond.wait(l);
mtx.unlock();
} }
void incr() { void incr() {
mtx.lock(); UniqueLock<Mutex> l(mtx);
++counter; ++counter;
mtx.unlock();
} }
void decr() { void decr() {
mtx.lock(); UniqueLock<Mutex> l(mtx);
if (!--counter) { if (!--counter) {
mtx.unlock(); l.unlock();
cond.broadcast(); cond.broadcast();
} else }
mtx.unlock();
} }
Cond cond; Condition cond;
Mutex mtx; Mutex mtx;
volatile int counter; volatile int counter;
ostd::AtomicInt result; ostd::AtomicInt result;

View File

@ -11,7 +11,7 @@ OB_CXXFLAGS = [@OB_CXXFLAGS -std=c++14 -I. -I@CS_PATH -I@OS_PATH -pthread]
rule obuild $FILES [ rule obuild $FILES [
echo " LD" $target echo " LD" $target
shell $CXX $OB_CXXFLAGS -o obuild_ob $sources shell $CXX $OB_CXXFLAGS -lstdthreads -o obuild_ob $sources
] ]
rule %_ob.o %.cc [ rule %_ob.o %.cc [