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

Fix mutex locking in _pkcs11h_threading_cond{Init,Wait} functions. #65

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/pkcs11h-threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ _pkcs11h_threading_condInit (
if (
(
pthread_mutex_init (&cond->mut, NULL) ||
pthread_cond_init (&cond->cond, NULL) ||
pthread_mutex_lock (&cond->mut)
pthread_cond_init (&cond->cond, NULL)
alonbl marked this conversation as resolved.
Show resolved Hide resolved
)
) {
rv = CKR_FUNCTION_FAILED;
Expand Down Expand Up @@ -423,7 +422,13 @@ _pkcs11h_threading_condWait (
goto cleanup;
}
#else
int unlock_mutex = 0;
if (milli == PKCS11H_COND_INFINITE) {
if (pthread_mutex_lock (&cond->mut)) {
rv = CKR_FUNCTION_FAILED;
goto cleanup;
}
unlock_mutex = 1;
if (pthread_cond_wait (&cond->cond, &cond->mut) ) {
rv = CKR_FUNCTION_FAILED;
goto cleanup;
Expand All @@ -441,6 +446,11 @@ _pkcs11h_threading_condWait (
timeout.tv_sec = now.tv_sec + milli/1000;
timeout.tv_nsec = now.tv_usec*1000 + milli%1000;

if (pthread_mutex_trylock (&cond->mut)) {
rv = CKR_FUNCTION_FAILED;
goto cleanup;
}
unlock_mutex = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should wait not fail if other holds the lock, can it be done?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could spin in a loop calling pthread_mutex_trylock until times passes out. But this is basically a spinlock.
Not sure if this is a good idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I remember this was my initial use of cond as mutex cannot wait... yes, spin loop is bad, there must be a different solution, otherwise the pthread_cond_timedwait has little sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the mutex is only used for waiting on the condition variable, the amount of time it is locked should be really short. Maybe spinlock here is actually OK.

Anyways, this change doesn't make the code any worse than it was.

if (pthread_cond_timedwait (&cond->cond, &cond->mut, &timeout)) {
rv = CKR_FUNCTION_FAILED;
goto cleanup;
Expand All @@ -449,6 +459,9 @@ _pkcs11h_threading_condWait (
#endif
rv = CKR_OK;
cleanup:
if (unlock_mutex) {
pthread_mutex_unlock (&cond->mut);
}
return rv;
}

Expand Down
Loading