1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include "stdafx.h"
#include "Thread.h"
#include "Hash.h"
#include "StrBuf.h"
namespace storm {
Thread::Thread() : osThread(os::Thread::invalid), create(null) {}
Thread::Thread(const os::Thread &thread) : osThread(thread), create(null) {}
Thread::Thread(DeclThread::CreateFn fn) : osThread(os::Thread::invalid), create(fn) {}
Thread::Thread(const Thread &o) : osThread(const_cast<Thread &>(o).thread()), create(null) {}
Thread::~Thread() {
// We need to call the destructor of 'thread', so we need to declare a destructor to tell
// Storm that the destructor needs to be called.
}
void Thread::deepCopy(CloneEnv *env) {
// 'osThread' is threadsafe anyway.
}
Bool Thread::operator ==(const Thread &o) const {
if (this == &o)
return true;
// If any of this or other is not yet initialized, then we know that we must differ as long
// as we are different objects, since copying a thread starts it.
if (osThread == os::Thread::invalid)
return false;
if (o.osThread == os::Thread::invalid)
return false;
return osThread == o.osThread;
}
Nat Thread::hash() const {
// Note: If we are currently 'null', then we need to create the thread now. Otherwise our
// hash will not be consistent.
Thread *t = const_cast<Thread *>(this);
return ptrHash((const void *)t->thread().id());
}
const os::Thread &Thread::thread() {
// Do not acquire the lock first, as creation will only happen once.
if (osThread == os::Thread::invalid) {
// Acquire lock and check again.
util::Lock::L z(runtime::threadLock(engine()));
if (osThread == os::Thread::invalid) {
if (create) {
// TODO: make sure the newly created thread has been properly registered with the gc?
osThread = (*create)(engine());
} else {
osThread = os::Thread::spawn(util::Fn<void>(), runtime::threadGroup(engine()));
}
}
}
return osThread;
}
bool Thread::sameAs(const os::Thread &o) const {
return o == osThread;
}
bool Thread::sameAs(size_t id) const {
if (osThread == os::Thread::invalid)
return false;
return osThread.id() == id;
}
Bool Thread::isCurrent() const {
return sameAs(os::Thread::current());
}
Thread *Thread::current(EnginePtr e) {
return new (e.v) Thread(os::Thread::current());
}
void Thread::toS(StrBuf *to) const {
*to << S("Thread: ");
if (osThread == os::Thread::invalid)
*to << S("<not started>");
else
*to << hex(osThread.id());
}
STORM_DEFINE_THREAD(Compiler);
}
|