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

pthread_cond_wait: Use atomic_t to protect the waiter count #339

Merged
merged 6 commits into from
Jan 20, 2025

Conversation

pussuw
Copy link

@pussuw pussuw commented Jan 17, 2025

pthread_cond_wait: Use atomic_t to protect the waiter count

From upstream (hopefully)

atomic_read is missing from the nuttx headers, it is added later in
upstream.

!! REMOVE THIS COMMIT WHEN REBASING WITH UPSTREAM !!
Copy link

@jlaitine jlaitine left a comment

Choose a reason for hiding this comment

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

Neat!

Because sem_getvalue modifies errno.
sem_getvalue returns ERROR and sets errno if it fails, we don't want to
return OK in this case, we want to return the non-negated error number.
sem_getvalue returns ERROR and sets errno if it fails, we don't want to
return OK in this case, we want to return the non-negated error number.
The load/compare and RMW to wait_count need protection. Using atomic
operations should resolve both issues.

NOTE:
The assumption that the user will call pthread_cond_signal /
pthread_cond_broadcast with the mutex given to pthread_cond_wait held is
simply not true. It MAY hold it, but it is not forced. Thus, using the
user space lock for protecting the wait counter as well is not valid!

The pthread_cond_signal() or pthread_cond_broadcast() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behaviour is required, then that mutex is locked by the thread calling pthread_cond_signal() or pthread_cond_broadcast().

[1] https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cond_signal.html
@jlaitine
Copy link

LGTM; just wondering whether the i2c and fb driver changes are here on purpose (fb we are not even using afaik) ?

@pussuw
Copy link
Author

pussuw commented Jan 20, 2025

They can be dropped / are not tightly related. Just took everything that was changed in the upstream PR.

@jlaitine
Copy link

it is ok to keep those!

@pussuw pussuw merged commit e53908a into master Jan 20, 2025
10 checks passed
@pussuw pussuw deleted the fix_pt_cond branch January 20, 2025 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants