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 92 93 94 95 96 97
|
#pragma once
#include "Object.h"
#include "EnginePtr.h"
namespace storm {
STORM_PKG(core);
/**
* A thread known by Storm.
*
* NOTE: due to the startup process, this thread may *not* contain any pointers to other Storm
* objects, as they are not reported to the GC during startup.
*
* NOTE: These may be copied, so never assume that if a != b, then they represent different
* threads. However, the reverse holds. Ie. if a == b then they represent the same thread.
*/
class Thread : public Object {
STORM_CLASS;
public:
// Create a thread.
STORM_CTOR Thread();
// Create from a previously started os::Thread object. This thread must have called the
// 'register' function previously.
Thread(const os::Thread &thread);
// Lazily create the underlying thread when needed.
Thread(DeclThread::CreateFn fn);
// Make sure that the previous thread has been created during a copy.
Thread(const Thread &o);
// Destroy.
~Thread();
// Deep copy.
virtual void STORM_FN deepCopy(CloneEnv *env);
// Check for equality.
Bool STORM_FN operator ==(const Thread &o) const;
// Compute a hash.
Nat STORM_FN hash() const;
// Get the thread handle.
const os::Thread &thread();
// Check if this is the same thread as the one passed as a parameter. This will not cause
// any threads to be created.
bool sameAs(const os::Thread &other) const;
bool sameAs(size_t id) const;
// Check if the caller of the function is the thread represented by this object.
Bool STORM_FN isCurrent() const;
// Get the current thread as a Thread object.
static Thread *STORM_FN current(EnginePtr e);
// To string.
virtual void STORM_FN toS(StrBuf *to) const;
#ifdef STORM_COMPILER
/**
* Allow stand-alone allocation of the first Thread.
*/
struct First {
Engine &e;
First(Engine &e) : e(e) {}
};
static void *operator new(size_t size, First t);
static void operator delete(void *mem, First t);
#endif
private:
// Thread handle.
UNKNOWN(PTR_NOGC) os::Thread osThread;
// Create using.
UNKNOWN(PTR_NOGC) DeclThread::CreateFn create;
};
/**
* The main thread of the compiler.
*
* In order to avoid many potential threading issues in the compiler, the compiler itself is
* single threaded and executed by this thread. This is important to consider when writing
* languages and other code that deal with compilation. Even though the languages in Storm will
* ensure that your progam is properly synchronized with regards to data races, it is beneficial
* to place logic that relies a lot on functionality provided by the compiler itself (such as
* the name tree) on the Compiler thread as well in order to avoid an excessive amount of
* expensive thread switches.
*/
STORM_THREAD(Compiler);
}
|