From b78aff85857a3a356fdf11e1dbc4e5f52490676e Mon Sep 17 00:00:00 2001
From: bunnei <ericbunnie@gmail.com>
Date: Mon, 2 Jun 2014 20:38:34 -0400
Subject: [PATCH] svc: added optional name field to Event and Mutex (used for
 debugging)

---
 src/core/hle/kernel/event.cpp | 11 ++++++++---
 src/core/hle/kernel/event.h   |  3 ++-
 src/core/hle/kernel/mutex.cpp | 12 +++++++++---
 src/core/hle/kernel/mutex.h   |  3 ++-
 src/core/hle/service/apt.cpp  |  6 +++---
 src/core/hle/service/srv.cpp  |  2 +-
 6 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/core/hle/kernel/event.cpp b/src/core/hle/kernel/event.cpp
index e84d0b49e2..70e50115d2 100644
--- a/src/core/hle/kernel/event.cpp
+++ b/src/core/hle/kernel/event.cpp
@@ -15,6 +15,7 @@ namespace Kernel {
 class Event : public Object {
 public:
     const char* GetTypeName() { return "Event"; }
+    const char* GetName() { return name.c_str(); }
 
     static Kernel::HandleType GetStaticHandleType() {  return Kernel::HandleType::Event; }
     Kernel::HandleType GetHandleType() const { return Kernel::HandleType::Event; }
@@ -24,6 +25,7 @@ public:
 
     bool locked;                    ///< Current locked state
     bool permanent_locked;          ///< Hack - to set event permanent state (for easy passthrough)
+    std::string name;               ///< Name of event (optional)
 
     /**
      * Synchronize kernel object 
@@ -98,9 +100,10 @@ Result ClearEvent(Handle handle) {
  * Creates an event
  * @param handle Reference to handle for the newly created mutex
  * @param reset_type ResetType describing how to create event
+ * @param name Optional name of event
  * @return Newly created Event object
  */
-Event* CreateEvent(Handle& handle, const ResetType reset_type) {
+Event* CreateEvent(Handle& handle, const ResetType reset_type, const std::string name) {
     Event* evt = new Event;
 
     handle = Kernel::g_object_pool.Create(evt);
@@ -108,6 +111,7 @@ Event* CreateEvent(Handle& handle, const ResetType reset_type) {
     evt->locked = true;
     evt->permanent_locked = false;
     evt->reset_type = evt->intitial_reset_type = reset_type;
+    evt->name = name;
 
     return evt;
 }
@@ -115,11 +119,12 @@ Event* CreateEvent(Handle& handle, const ResetType reset_type) {
 /**
  * Creates an event
  * @param reset_type ResetType describing how to create event
+ * @param name Optional name of event
  * @return Handle to newly created Event object
  */
-Handle CreateEvent(const ResetType reset_type) {
+Handle CreateEvent(const ResetType reset_type, const std::string name) {
     Handle handle;
-    Event* evt = CreateEvent(handle, reset_type);
+    Event* evt = CreateEvent(handle, reset_type, name);
     return handle;
 }
 
diff --git a/src/core/hle/kernel/event.h b/src/core/hle/kernel/event.h
index f91a72c1cd..eed09f0e32 100644
--- a/src/core/hle/kernel/event.h
+++ b/src/core/hle/kernel/event.h
@@ -37,8 +37,9 @@ Result ClearEvent(Handle handle);
 /**
  * Creates an event
  * @param reset_type ResetType describing how to create event
+ * @param name Optional name of event
  * @return Handle to newly created Event object
  */
-Handle CreateEvent(const ResetType reset_type);
+Handle CreateEvent(const ResetType reset_type, const std::string name="Unknown");
 
 } // namespace
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp
index 5ac88cd853..7e60fbfe0f 100644
--- a/src/core/hle/kernel/mutex.cpp
+++ b/src/core/hle/kernel/mutex.cpp
@@ -16,6 +16,7 @@ namespace Kernel {
 class Mutex : public Object {
 public:
     const char* GetTypeName() { return "Mutex"; }
+    const char* GetName() { return name.c_str(); }
 
     static Kernel::HandleType GetStaticHandleType() {  return Kernel::HandleType::Mutex; }
     Kernel::HandleType GetHandleType() const { return Kernel::HandleType::Mutex; }
@@ -24,6 +25,7 @@ public:
     bool locked;                                ///< Current locked state
     Handle lock_thread;                         ///< Handle to thread that currently has mutex
     std::vector<Handle> waiting_threads;        ///< Threads that are waiting for the mutex
+    std::string name;                           ///< Name of mutex (optional)
 
     /**
      * Synchronize kernel object 
@@ -128,13 +130,15 @@ Result ReleaseMutex(Handle handle) {
  * Creates a mutex
  * @param handle Reference to handle for the newly created mutex
  * @param initial_locked Specifies if the mutex should be locked initially
+ * @param name Optional name of mutex
  * @return Pointer to new Mutex object
  */
-Mutex* CreateMutex(Handle& handle, bool initial_locked) {
+Mutex* CreateMutex(Handle& handle, bool initial_locked, const std::string name) {
     Mutex* mutex = new Mutex;
     handle = Kernel::g_object_pool.Create(mutex);
 
     mutex->locked = mutex->initial_locked = initial_locked;
+    mutex->name = name;
 
     // Acquire mutex with current thread if initialized as locked...
     if (mutex->locked) {
@@ -150,10 +154,12 @@ Mutex* CreateMutex(Handle& handle, bool initial_locked) {
 /**
  * Creates a mutex
  * @param initial_locked Specifies if the mutex should be locked initially
+ * @param name Optional name of mutex
+ * @return Handle to newly created object
  */
-Handle CreateMutex(bool initial_locked) {
+Handle CreateMutex(bool initial_locked, std::string name) {
     Handle handle;
-    Mutex* mutex = CreateMutex(handle, initial_locked);
+    Mutex* mutex = CreateMutex(handle, initial_locked, name);
     return handle;
 }
 
diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h
index 4cd2667255..fde5549fa8 100644
--- a/src/core/hle/kernel/mutex.h
+++ b/src/core/hle/kernel/mutex.h
@@ -20,8 +20,9 @@ Result ReleaseMutex(Handle handle);
 /**
  * Creates a mutex
  * @param initial_locked Specifies if the mutex should be locked initially
+ * @param name Optional name of mutex
  * @return Handle to newly created object
  */
-Handle CreateMutex(bool initial_locked);
+Handle CreateMutex(bool initial_locked, const std::string name="Unknown");
 
 } // namespace
diff --git a/src/core/hle/service/apt.cpp b/src/core/hle/service/apt.cpp
index 10d9a94bde..33ee1ec8f5 100644
--- a/src/core/hle/service/apt.cpp
+++ b/src/core/hle/service/apt.cpp
@@ -19,8 +19,8 @@ void Initialize(Service::Interface* self) {
     u32* cmd_buff = Service::GetCommandBuffer();
     DEBUG_LOG(KERNEL, "called");
     
-    cmd_buff[3] = Kernel::CreateEvent(RESETTYPE_ONESHOT); // APT menu event handle
-    cmd_buff[4] = Kernel::CreateEvent(RESETTYPE_ONESHOT); // APT pause event handle
+    cmd_buff[3] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "APT_U:Menu");  // APT menu event handle
+    cmd_buff[4] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "APT_U:Pause"); // APT pause event handle
 
     Kernel::SetEventLocked(cmd_buff[3], true);
     Kernel::SetEventLocked(cmd_buff[4], false); // Fire start event
@@ -32,7 +32,7 @@ void GetLockHandle(Service::Interface* self) {
     u32* cmd_buff = Service::GetCommandBuffer();
     u32 flags = cmd_buff[1]; // TODO(bunnei): Figure out the purpose of the flag field
     cmd_buff[1] = 0; // No error
-    cmd_buff[5] = Kernel::CreateMutex(false);
+    cmd_buff[5] = Kernel::CreateMutex(false, "APT_U:Lock");
     DEBUG_LOG(KERNEL, "called handle=0x%08X", cmd_buff[5]);
 }
 
diff --git a/src/core/hle/service/srv.cpp b/src/core/hle/service/srv.cpp
index f1940e6f5a..07e2009a0c 100644
--- a/src/core/hle/service/srv.cpp
+++ b/src/core/hle/service/srv.cpp
@@ -17,7 +17,7 @@ Handle g_mutex = 0;
 void Initialize(Service::Interface* self) {
     DEBUG_LOG(OSHLE, "called");
     if (!g_mutex) {
-        g_mutex = Kernel::CreateMutex(false);
+        g_mutex = Kernel::CreateMutex(true, "SRV:Lock");
     }
 }