2008-02-19 11:06:52 -07:00
|
|
|
/* Copyright (c) 2008, Avian Contributors
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-06-02 19:57:37 -06:00
|
|
|
#ifndef SYSTEM_H
|
|
|
|
#define SYSTEM_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2007-06-18 13:23:44 -06:00
|
|
|
namespace vm {
|
|
|
|
|
2008-01-13 15:05:08 -07:00
|
|
|
class System {
|
2007-06-02 19:57:37 -06:00
|
|
|
public:
|
2007-06-20 15:27:22 -06:00
|
|
|
typedef intptr_t Status;
|
2007-06-02 19:57:37 -06:00
|
|
|
|
2007-09-16 18:13:36 -06:00
|
|
|
enum FileType {
|
2008-11-21 16:20:35 -07:00
|
|
|
TypeUnknown,
|
|
|
|
TypeDoesNotExist,
|
|
|
|
TypeFile,
|
|
|
|
TypeDirectory
|
2007-09-16 18:13:36 -06:00
|
|
|
};
|
|
|
|
|
2007-06-02 19:57:37 -06:00
|
|
|
class Thread {
|
|
|
|
public:
|
2007-07-28 15:28:25 -06:00
|
|
|
virtual void interrupt() = 0;
|
2007-07-07 12:09:16 -06:00
|
|
|
virtual void join() = 0;
|
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
2008-04-09 13:08:13 -06:00
|
|
|
class ThreadVisitor {
|
|
|
|
public:
|
|
|
|
virtual void visit(void* ip, void* base, void* stack) = 0;
|
|
|
|
};
|
|
|
|
|
2007-07-07 12:09:16 -06:00
|
|
|
class Runnable {
|
|
|
|
public:
|
2007-07-28 15:28:25 -06:00
|
|
|
virtual void attach(Thread*) = 0;
|
|
|
|
virtual void run() = 0;
|
|
|
|
virtual bool interrupted() = 0;
|
|
|
|
virtual void setInterrupted(bool v) = 0;
|
2007-06-02 19:57:37 -06:00
|
|
|
};
|
|
|
|
|
2008-01-13 15:05:08 -07:00
|
|
|
class Mutex {
|
|
|
|
public:
|
|
|
|
virtual void acquire() = 0;
|
|
|
|
virtual void release() = 0;
|
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
2007-06-02 19:57:37 -06:00
|
|
|
class Monitor {
|
|
|
|
public:
|
2007-07-28 15:28:25 -06: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-02 19:57:37 -06:00
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
2007-09-10 17:33:58 -06:00
|
|
|
class Local {
|
|
|
|
public:
|
|
|
|
virtual void* get() = 0;
|
|
|
|
virtual void set(void* p) = 0;
|
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
2007-09-16 18:13:36 -06:00
|
|
|
class Region {
|
|
|
|
public:
|
|
|
|
virtual const uint8_t* start() = 0;
|
|
|
|
virtual size_t length() = 0;
|
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
2008-11-21 16:20:35 -07:00
|
|
|
class Directory {
|
|
|
|
public:
|
|
|
|
virtual const char* next() = 0;
|
|
|
|
virtual void dispose() = 0;
|
|
|
|
};
|
|
|
|
|
2007-06-23 19:39:49 -06:00
|
|
|
class Library {
|
|
|
|
public:
|
|
|
|
virtual void* resolve(const char* function) = 0;
|
2007-09-18 17:30:09 -06:00
|
|
|
virtual const char* name() = 0;
|
|
|
|
virtual bool mapName() = 0;
|
2007-06-23 19:39:49 -06:00
|
|
|
virtual Library* next() = 0;
|
2008-01-25 16:38:26 -07:00
|
|
|
virtual void setNext(Library* lib) = 0;
|
|
|
|
virtual void disposeAll() = 0;
|
2007-06-23 19:39:49 -06:00
|
|
|
};
|
|
|
|
|
2007-12-30 15:24:48 -07:00
|
|
|
class SignalHandler {
|
|
|
|
public:
|
2008-01-01 10:08:47 -07:00
|
|
|
virtual bool handleSignal(void** ip, void** base, void** stack,
|
|
|
|
void** thread) = 0;
|
2007-12-30 15:24:48 -07:00
|
|
|
};
|
|
|
|
|
2008-04-09 13:08:13 -06:00
|
|
|
class MonitorResource {
|
|
|
|
public:
|
|
|
|
MonitorResource(System::Thread* t, System::Monitor* m): t(t), m(m) {
|
|
|
|
m->acquire(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
~MonitorResource() {
|
|
|
|
m->release(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
System::Thread* t;
|
|
|
|
System::Monitor* m;
|
|
|
|
};
|
|
|
|
|
2007-06-02 19:57:37 -06:00
|
|
|
virtual bool success(Status) = 0;
|
2008-04-13 12:15:04 -06:00
|
|
|
virtual void* tryAllocate(unsigned sizeInBytes) = 0;
|
|
|
|
virtual void free(const void* p) = 0;
|
|
|
|
virtual void* tryAllocateExecutable(unsigned sizeInBytes) = 0;
|
|
|
|
virtual void freeExecutable(const void* p, unsigned sizeInBytes) = 0;
|
2007-07-28 15:28:25 -06:00
|
|
|
virtual Status attach(Runnable*) = 0;
|
2007-07-07 12:09:16 -06:00
|
|
|
virtual Status start(Runnable*) = 0;
|
2008-01-13 15:05:08 -07:00
|
|
|
virtual Status make(Mutex**) = 0;
|
2007-06-02 19:57:37 -06:00
|
|
|
virtual Status make(Monitor**) = 0;
|
2007-09-10 17:33:58 -06:00
|
|
|
virtual Status make(Local**) = 0;
|
2007-12-30 15:24:48 -07:00
|
|
|
virtual Status handleSegFault(SignalHandler* handler) = 0;
|
2008-04-09 13:08:13 -06:00
|
|
|
virtual Status visit(Thread* thread, Thread* target,
|
|
|
|
ThreadVisitor* visitor) = 0;
|
2007-06-28 20:58:48 -06:00
|
|
|
virtual uint64_t call(void* function, uintptr_t* arguments, uint8_t* types,
|
|
|
|
unsigned count, unsigned size,
|
|
|
|
unsigned returnType) = 0;
|
2007-09-16 18:13:36 -06:00
|
|
|
virtual Status map(Region**, const char* name) = 0;
|
|
|
|
virtual FileType identify(const char* name) = 0;
|
2008-11-21 16:20:35 -07:00
|
|
|
virtual Status open(Directory**, const char* name) = 0;
|
2008-01-25 16:25:30 -07:00
|
|
|
virtual Status load(Library**, const char* name, bool mapName) = 0;
|
2007-10-24 09:46:09 -06:00
|
|
|
virtual char pathSeparator() = 0;
|
2007-08-18 16:35:22 -06:00
|
|
|
virtual int64_t now() = 0;
|
2007-10-24 09:46:09 -06:00
|
|
|
virtual void exit(int code) = 0;
|
2007-06-17 22:25:42 -06:00
|
|
|
virtual void abort() = 0;
|
2007-07-20 08:36:31 -06:00
|
|
|
virtual void dispose() = 0;
|
2007-06-02 19:57:37 -06:00
|
|
|
};
|
|
|
|
|
2008-04-09 13:08:13 -06:00
|
|
|
#define ACQUIRE_MONITOR(t, m) \
|
|
|
|
System::MonitorResource MAKE_NAME(monitorResource_) (t, m)
|
|
|
|
|
2007-06-22 17:17:13 -06:00
|
|
|
inline void NO_RETURN
|
|
|
|
abort(System* s)
|
|
|
|
{
|
|
|
|
s->abort(); // this should not return
|
|
|
|
::abort();
|
|
|
|
}
|
|
|
|
|
2007-07-20 08:36:31 -06:00
|
|
|
inline void NO_RETURN
|
|
|
|
sysAbort(System* s)
|
|
|
|
{
|
|
|
|
abort(s);
|
|
|
|
}
|
|
|
|
|
2007-06-24 13:57:00 -06:00
|
|
|
inline void
|
|
|
|
expect(System* s, bool v)
|
|
|
|
{
|
|
|
|
if (UNLIKELY(not v)) abort(s);
|
|
|
|
}
|
|
|
|
|
2007-06-22 17:17:13 -06:00
|
|
|
#ifdef NDEBUG
|
2007-07-28 15:28:25 -06:00
|
|
|
|
2007-08-19 13:45:51 -06:00
|
|
|
# define assert(a, b)
|
2007-07-28 15:28:25 -06:00
|
|
|
|
|
|
|
#else // not NDEBUG
|
|
|
|
|
2007-06-22 17:17:13 -06:00
|
|
|
inline void
|
|
|
|
assert(System* s, bool v)
|
|
|
|
{
|
2007-06-24 13:57:00 -06:00
|
|
|
expect(s, v);
|
2007-06-22 17:17:13 -06:00
|
|
|
}
|
2007-07-28 15:28:25 -06:00
|
|
|
|
|
|
|
#endif // not NDEBUG
|
2007-06-22 17:17:13 -06:00
|
|
|
|
2007-07-20 08:36:31 -06:00
|
|
|
System*
|
2008-10-27 15:13:27 -06:00
|
|
|
makeSystem(const char* crashDumpDirectory);
|
2007-07-20 08:36:31 -06:00
|
|
|
|
2007-06-18 13:23:44 -06:00
|
|
|
} // namespace vm
|
|
|
|
|
2007-06-02 19:57:37 -06:00
|
|
|
#endif//SYSTEM_H
|