summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Subv2016-12-08 10:34:53 -0500
committerGravatar Subv2016-12-09 12:23:09 -0500
commit17b29d8865ea4d96c18f7e1671bd6d0f01eab95f (patch)
tree20137ff3eed145ae36db64ccf4b54cf37384c82c
parentUse boost remove_erase_if instead of the erase-remove idiom (diff)
downloadyuzu-17b29d8865ea4d96c18f7e1671bd6d0f01eab95f.tar.gz
yuzu-17b29d8865ea4d96c18f7e1671bd6d0f01eab95f.tar.xz
yuzu-17b29d8865ea4d96c18f7e1671bd6d0f01eab95f.zip
WaitSynch: Removed unused variables and reduced SharedPtr copies.
Define a variable with the value of the sync timeout error code. Use a boost::flat_map instead of an unordered_map to hold the equivalence of objects and wait indices in a WaitSynchN call.
-rw-r--r--src/citra_qt/debugger/wait_tree.cpp2
-rw-r--r--src/core/hle/kernel/kernel.cpp14
-rw-r--r--src/core/hle/kernel/kernel.h5
-rw-r--r--src/core/hle/kernel/thread.h5
-rw-r--r--src/core/hle/svc.cpp105
5 files changed, 57 insertions, 74 deletions
diff --git a/src/citra_qt/debugger/wait_tree.cpp b/src/citra_qt/debugger/wait_tree.cpp
index 829ac7dd6..8ff094209 100644
--- a/src/citra_qt/debugger/wait_tree.cpp
+++ b/src/citra_qt/debugger/wait_tree.cpp
@@ -230,7 +230,7 @@ std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeThread::GetChildren() const {
230 list.push_back(std::make_unique<WaitTreeMutexList>(thread.held_mutexes)); 230 list.push_back(std::make_unique<WaitTreeMutexList>(thread.held_mutexes));
231 } 231 }
232 if (thread.status == THREADSTATUS_WAIT_SYNCH) { 232 if (thread.status == THREADSTATUS_WAIT_SYNCH) {
233 list.push_back(std::make_unique<WaitTreeObjectList>(thread.wait_objects, thread.IsWaitingAll())); 233 list.push_back(std::make_unique<WaitTreeObjectList>(thread.wait_objects, thread.IsSleepingOnWaitAll()));
234 } 234 }
235 235
236 return list; 236 return list;
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index b8b69f9d0..653697843 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -34,14 +34,11 @@ void WaitObject::RemoveWaitingThread(Thread* thread) {
34 34
35SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() { 35SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
36 // Remove the threads that are ready or already running from our waitlist 36 // Remove the threads that are ready or already running from our waitlist
37 boost::range::remove_erase_if(waiting_threads, [](const SharedPtr<Thread>& thread) -> bool { 37 boost::range::remove_erase_if(waiting_threads, [](const SharedPtr<Thread>& thread) {
38 return thread->status == THREADSTATUS_RUNNING || thread->status == THREADSTATUS_READY; 38 return thread->status == THREADSTATUS_RUNNING || thread->status == THREADSTATUS_READY;
39 }); 39 });
40 40
41 if (waiting_threads.empty()) 41 Thread* candidate = nullptr;
42 return nullptr;
43
44 SharedPtr<Thread> candidate = nullptr;
45 s32 candidate_priority = THREADPRIO_LOWEST + 1; 42 s32 candidate_priority = THREADPRIO_LOWEST + 1;
46 43
47 for (const auto& thread : waiting_threads) { 44 for (const auto& thread : waiting_threads) {
@@ -52,7 +49,7 @@ SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
52 return object->ShouldWait(); 49 return object->ShouldWait();
53 }); 50 });
54 if (ready_to_run) { 51 if (ready_to_run) {
55 candidate = thread; 52 candidate = thread.get();
56 candidate_priority = thread->current_priority; 53 candidate_priority = thread->current_priority;
57 } 54 }
58 } 55 }
@@ -61,9 +58,8 @@ SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
61} 58}
62 59
63void WaitObject::WakeupAllWaitingThreads() { 60void WaitObject::WakeupAllWaitingThreads() {
64 // Wake up all threads that can be awoken, in priority order
65 while (auto thread = GetHighestPriorityReadyThread()) { 61 while (auto thread = GetHighestPriorityReadyThread()) {
66 if (thread->wait_objects.empty()) { 62 if (!thread->IsSleepingOnWaitAll()) {
67 Acquire(); 63 Acquire();
68 // Set the output index of the WaitSynchronizationN call to the index of this object. 64 // Set the output index of the WaitSynchronizationN call to the index of this object.
69 if (thread->wait_set_output) { 65 if (thread->wait_set_output) {
@@ -73,7 +69,6 @@ void WaitObject::WakeupAllWaitingThreads() {
73 } else { 69 } else {
74 for (auto object : thread->wait_objects) { 70 for (auto object : thread->wait_objects) {
75 object->Acquire(); 71 object->Acquire();
76 // Remove the thread from the object's waitlist
77 object->RemoveWaitingThread(thread.get()); 72 object->RemoveWaitingThread(thread.get());
78 } 73 }
79 // Note: This case doesn't update the output index of WaitSynchronizationN. 74 // Note: This case doesn't update the output index of WaitSynchronizationN.
@@ -81,7 +76,6 @@ void WaitObject::WakeupAllWaitingThreads() {
81 thread->wait_objects.clear(); 76 thread->wait_objects.clear();
82 } 77 }
83 78
84 // Set the result of the call to WaitSynchronization to RESULT_SUCCESS
85 thread->SetWaitSynchronizationResult(RESULT_SUCCESS); 79 thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
86 thread->ResumeFromWait(); 80 thread->ResumeFromWait();
87 // Note: Removing the thread from the object's waitlist will be done by GetHighestPriorityReadyThread 81 // Note: Removing the thread from the object's waitlist will be done by GetHighestPriorityReadyThread
diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h
index eb5a3bf7e..4227d2373 100644
--- a/src/core/hle/kernel/kernel.h
+++ b/src/core/hle/kernel/kernel.h
@@ -152,7 +152,10 @@ public:
152 */ 152 */
153 void RemoveWaitingThread(Thread* thread); 153 void RemoveWaitingThread(Thread* thread);
154 154
155 /// Wake up all threads waiting on this object 155 /**
156 * Wake up all threads waiting on this object that can be awoken, in priority order,
157 * and set the synchronization result and output of the thread.
158 */
156 void WakeupAllWaitingThreads(); 159 void WakeupAllWaitingThreads();
157 160
158 /// Obtains the highest priority thread that is ready to run from this object's waiting list. 161 /// Obtains the highest priority thread that is ready to run from this object's waiting list.
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index 1b29fb3a3..4c254cb9d 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -7,6 +7,7 @@
7#include <string> 7#include <string>
8#include <unordered_map> 8#include <unordered_map>
9#include <vector> 9#include <vector>
10#include <boost/container/flat_map.hpp>
10#include <boost/container/flat_set.hpp> 11#include <boost/container/flat_set.hpp>
11#include "common/common_types.h" 12#include "common/common_types.h"
12#include "core/core.h" 13#include "core/core.h"
@@ -153,7 +154,7 @@ public:
153 * its wait list to become ready, as a result of a WaitSynchronizationN call 154 * its wait list to become ready, as a result of a WaitSynchronizationN call
154 * with wait_all = true, or a ReplyAndReceive call. 155 * with wait_all = true, or a ReplyAndReceive call.
155 */ 156 */
156 bool IsWaitingAll() const { 157 bool IsSleepingOnWaitAll() const {
157 return !wait_objects.empty(); 158 return !wait_objects.empty();
158 } 159 }
159 160
@@ -183,7 +184,7 @@ public:
183 /// This is only populated when the thread should wait for all the objects to become ready. 184 /// This is only populated when the thread should wait for all the objects to become ready.
184 std::vector<SharedPtr<WaitObject>> wait_objects; 185 std::vector<SharedPtr<WaitObject>> wait_objects;
185 186
186 std::unordered_map<int, s32> wait_objects_index; ///< Mapping of Object ids to their position in the last waitlist that this object waited on. 187 boost::container::flat_map<int, s32> wait_objects_index; ///< Mapping of Object ids to their position in the last waitlist that this object waited on.
187 188
188 VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address 189 VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address
189 190
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp
index c06df84b3..14da09883 100644
--- a/src/core/hle/svc.cpp
+++ b/src/core/hle/svc.cpp
@@ -41,6 +41,9 @@ const ResultCode ERR_PORT_NAME_TOO_LONG(ErrorDescription(30), ErrorModule::OS,
41 ErrorSummary::InvalidArgument, 41 ErrorSummary::InvalidArgument,
42 ErrorLevel::Usage); // 0xE0E0181E 42 ErrorLevel::Usage); // 0xE0E0181E
43 43
44const ResultCode ERR_SYNC_TIMEOUT(ErrorDescription::Timeout, ErrorModule::OS,
45 ErrorSummary::StatusChanged, ErrorLevel::Info);
46
44const ResultCode ERR_MISALIGNED_ADDRESS{// 0xE0E01BF1 47const ResultCode ERR_MISALIGNED_ADDRESS{// 0xE0E01BF1
45 ErrorDescription::MisalignedAddress, ErrorModule::OS, 48 ErrorDescription::MisalignedAddress, ErrorModule::OS,
46 ErrorSummary::InvalidArgument, ErrorLevel::Usage}; 49 ErrorSummary::InvalidArgument, ErrorLevel::Usage};
@@ -257,11 +260,8 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
257 260
258 if (object->ShouldWait()) { 261 if (object->ShouldWait()) {
259 262
260 if (nano_seconds == 0) { 263 if (nano_seconds == 0)
261 return ResultCode(ErrorDescription::Timeout, ErrorModule::OS, 264 return ERR_SYNC_TIMEOUT;
262 ErrorSummary::StatusChanged,
263 ErrorLevel::Info);
264 }
265 265
266 object->AddWaitingThread(thread); 266 object->AddWaitingThread(thread);
267 // TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion. 267 // TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
@@ -273,9 +273,7 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
273 273
274 // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in its wait objects. 274 // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in its wait objects.
275 // Otherwise we retain the default value of timeout. 275 // Otherwise we retain the default value of timeout.
276 return ResultCode(ErrorDescription::Timeout, ErrorModule::OS, 276 return ERR_SYNC_TIMEOUT;
277 ErrorSummary::StatusChanged,
278 ErrorLevel::Info);
279 } 277 }
280 278
281 object->Acquire(); 279 object->Acquire();
@@ -286,8 +284,6 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
286/// Wait for the given handles to synchronize, timeout after the specified nanoseconds 284/// Wait for the given handles to synchronize, timeout after the specified nanoseconds
287static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wait_all, 285static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wait_all,
288 s64 nano_seconds) { 286 s64 nano_seconds) {
289 bool wait_thread = !wait_all;
290 int handle_index = 0;
291 Kernel::Thread* thread = Kernel::GetCurrentThread(); 287 Kernel::Thread* thread = Kernel::GetCurrentThread();
292 288
293 // Check if 'handles' is invalid 289 // Check if 'handles' is invalid
@@ -305,7 +301,6 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
305 ErrorSummary::InvalidArgument, ErrorLevel::Usage); 301 ErrorSummary::InvalidArgument, ErrorLevel::Usage);
306 302
307 using ObjectPtr = Kernel::SharedPtr<Kernel::WaitObject>; 303 using ObjectPtr = Kernel::SharedPtr<Kernel::WaitObject>;
308
309 std::vector<ObjectPtr> objects(handle_count); 304 std::vector<ObjectPtr> objects(handle_count);
310 305
311 for (int i = 0; i < handle_count; ++i) { 306 for (int i = 0; i < handle_count; ++i) {
@@ -320,100 +315,90 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
320 // It will be repopulated later in the wait_all = false case. 315 // It will be repopulated later in the wait_all = false case.
321 thread->wait_objects_index.clear(); 316 thread->wait_objects_index.clear();
322 317
323 if (!wait_all) { 318 if (wait_all) {
324 // Find the first object that is acquireable in the provided list of objects 319 bool all_available = std::all_of(objects.begin(), objects.end(), [](const ObjectPtr& object) {
325 auto itr = std::find_if(objects.begin(), objects.end(), [](const ObjectPtr& object) {
326 return !object->ShouldWait(); 320 return !object->ShouldWait();
327 }); 321 });
328 322 if (all_available) {
329 if (itr != objects.end()) { 323 // We can acquire all objects right now, do so.
330 // We found a ready object, acquire it and set the result value 324 for (auto object : objects)
331 ObjectPtr object = *itr; 325 object->Acquire();
332 object->Acquire(); 326 // Note: In this case, the `out` parameter is not set, and retains whatever value it had before.
333 *out = std::distance(objects.begin(), itr);
334 return RESULT_SUCCESS; 327 return RESULT_SUCCESS;
335 } 328 }
336 329
337 // No objects were ready to be acquired, prepare to suspend the thread. 330 // Not all objects were available right now, prepare to suspend the thread.
338 331
339 // If a timeout value of 0 was provided, just return the Timeout error code instead of suspending the thread. 332 // If a timeout value of 0 was provided, just return the Timeout error code instead of suspending the thread.
340 if (nano_seconds == 0) { 333 if (nano_seconds == 0)
341 return ResultCode(ErrorDescription::Timeout, ErrorModule::OS, 334 return ERR_SYNC_TIMEOUT;
342 ErrorSummary::StatusChanged,
343 ErrorLevel::Info);
344 }
345 335
346 // Put the thread to sleep 336 // Put the thread to sleep
347 thread->status = THREADSTATUS_WAIT_SYNCH; 337 thread->status = THREADSTATUS_WAIT_SYNCH;
348 338
349 // Clear the thread's waitlist, we won't use it for wait_all = false
350 thread->wait_objects.clear();
351
352 // Add the thread to each of the objects' waiting threads. 339 // Add the thread to each of the objects' waiting threads.
353 for (size_t i = 0; i < objects.size(); ++i) { 340 for (auto& object : objects) {
354 ObjectPtr object = objects[i];
355 // Set the index of this object in the mapping of Objects -> index for this thread.
356 thread->wait_objects_index[object->GetObjectId()] = static_cast<int>(i);
357 object->AddWaitingThread(thread); 341 object->AddWaitingThread(thread);
358 // TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion. 342 // TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
359 // Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here. 343 // Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here.
360 } 344 }
361 345
362 // Note: If no handles and no timeout were given, then the thread will deadlock, this is consistent with hardware behavior. 346 // Set the thread's waitlist to the list of objects passed to WaitSynchronizationN
347 thread->wait_objects = std::move(objects);
363 348
364 // Create an event to wake the thread up after the specified nanosecond delay has passed 349 // Create an event to wake the thread up after the specified nanosecond delay has passed
365 thread->WakeAfterDelay(nano_seconds); 350 thread->WakeAfterDelay(nano_seconds);
366 351
367 // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in one of its wait objects. 352 // This value gets set to -1 by default in this case, it is not modified after this.
368 // Otherwise we retain the default value of timeout, and -1 in the out parameter
369 thread->wait_set_output = true;
370 *out = -1; 353 *out = -1;
371 return ResultCode(ErrorDescription::Timeout, ErrorModule::OS, 354 // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in one of its wait objects.
372 ErrorSummary::StatusChanged, 355 return ERR_SYNC_TIMEOUT;
373 ErrorLevel::Info);
374 } else { 356 } else {
375 bool all_available = std::all_of(objects.begin(), objects.end(), [](const ObjectPtr& object) { 357 // Find the first object that is acquirable in the provided list of objects
358 auto itr = std::find_if(objects.begin(), objects.end(), [](const ObjectPtr& object) {
376 return !object->ShouldWait(); 359 return !object->ShouldWait();
377 }); 360 });
378 if (all_available) { 361
379 // We can acquire all objects right now, do so. 362 if (itr != objects.end()) {
380 for (auto object : objects) 363 // We found a ready object, acquire it and set the result value
381 object->Acquire(); 364 Kernel::WaitObject* object = itr->get();
382 // Note: In this case, the `out` parameter is not set, and retains whatever value it had before. 365 object->Acquire();
366 *out = std::distance(objects.begin(), itr);
383 return RESULT_SUCCESS; 367 return RESULT_SUCCESS;
384 } 368 }
385 369
386 // Not all objects were available right now, prepare to suspend the thread. 370 // No objects were ready to be acquired, prepare to suspend the thread.
387 371
388 // If a timeout value of 0 was provided, just return the Timeout error code instead of suspending the thread. 372 // If a timeout value of 0 was provided, just return the Timeout error code instead of suspending the thread.
389 if (nano_seconds == 0) { 373 if (nano_seconds == 0)
390 return ResultCode(ErrorDescription::Timeout, ErrorModule::OS, 374 return ERR_SYNC_TIMEOUT;
391 ErrorSummary::StatusChanged,
392 ErrorLevel::Info);
393 }
394 375
395 // Put the thread to sleep 376 // Put the thread to sleep
396 thread->status = THREADSTATUS_WAIT_SYNCH; 377 thread->status = THREADSTATUS_WAIT_SYNCH;
397 378
398 // Set the thread's waitlist to the list of objects passed to WaitSynchronizationN 379 // Clear the thread's waitlist, we won't use it for wait_all = false
399 thread->wait_objects = objects; 380 thread->wait_objects.clear();
400 381
401 // Add the thread to each of the objects' waiting threads. 382 // Add the thread to each of the objects' waiting threads.
402 for (auto object : objects) { 383 for (size_t i = 0; i < objects.size(); ++i) {
384 Kernel::WaitObject* object = objects[i].get();
385 // Set the index of this object in the mapping of Objects -> index for this thread.
386 thread->wait_objects_index[object->GetObjectId()] = static_cast<int>(i);
403 object->AddWaitingThread(thread); 387 object->AddWaitingThread(thread);
404 // TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion. 388 // TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
405 // Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here. 389 // Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here.
406 } 390 }
407 391
392 // Note: If no handles and no timeout were given, then the thread will deadlock, this is consistent with hardware behavior.
393
408 // Create an event to wake the thread up after the specified nanosecond delay has passed 394 // Create an event to wake the thread up after the specified nanosecond delay has passed
409 thread->WakeAfterDelay(nano_seconds); 395 thread->WakeAfterDelay(nano_seconds);
410 396
411 // This value gets set to -1 by default in this case, it is not modified after this.
412 *out = -1;
413 // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in one of its wait objects. 397 // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in one of its wait objects.
414 return ResultCode(ErrorDescription::Timeout, ErrorModule::OS, 398 // Otherwise we retain the default value of timeout, and -1 in the out parameter
415 ErrorSummary::StatusChanged, 399 thread->wait_set_output = true;
416 ErrorLevel::Info); 400 *out = -1;
401 return ERR_SYNC_TIMEOUT;
417 } 402 }
418} 403}
419 404