Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Suyu
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
many-archive
Suyu
Commits
2f3020a1
There was an error fetching the commit references. Please try again later.
Commit
2f3020a1
authored
10 years ago
by
bunnei
Browse files
Options
Downloads
Patches
Plain Diff
Mutex: Cleanup and remove redundant code.
parent
f09806ae
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/core/hle/kernel/mutex.cpp
+27
-45
27 additions, 45 deletions
src/core/hle/kernel/mutex.cpp
src/core/hle/kernel/mutex.h
+1
-1
1 addition, 1 deletion
src/core/hle/kernel/mutex.h
src/core/hle/kernel/thread.cpp
+1
-1
1 addition, 1 deletion
src/core/hle/kernel/thread.cpp
with
29 additions
and
47 deletions
src/core/hle/kernel/mutex.cpp
+
27
−
45
View file @
2f3020a1
...
...
@@ -23,9 +23,8 @@ public:
bool
initial_locked
;
///< Initial lock state when mutex was created
bool
locked
;
///< Current locked state
Handle
lock_thread
;
///< Handle to thread that currently has mutex
std
::
string
name
;
///< Name of mutex (optional)
SharedPtr
<
Thread
>
current
_thread
;
///< Thread that has acquired the mutex
SharedPtr
<
Thread
>
holding
_thread
;
///< Thread that has acquired the mutex
bool
ShouldWait
()
override
;
void
Acquire
()
override
;
...
...
@@ -33,18 +32,17 @@ public:
////////////////////////////////////////////////////////////////////////////////////////////////////
typedef
std
::
multimap
<
Handle
,
Handle
>
MutexMap
;
typedef
std
::
multimap
<
SharedPtr
<
Thread
>
,
SharedPtr
<
Mutex
>
>
MutexMap
;
static
MutexMap
g_mutex_held_locks
;
/**
* Acquires the specified mutex for the specified thread
* @param mutex Mutex that is to be acquired
* @param thread Thread that will acquire
d
* @param thread Thread that will acquire
the mutex
*/
void
MutexAcquireLock
(
Mutex
*
mutex
,
Handle
thread
=
GetCurrentThread
()
->
GetHandle
())
{
g_mutex_held_locks
.
insert
(
std
::
make_pair
(
thread
,
mutex
->
GetHandle
()));
mutex
->
lock_thread
=
thread
;
mutex
->
current_thread
=
Kernel
::
g_handle_table
.
Get
<
Thread
>
(
thread
);
void
MutexAcquireLock
(
Mutex
*
mutex
,
Thread
*
thread
)
{
g_mutex_held_locks
.
insert
(
std
::
make_pair
(
thread
,
mutex
));
mutex
->
holding_thread
=
thread
;
}
/**
...
...
@@ -55,51 +53,39 @@ void ResumeWaitingThread(Mutex* mutex) {
// Find the next waiting thread for the mutex...
auto
next_thread
=
mutex
->
WakeupNextThread
();
if
(
next_thread
!=
nullptr
)
{
MutexAcquireLock
(
mutex
,
next_thread
->
GetHandle
()
);
MutexAcquireLock
(
mutex
,
next_thread
);
}
else
{
// Reset mutex lock thread handle, nothing is waiting
mutex
->
locked
=
false
;
mutex
->
lock
_thread
=
-
1
;
mutex
->
holding
_thread
=
nullptr
;
}
}
void
MutexEraseLock
(
Mutex
*
mutex
)
{
Handle
handle
=
mutex
->
GetHandle
();
auto
locked
=
g_mutex_held_locks
.
equal_range
(
mutex
->
lock_thread
);
for
(
MutexMap
::
iterator
iter
=
locked
.
first
;
iter
!=
locked
.
second
;
++
iter
)
{
if
(
iter
->
second
==
handle
)
{
g_mutex_held_locks
.
erase
(
iter
);
break
;
}
}
mutex
->
lock_thread
=
-
1
;
}
void
ReleaseThreadMutexes
(
Handle
thread
)
{
void
ReleaseThreadMutexes
(
Thread
*
thread
)
{
auto
locked
=
g_mutex_held_locks
.
equal_range
(
thread
);
// Release every mutex that the thread holds, and resume execution on the waiting threads
for
(
MutexMap
::
iterator
iter
=
locked
.
first
;
iter
!=
locked
.
second
;
++
iter
)
{
Mutex
*
mutex
=
g_handle_table
.
Get
<
Mutex
>
(
iter
->
second
).
get
();
ResumeWaitingThread
(
mutex
);
for
(
auto
iter
=
locked
.
first
;
iter
!=
locked
.
second
;
++
iter
)
{
ResumeWaitingThread
(
iter
->
second
.
get
());
}
// Erase all the locks that this thread holds
g_mutex_held_locks
.
erase
(
thread
);
}
bool
LockMutex
(
Mutex
*
mutex
)
{
// Mutex alread locked?
bool
ReleaseMutex
(
Mutex
*
mutex
)
{
if
(
mutex
->
locked
)
{
return
false
;
}
MutexAcquireLock
(
mutex
);
return
true
;
}
auto
locked
=
g_mutex_held_locks
.
equal_range
(
mutex
->
holding_thread
);
bool
ReleaseMutex
(
Mutex
*
mutex
)
{
MutexEraseLock
(
mutex
);
ResumeWaitingThread
(
mutex
);
for
(
MutexMap
::
iterator
iter
=
locked
.
first
;
iter
!=
locked
.
second
;
++
iter
)
{
if
(
iter
->
second
==
mutex
)
{
g_mutex_held_locks
.
erase
(
iter
);
break
;
}
}
ResumeWaitingThread
(
mutex
);
}
return
true
;
}
...
...
@@ -134,16 +120,12 @@ Mutex* CreateMutex(Handle& handle, bool initial_locked, const std::string& name)
mutex
->
locked
=
mutex
->
initial_locked
=
initial_locked
;
mutex
->
name
=
name
;
mutex
->
current
_thread
=
nullptr
;
mutex
->
holding
_thread
=
nullptr
;
// Acquire mutex with current thread if initialized as locked...
if
(
mutex
->
locked
)
{
MutexAcquireLock
(
mutex
);
if
(
mutex
->
locked
)
MutexAcquireLock
(
mutex
,
GetCurrentThread
()
);
// Otherwise, reset lock thread handle
}
else
{
mutex
->
lock_thread
=
-
1
;
}
return
mutex
;
}
...
...
@@ -160,13 +142,13 @@ Handle CreateMutex(bool initial_locked, const std::string& name) {
}
bool
Mutex
::
ShouldWait
()
{
return
locked
&&
current
_thread
!=
GetCurrentThread
();
return
locked
&&
holding
_thread
!=
GetCurrentThread
();
}
void
Mutex
::
Acquire
()
{
_assert_msg_
(
Kernel
,
!
ShouldWait
(),
"object unavailable!"
);
locked
=
true
;
MutexAcquireLock
(
this
);
MutexAcquireLock
(
this
,
GetCurrentThread
()
);
}
}
// namespace
This diff is collapsed.
Click to expand it.
src/core/hle/kernel/mutex.h
+
1
−
1
View file @
2f3020a1
...
...
@@ -28,6 +28,6 @@ Handle CreateMutex(bool initial_locked, const std::string& name="Unknown");
* Releases all the mutexes held by the specified thread
* @param thread Thread that is holding the mutexes
*/
void
ReleaseThreadMutexes
(
Handle
thread
);
void
ReleaseThreadMutexes
(
Thread
*
thread
);
}
// namespace
This diff is collapsed.
Click to expand it.
src/core/hle/kernel/thread.cpp
+
1
−
1
View file @
2f3020a1
...
...
@@ -100,7 +100,7 @@ static bool CheckWait_AddressArbiter(const Thread* thread, VAddr wait_address) {
/// Stops the current thread
void
Thread
::
Stop
(
const
char
*
reason
)
{
// Release all the mutexes that this thread holds
ReleaseThreadMutexes
(
GetHandle
()
);
ReleaseThreadMutexes
(
this
);
ChangeReadyState
(
this
,
false
);
status
=
THREADSTATUS_DORMANT
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment