In progress XNotify stuff. Going to merge xam/xboxkrnl next.

This commit is contained in:
Ben Vanik
2014-01-04 16:18:16 -08:00
parent f23c330353
commit aad4d7bebf
9 changed files with 321 additions and 5 deletions

View File

@@ -7,6 +7,8 @@
'xfile.h',
'xmodule.cc',
'xmodule.h',
'xnotify_listener.cc',
'xnotify_listener.h',
'xsemaphore.cc',
'xsemaphore.h',
'xthread.cc',

View File

@@ -0,0 +1,126 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <xenia/kernel/xboxkrnl/objects/xnotify_listener.h>
using namespace xe;
using namespace xe::kernel;
using namespace xe::kernel::xboxkrnl;
XNotifyListener::XNotifyListener(KernelState* kernel_state) :
XObject(kernel_state, kTypeNotifyListener),
wait_handle_(NULL), lock_(0), mask_(0), notification_count_(0) {
}
XNotifyListener::~XNotifyListener() {
xe_mutex_free(lock_);
if (wait_handle_) {
CloseHandle(wait_handle_);
}
}
void XNotifyListener::Initialize(uint64_t mask) {
XEASSERTNULL(wait_handle_);
lock_ = xe_mutex_alloc();
wait_handle_ = CreateEvent(NULL, TRUE, FALSE, NULL);
mask_ = mask;
}
void XNotifyListener::EnqueueNotification(XNotificationID id, uint32_t data) {
xe_mutex_lock(lock_);
auto existing = notifications_.find(id);
if (existing != notifications_.end()) {
// Already exists. Overwrite.
notifications_[id] = data;
} else {
// New.
notification_count_++;
}
SetEvent(wait_handle_);
xe_mutex_unlock(lock_);
}
bool XNotifyListener::DequeueNotification(
XNotificationID* out_id, uint32_t* out_data) {
bool dequeued = false;
xe_mutex_lock(lock_);
if (notification_count_) {
dequeued = true;
auto it = notifications_.begin();
*out_id = it->first;
*out_data = it->second;
notifications_.erase(it);
notification_count_--;
if (!notification_count_) {
ResetEvent(wait_handle_);
}
}
xe_mutex_unlock(lock_);
return dequeued;
}
bool XNotifyListener::DequeueNotification(
XNotificationID id, uint32_t* out_data) {
bool dequeued = false;
xe_mutex_lock(lock_);
if (notification_count_) {
dequeued = true;
auto it = notifications_.find(id);
if (it != notifications_.end()) {
*out_data = it->second;
notifications_.erase(it);
notification_count_--;
if (!notification_count_) {
ResetEvent(wait_handle_);
}
}
}
xe_mutex_unlock(lock_);
return dequeued;
}
// TODO(benvanik): move this to common class
X_STATUS XNotifyListener::Wait(uint32_t wait_reason, uint32_t processor_mode,
uint32_t alertable, uint64_t* opt_timeout) {
DWORD timeout_ms;
if (opt_timeout) {
int64_t timeout_ticks = (int64_t)(*opt_timeout);
if (timeout_ticks > 0) {
// Absolute time, based on January 1, 1601.
// TODO(benvanik): convert time to relative time.
XEASSERTALWAYS();
timeout_ms = 0;
} else if (timeout_ticks < 0) {
// Relative time.
timeout_ms = (DWORD)(-timeout_ticks / 10000); // Ticks -> MS
} else {
timeout_ms = 0;
}
} else {
timeout_ms = INFINITE;
}
DWORD result = WaitForSingleObjectEx(wait_handle_, timeout_ms, alertable);
switch (result) {
case WAIT_OBJECT_0:
return X_STATUS_SUCCESS;
case WAIT_IO_COMPLETION:
// Or X_STATUS_ALERTED?
return X_STATUS_USER_APC;
case WAIT_TIMEOUT:
return X_STATUS_TIMEOUT;
default:
case WAIT_FAILED:
case WAIT_ABANDONED:
return X_STATUS_ABANDONED_WAIT_0;
}
}

View File

@@ -0,0 +1,57 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
// This should probably be in XAM, but I don't want to build an extensible
// object system. Meh.
#ifndef XENIA_KERNEL_XBOXKRNL_XNOTIFY_LISTENER_H_
#define XENIA_KERNEL_XBOXKRNL_XNOTIFY_LISTENER_H_
#include <xenia/kernel/xboxkrnl/xobject.h>
#include <xenia/xbox.h>
namespace xe {
namespace kernel {
namespace xboxkrnl {
// Values seem to be all over the place - GUIDs?
typedef uint32_t XNotificationID;
class XNotifyListener : public XObject {
public:
XNotifyListener(KernelState* kernel_state);
virtual ~XNotifyListener();
void Initialize(uint64_t mask);
void EnqueueNotification(XNotificationID id, uint32_t data);
bool DequeueNotification(XNotificationID* out_id, uint32_t* out_data);
bool DequeueNotification(XNotificationID id, uint32_t* out_data);
virtual X_STATUS Wait(uint32_t wait_reason, uint32_t processor_mode,
uint32_t alertable, uint64_t* opt_timeout);
private:
HANDLE wait_handle_;
xe_mutex_t* lock_;
std::unordered_map<XNotificationID, uint32_t> notifications_;
size_t notification_count_;
uint64_t mask_;
};
} // namespace xboxkrnl
} // namespace kernel
} // namespace xe
#endif // XENIA_KERNEL_XBOXKRNL_XNOTIFY_LISTENER_H_

View File

@@ -32,11 +32,12 @@ typedef struct {
class XObject {
public:
enum Type {
kTypeModule = 0x00000001,
kTypeThread = 0x00000002,
kTypeEvent = 0x00000003,
kTypeFile = 0x00000004,
kTypeSemaphore = 0x00000005,
kTypeModule,
kTypeThread,
kTypeEvent,
kTypeFile,
kTypeSemaphore,
kTypeNotifyListener,
};
XObject(KernelState* kernel_state, Type type);