Fleshed out the handle manager. Switched threads to handles.

This commit is contained in:
Cody Brocious 2016-05-15 09:36:43 -06:00
parent ae9a1e3de3
commit 092a270e3f
6 changed files with 31 additions and 18 deletions

View file

@ -1,11 +1,22 @@
#include "Zookeeper.hpp"
uint32_t HandleManager::add_handle(shared_ptr<Handle> handle) {
void Handle::close() {
box->hm->remove(handle);
}
uint32_t HandleManager::add(shared_ptr<Handle> handle) {
bailout(handle_id == 0xFFFFFFFE); // XXX: We should have a way of recycling handles
handle->handle = ++handle_id;
handles[handle_id] = handle;
return handle_id;
}
shared_ptr<Handle> HandleManager::get_handle(uint32_t handle_id) {
shared_ptr<Handle> HandleManager::get(uint32_t handle_id) {
return handles[handle_id];
}
void HandleManager::remove(uint32_t handle_id) {
auto iter = handles.find(handle_id);
if(iter != handles.end())
handles.erase(iter);
}

View file

@ -3,22 +3,23 @@
class Handle {
public:
virtual void close() = 0;
virtual void close();
uint32_t handle;
};
class HandleManager {
public:
uint32_t add_handle(shared_ptr<Handle> handle);
shared_ptr<Handle> get_handle(uint32_t handle_id);
uint32_t add(shared_ptr<Handle> handle);
shared_ptr<Handle> get(uint32_t handle_id);
void remove(uint32_t handle_id);
template<typename T>
uint32_t add_handle(shared_ptr<T> handle) {
return add_handle(static_pointer_cast<Handle>(handle));
uint32_t add(shared_ptr<T> handle) {
return add(static_pointer_cast<Handle>(handle));
}
template<typename T>
shared_ptr<T> get_handle(uint32_t handle_id) {
return dynamic_pointer_cast<T>(get_handle(handle_id));
shared_ptr<T> get(uint32_t handle_id) {
return dynamic_pointer_cast<T>(get(handle_id));
}
map<uint32_t, shared_ptr<Handle>> handles;

View file

@ -50,7 +50,7 @@ uint32_t Hypercall::query_eeprom(uint32_t index) {
uint32_t Hypercall::io_open(uint32_t dir_handle, uint32_t fn) {
auto fnstr = read_string(fn);
if(dir_handle != 0) {
auto dirhnd = box->hm->get_handle<DirHandle>(dir_handle);
auto dirhnd = box->hm->get<DirHandle>(dir_handle);
assert(dirhnd->type == IOType::IO_DIRECTORY);
fnstr = dirhnd->path + "\\" + fnstr;
}

View file

@ -42,7 +42,7 @@ shared_ptr<IOHandle> IOManager::open(string pathstr) {
auto mapped = lookup_map(pathstr);
auto file = static_pointer_cast<IOHandle>(make_shared<FileHandle>(pathstr, mapped));
box->hm->add_handle(file);
box->hm->add(file);
return file;
}
@ -124,7 +124,7 @@ void IOManager::create_map(string from, string to) {
shared_ptr<IOHandle> Directory::open() {
auto hnd = static_pointer_cast<IOHandle>(make_shared<DirHandle>(path));
box->hm->add_handle(hnd);
box->hm->add(hnd);
return hnd;
}

View file

@ -9,12 +9,12 @@ ThreadManager::ThreadManager() {
uint32_t ThreadManager::create(uint32_t eip, uint32_t esp) {
auto thread = make_shared<Thread>();
box->hm->add(thread);
thread->eflags = 2;
thread->id = ++tid;
thread->eip = eip;
thread->esp = esp;
threads.push_back(thread);
return thread->id;
return thread->handle;
}
void ThreadManager::terminate(uint32_t thread) {
@ -31,11 +31,13 @@ void ThreadManager::terminate(uint32_t thread) {
}
for(auto iter = threads.begin(); iter != threads.end(); ++iter) {
if((*iter)->id == thread) {
if((*iter)->handle == thread) {
threads.erase(iter);
(*iter)->close();
return;
}
}
cout << "Could not find thread with id " << dec << thread << endl;
bailout(true);
}
@ -52,7 +54,7 @@ void ThreadManager::next() {
}
uint32_t ThreadManager::current_thread() {
return (*iterator)->id;
return (*iterator)->handle;
}
#define REGMAGIC() do {\

View file

@ -1,7 +1,7 @@
#pragma once
#include "Zookeeper.hpp"
class Thread {
class Thread : public Handle {
public:
void save();
void restore();
@ -24,5 +24,4 @@ public:
list<shared_ptr<Thread>> threads;
list<shared_ptr<Thread>>::iterator iterator;
uint32_t tid = 0;
};