Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible deadlock issues #224

Open
ryancaicse opened this issue Mar 30, 2022 · 3 comments
Open

Possible deadlock issues #224

ryancaicse opened this issue Mar 30, 2022 · 3 comments

Comments

@ryancaicse
Copy link

Hi, it seems that lock device->sync_mutex is missed to be released after 248. In the while loop, the lock could be acquired twice and a deadlock occurs.

cen64/device/device.c

Lines 230 to 256 in 1b31ca9

while (likely(device->running)) {
unsigned i, j;
for (i = 0; i < 6250 / 2; i++) {
for (j = 0; j < 2; j++) {
ai_cycle(&device->ai);
pi_cycle(&device->pi);
}
for (j = 0; j < 3; j++)
vr4300_cycle(device->vr4300);
}
// Sync up with the RCP thread.
cen64_mutex_lock(&device->sync_mutex);
if (!device->other_thread_is_waiting) {
device->other_thread_is_waiting = true;
cen64_cv_wait(&device->sync_cv, &device->sync_mutex);
}
else {
device->other_thread_is_waiting = false;
cen64_mutex_unlock(&device->sync_mutex);
cen64_cv_signal(&device->sync_cv);
}
}

@ryancaicse
Copy link
Author

Here is another one similar to the above.

cen64/device/device.c

Lines 230 to 255 in 1b31ca9

while (likely(device->running)) {
unsigned i, j;
for (i = 0; i < 6250 / 2; i++) {
for (j = 0; j < 2; j++) {
ai_cycle(&device->ai);
pi_cycle(&device->pi);
}
for (j = 0; j < 3; j++)
vr4300_cycle(device->vr4300);
}
// Sync up with the RCP thread.
cen64_mutex_lock(&device->sync_mutex);
if (!device->other_thread_is_waiting) {
device->other_thread_is_waiting = true;
cen64_cv_wait(&device->sync_cv, &device->sync_mutex);
}
else {
device->other_thread_is_waiting = false;
cen64_mutex_unlock(&device->sync_mutex);
cen64_cv_signal(&device->sync_cv);
}

@ryancaicse
Copy link
Author

Could anyone take a look? thanks

@tj90241
Copy link
Collaborator

tj90241 commented Nov 27, 2022

This is expected... see the manpage for pthread_cond_wait:

These functions atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants