corda/src/system.h

116 lines
2.3 KiB
C
Raw Normal View History

#ifndef SYSTEM_H
#define SYSTEM_H
#include "common.h"
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 {
public:
2007-06-20 21:27:22 +00:00
typedef intptr_t Status;
class Thread {
public:
virtual ~Thread() { }
virtual void run() = 0;
2007-07-07 18:09:16 +00:00
virtual void join() = 0;
virtual void dispose() = 0;
};
class Runnable {
public:
virtual ~Runnable() { }
virtual void run(Thread*) = 0;
2007-07-11 01:38:06 +00:00
virtual void dispose() = 0;
};
class Monitor {
public:
virtual ~Monitor() { }
2007-07-02 01:42:35 +00:00
virtual bool tryAcquire(void* context) = 0;
virtual void acquire(void* context) = 0;
virtual void release(void* context) = 0;
virtual void wait(void* context, int64_t time) = 0;
virtual void notify(void* context) = 0;
virtual void notifyAll(void* context) = 0;
2007-07-07 23:47:35 +00:00
virtual void* owner() = 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;
virtual Library* next() = 0;
virtual void dispose() = 0;
2007-06-24 01:39:49 +00:00
};
virtual ~System() { }
virtual bool success(Status) = 0;
2007-07-07 18:09:16 +00:00
virtual Status attach(Thread**) = 0;
virtual Status start(Runnable*) = 0;
virtual Status make(Monitor**) = 0;
2007-07-11 01:38:06 +00:00
virtual void sleep(int64_t milliseconds) = 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-06-25 01:34:07 +00:00
virtual Status load(Library**, const char* name, Library* next) = 0;
2007-06-18 04:25:42 +00:00
virtual void abort() = 0;
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;
}
};
inline void NO_RETURN
abort(System* s)
{
s->abort(); // this should not return
::abort();
}
inline void NO_RETURN
sysAbort(System* s)
{
abort(s);
}
inline void
expect(System* s, bool v)
{
if (UNLIKELY(not v)) abort(s);
}
#ifdef NDEBUG
inline void
assert(System*, bool)
{ }
#else
inline void
assert(System* s, bool v)
{
expect(s, v);
}
#endif
System*
makeSystem(unsigned heapSize);
} // namespace vm
#endif//SYSTEM_H