2007-06-03 01:57:37 +00:00
|
|
|
#ifndef SYSTEM_H
|
|
|
|
#define SYSTEM_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2007-06-18 19:23:44 +00:00
|
|
|
namespace vm {
|
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
class Allocator {
|
|
|
|
public:
|
|
|
|
virtual ~Allocator() { }
|
|
|
|
virtual void* tryAllocate(unsigned size) = 0;
|
|
|
|
virtual void* allocate(unsigned size) = 0;
|
|
|
|
virtual void free(const void*) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class System: public Allocator {
|
2007-06-03 01:57:37 +00:00
|
|
|
public:
|
2007-06-20 21:27:22 +00:00
|
|
|
typedef intptr_t Status;
|
2007-06-03 01:57:37 +00:00
|
|
|
|
|
|
|
class Thread {
|
|
|
|
public:
|
|
|
|
virtual ~Thread() { }
|
2007-07-28 21:28:25 +00:00
|
|
|
virtual void interrupt() = 0;
|
2007-07-07 18:09:16 +00:00
|
|
|
virtual void join() = 0;
|
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Runnable {
|
|
|
|
public:
|
|
|
|
virtual ~Runnable() { }
|
2007-07-28 21:28:25 +00:00
|
|
|
virtual void attach(Thread*) = 0;
|
|
|
|
virtual void run() = 0;
|
|
|
|
virtual bool interrupted() = 0;
|
|
|
|
virtual void setInterrupted(bool v) = 0;
|
2007-06-03 01:57:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Monitor {
|
|
|
|
public:
|
|
|
|
virtual ~Monitor() { }
|
2007-07-28 21:28:25 +00:00
|
|
|
virtual bool tryAcquire(Thread* context) = 0;
|
|
|
|
virtual void acquire(Thread* context) = 0;
|
|
|
|
virtual void release(Thread* context) = 0;
|
|
|
|
virtual bool wait(Thread* context, int64_t time) = 0;
|
|
|
|
virtual void notify(Thread* context) = 0;
|
|
|
|
virtual void notifyAll(Thread* context) = 0;
|
|
|
|
virtual Thread* owner() = 0;
|
2007-06-03 01:57:37 +00:00
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
2007-09-10 23:33:58 +00:00
|
|
|
class Local {
|
|
|
|
public:
|
|
|
|
virtual ~Local() { }
|
|
|
|
virtual void* get() = 0;
|
|
|
|
virtual void set(void* p) = 0;
|
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
2007-06-24 01:39:49 +00:00
|
|
|
class Library {
|
|
|
|
public:
|
|
|
|
virtual ~Library() { }
|
|
|
|
virtual void* resolve(const char* function) = 0;
|
2007-09-12 01:13:05 +00:00
|
|
|
virtual bool matches(const char* name, bool mapName) = 0;
|
2007-06-24 01:39:49 +00:00
|
|
|
virtual Library* next() = 0;
|
2007-06-24 19:57:00 +00:00
|
|
|
virtual void dispose() = 0;
|
2007-06-24 01:39:49 +00:00
|
|
|
};
|
|
|
|
|
2007-06-03 01:57:37 +00:00
|
|
|
virtual ~System() { }
|
|
|
|
|
|
|
|
virtual bool success(Status) = 0;
|
2007-07-28 21:28:25 +00:00
|
|
|
virtual Status attach(Runnable*) = 0;
|
2007-07-07 18:09:16 +00:00
|
|
|
virtual Status start(Runnable*) = 0;
|
2007-06-03 01:57:37 +00:00
|
|
|
virtual Status make(Monitor**) = 0;
|
2007-09-10 23:33:58 +00:00
|
|
|
virtual Status make(Local**) = 0;
|
2007-06-29 02:58:48 +00:00
|
|
|
virtual uint64_t call(void* function, uintptr_t* arguments, uint8_t* types,
|
|
|
|
unsigned count, unsigned size,
|
|
|
|
unsigned returnType) = 0;
|
2007-09-12 01:13:05 +00:00
|
|
|
virtual Status load(Library**, const char* name, bool mapName, Library* next)
|
|
|
|
= 0;
|
2007-07-21 17:50:26 +00:00
|
|
|
virtual void exit(int code) = 0;
|
2007-08-18 22:35:22 +00:00
|
|
|
virtual int64_t now() = 0;
|
2007-06-18 04:25:42 +00:00
|
|
|
virtual void abort() = 0;
|
2007-07-20 14:36:31 +00:00
|
|
|
virtual void dispose() = 0;
|
2007-06-20 19:20:25 +00:00
|
|
|
|
2007-07-07 18:09:16 +00:00
|
|
|
virtual void* allocate(unsigned size) {
|
2007-06-22 02:13:17 +00:00
|
|
|
void* p = tryAllocate(size);
|
|
|
|
if (p == 0) {
|
2007-06-20 19:20:25 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2007-06-03 01:57:37 +00:00
|
|
|
};
|
|
|
|
|
2007-06-22 23:17:13 +00:00
|
|
|
inline void NO_RETURN
|
|
|
|
abort(System* s)
|
|
|
|
{
|
|
|
|
s->abort(); // this should not return
|
|
|
|
::abort();
|
|
|
|
}
|
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
inline void NO_RETURN
|
|
|
|
sysAbort(System* s)
|
|
|
|
{
|
|
|
|
abort(s);
|
|
|
|
}
|
|
|
|
|
2007-06-24 19:57:00 +00:00
|
|
|
inline void
|
|
|
|
expect(System* s, bool v)
|
|
|
|
{
|
|
|
|
if (UNLIKELY(not v)) abort(s);
|
|
|
|
}
|
|
|
|
|
2007-06-22 23:17:13 +00:00
|
|
|
#ifdef NDEBUG
|
2007-07-28 21:28:25 +00:00
|
|
|
|
2007-08-19 19:45:51 +00:00
|
|
|
# define assert(a, b)
|
2007-07-28 21:28:25 +00:00
|
|
|
|
|
|
|
#else // not NDEBUG
|
|
|
|
|
2007-06-22 23:17:13 +00:00
|
|
|
inline void
|
|
|
|
assert(System* s, bool v)
|
|
|
|
{
|
2007-06-24 19:57:00 +00:00
|
|
|
expect(s, v);
|
2007-06-22 23:17:13 +00:00
|
|
|
}
|
2007-07-28 21:28:25 +00:00
|
|
|
|
|
|
|
#endif // not NDEBUG
|
2007-06-22 23:17:13 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
System*
|
|
|
|
makeSystem(unsigned heapSize);
|
|
|
|
|
2007-06-18 19:23:44 +00:00
|
|
|
} // namespace vm
|
|
|
|
|
2007-06-03 01:57:37 +00:00
|
|
|
#endif//SYSTEM_H
|