summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/kernel')
-rw-r--r--src/core/hle/kernel/k_handle_table.cpp1
-rw-r--r--src/core/hle/kernel/k_process.cpp11
-rw-r--r--src/core/hle/kernel/kernel.cpp36
-rw-r--r--src/core/hle/kernel/kernel.h8
-rw-r--r--src/core/hle/kernel/svc.cpp6
5 files changed, 51 insertions, 11 deletions
diff --git a/src/core/hle/kernel/k_handle_table.cpp b/src/core/hle/kernel/k_handle_table.cpp
index 44d13169f..e90fc0628 100644
--- a/src/core/hle/kernel/k_handle_table.cpp
+++ b/src/core/hle/kernel/k_handle_table.cpp
@@ -56,6 +56,7 @@ bool KHandleTable::Remove(Handle handle) {
56 } 56 }
57 57
58 // Close the object. 58 // Close the object.
59 kernel.UnregisterInUseObject(obj);
59 obj->Close(); 60 obj->Close();
60 return true; 61 return true;
61} 62}
diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp
index 211157ccc..76fd8c285 100644
--- a/src/core/hle/kernel/k_process.cpp
+++ b/src/core/hle/kernel/k_process.cpp
@@ -434,11 +434,6 @@ void KProcess::PrepareForTermination() {
434} 434}
435 435
436void KProcess::Finalize() { 436void KProcess::Finalize() {
437 // Release memory to the resource limit.
438 if (resource_limit != nullptr) {
439 resource_limit->Close();
440 }
441
442 // Finalize the handle table and close any open handles. 437 // Finalize the handle table and close any open handles.
443 handle_table.Finalize(); 438 handle_table.Finalize();
444 439
@@ -460,6 +455,12 @@ void KProcess::Finalize() {
460 } 455 }
461 } 456 }
462 457
458 // Release memory to the resource limit.
459 if (resource_limit != nullptr) {
460 resource_limit->Close();
461 resource_limit = nullptr;
462 }
463
463 // Perform inherited finalization. 464 // Perform inherited finalization.
464 KAutoObjectWithSlabHeapAndContainer<KProcess, KSynchronizationObject>::Finalize(); 465 KAutoObjectWithSlabHeapAndContainer<KProcess, KSynchronizationObject>::Finalize();
465} 466}
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index bea945301..4a139c5e7 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -91,12 +91,6 @@ struct KernelCore::Impl {
91 } 91 }
92 92
93 void Shutdown() { 93 void Shutdown() {
94 // Shutdown all processes.
95 if (current_process) {
96 current_process->Finalize();
97 current_process->Close();
98 current_process = nullptr;
99 }
100 process_list.clear(); 94 process_list.clear();
101 95
102 // Close all open server ports. 96 // Close all open server ports.
@@ -170,6 +164,24 @@ struct KernelCore::Impl {
170 // Next host thead ID to use, 0-3 IDs represent core threads, >3 represent others 164 // Next host thead ID to use, 0-3 IDs represent core threads, >3 represent others
171 next_host_thread_id = Core::Hardware::NUM_CPU_CORES; 165 next_host_thread_id = Core::Hardware::NUM_CPU_CORES;
172 166
167 // Close kernel objects that were not freed on shutdown
168 {
169 std::lock_guard lk(registered_in_use_objects_lock);
170 if (registered_in_use_objects.size()) {
171 for (auto& object : registered_in_use_objects) {
172 object->Close();
173 }
174 registered_in_use_objects.clear();
175 }
176 }
177
178 // Shutdown all processes.
179 if (current_process) {
180 current_process->Finalize();
181 current_process->Close();
182 current_process = nullptr;
183 }
184
173 // Track kernel objects that were not freed on shutdown 185 // Track kernel objects that were not freed on shutdown
174 { 186 {
175 std::lock_guard lk(registered_objects_lock); 187 std::lock_guard lk(registered_objects_lock);
@@ -714,9 +726,11 @@ struct KernelCore::Impl {
714 std::unordered_set<KServerPort*> server_ports; 726 std::unordered_set<KServerPort*> server_ports;
715 std::unordered_set<KServerSession*> server_sessions; 727 std::unordered_set<KServerSession*> server_sessions;
716 std::unordered_set<KAutoObject*> registered_objects; 728 std::unordered_set<KAutoObject*> registered_objects;
729 std::unordered_set<KAutoObject*> registered_in_use_objects;
717 std::mutex server_ports_lock; 730 std::mutex server_ports_lock;
718 std::mutex server_sessions_lock; 731 std::mutex server_sessions_lock;
719 std::mutex registered_objects_lock; 732 std::mutex registered_objects_lock;
733 std::mutex registered_in_use_objects_lock;
720 734
721 std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor; 735 std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor;
722 std::vector<Kernel::PhysicalCore> cores; 736 std::vector<Kernel::PhysicalCore> cores;
@@ -928,6 +942,16 @@ void KernelCore::UnregisterKernelObject(KAutoObject* object) {
928 impl->registered_objects.erase(object); 942 impl->registered_objects.erase(object);
929} 943}
930 944
945void KernelCore::RegisterInUseObject(KAutoObject* object) {
946 std::lock_guard lk(impl->registered_in_use_objects_lock);
947 impl->registered_in_use_objects.insert(object);
948}
949
950void KernelCore::UnregisterInUseObject(KAutoObject* object) {
951 std::lock_guard lk(impl->registered_in_use_objects_lock);
952 impl->registered_in_use_objects.erase(object);
953}
954
931bool KernelCore::IsValidNamedPort(NamedPortTable::const_iterator port) const { 955bool KernelCore::IsValidNamedPort(NamedPortTable::const_iterator port) const {
932 return port != impl->named_ports.cend(); 956 return port != impl->named_ports.cend();
933} 957}
diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h
index b6658b437..d2ceae950 100644
--- a/src/core/hle/kernel/kernel.h
+++ b/src/core/hle/kernel/kernel.h
@@ -204,6 +204,14 @@ public:
204 /// destroyed during the current emulation session. 204 /// destroyed during the current emulation session.
205 void UnregisterKernelObject(KAutoObject* object); 205 void UnregisterKernelObject(KAutoObject* object);
206 206
207 /// Registers kernel objects with guest in use state, this is purely for close
208 /// after emulation has been shutdown.
209 void RegisterInUseObject(KAutoObject* object);
210
211 /// Unregisters a kernel object previously registered with RegisterInUseObject when it was
212 /// destroyed during the current emulation session.
213 void UnregisterInUseObject(KAutoObject* object);
214
207 /// Determines whether or not the given port is a valid named port. 215 /// Determines whether or not the given port is a valid named port.
208 bool IsValidNamedPort(NamedPortTable::const_iterator port) const; 216 bool IsValidNamedPort(NamedPortTable::const_iterator port) const;
209 217
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index 7f38ade1c..c43135856 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -427,11 +427,15 @@ static ResultCode WaitSynchronization(Core::System& system, s32* index, VAddr ha
427 R_UNLESS(handle_table.GetMultipleObjects<KSynchronizationObject>(objs.data(), handles, 427 R_UNLESS(handle_table.GetMultipleObjects<KSynchronizationObject>(objs.data(), handles,
428 num_handles), 428 num_handles),
429 ResultInvalidHandle); 429 ResultInvalidHandle);
430 for (const auto& obj : objs) {
431 kernel.RegisterInUseObject(obj);
432 }
430 } 433 }
431 434
432 // Ensure handles are closed when we're done. 435 // Ensure handles are closed when we're done.
433 SCOPE_EXIT({ 436 SCOPE_EXIT({
434 for (u64 i = 0; i < num_handles; ++i) { 437 for (u64 i = 0; i < num_handles; ++i) {
438 kernel.UnregisterInUseObject(objs[i]);
435 objs[i]->Close(); 439 objs[i]->Close();
436 } 440 }
437 }); 441 });
@@ -1561,6 +1565,7 @@ static ResultCode StartThread(Core::System& system, Handle thread_handle) {
1561 1565
1562 // If we succeeded, persist a reference to the thread. 1566 // If we succeeded, persist a reference to the thread.
1563 thread->Open(); 1567 thread->Open();
1568 system.Kernel().RegisterInUseObject(thread.GetPointerUnsafe());
1564 1569
1565 return ResultSuccess; 1570 return ResultSuccess;
1566} 1571}
@@ -1576,6 +1581,7 @@ static void ExitThread(Core::System& system) {
1576 auto* const current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread(); 1581 auto* const current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread();
1577 system.GlobalSchedulerContext().RemoveThread(current_thread); 1582 system.GlobalSchedulerContext().RemoveThread(current_thread);
1578 current_thread->Exit(); 1583 current_thread->Exit();
1584 system.Kernel().UnregisterInUseObject(current_thread);
1579} 1585}
1580 1586
1581static void ExitThread32(Core::System& system) { 1587static void ExitThread32(Core::System& system) {