Skip to content

[All - Fix] Improved threading safety, notably on macOS #130

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

Merged
merged 16 commits into from
Oct 9, 2024

Conversation

Kodikuu
Copy link

@Kodikuu Kodikuu commented Aug 20, 2024

When destroying threading-related contexts, the NULL assignment is moved to be first.
When calling other threading-related functions, the context is NULL-checked first.

This should help prevent some rare and specific race conditions around the calling of threading functions during or after the destruction of their contexts.

@Kodikuu Kodikuu added the 2. Review requested You have coded your code, and can now be reviewed. label Aug 20, 2024
@Kodikuu Kodikuu requested a review from Namaneo August 20, 2024 14:55
@Kodikuu Kodikuu self-assigned this Aug 20, 2024
@Kodikuu Kodikuu added 1. Coding Feature has been described inside of the parsec branch, and you are coding on it. and removed 2. Review requested You have coded your code, and can now be reviewed. labels Sep 5, 2024
@Kodikuu Kodikuu changed the title Thread safety [All - Fix] Improved threading safety, notably on macOS Sep 11, 2024
@Kodikuu Kodikuu added 2. Review requested You have coded your code, and can now be reviewed. and removed 1. Coding Feature has been described inside of the parsec branch, and you are coding on it. labels Sep 13, 2024
@Kodikuu Kodikuu requested review from bmcnett and Namaneo September 13, 2024 15:03
@@ -270,6 +291,9 @@ static void *thread_pool_func(void *opaque)

uint32_t MTY_ThreadPoolDispatch(MTY_ThreadPool *ctx, MTY_AnonFunc func, void *opaque)
{
if (!ctx)
Copy link

Choose a reason for hiding this comment

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

should we either emit an MTY_Log warning specific to this new error (NULL context being passed) or emit the same MTY_Log warning we do when we can't find an available index?

EnterCriticalSection(&ctx->mutex);
}

bool MTY_MutexTryLock(MTY_Mutex *ctx)
{
if (!ctx)
return false;;
Copy link

Choose a reason for hiding this comment

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

two semicolons here ;;

Copy link

@bmcnett bmcnett left a comment

Choose a reason for hiding this comment

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

eventually, we would want to emit a log warning for these extraordinary cases, where the code expects to do a thing through a pointer, and the pointer is unexpectedly NULL. handy if we had a WARNING macro that logged the source file name & line number, then it wouldn't take much creativity to come up with a unique log string

…tion (not just a macro)

for cross-platform code to use a memory barrier, there must be a function (not just a macro)
@Namaneo Namaneo added 3. 2nd review stage 2nd opinion has reviewed the code. and removed 2. Review requested You have coded your code, and can now be reviewed. labels Sep 16, 2024
@Namaneo
Copy link

Namaneo commented Sep 16, 2024

A follow-up PR from @bmcnett changes the memory barrier to be a function instead of a macro, depending on when this PR is intended to be merged you might want to update it to reflect the upcoming changes.

@Kodikuu Kodikuu changed the base branch from stable to memory-barrier-as-function September 16, 2024 11:22
@Kodikuu
Copy link
Author

Kodikuu commented Sep 16, 2024

Rebased onto new branch and adjusted to use new function. No further changes

@Kodikuu Kodikuu requested review from bmcnett and Namaneo September 16, 2024 11:23
@Kodikuu Kodikuu added 2. Review requested You have coded your code, and can now be reviewed. and removed 3. 2nd review stage 2nd opinion has reviewed the code. labels Sep 16, 2024
Copy link

@Namaneo Namaneo left a comment

Choose a reason for hiding this comment

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

Approved with a single suggestion, that might not even be related to this very PR, but I figured I'd still write it here since their are examples to illustrate what I'm saying.

src/thread.c Outdated
@@ -55,17 +55,21 @@ void MTY_RWLockDestroy(MTY_RWLock **rwlock)
return;

MTY_RWLock *ctx = *rwlock;
*rwlock = NULL;
MTY_MemoryBarrier();
Copy link

Choose a reason for hiding this comment

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

Just as a wild thought, it would be neat to have an MTY_SecureNull(...) function, which would have a this definition:

void *MTY_SecureNull(void **ptr)
{
	void *old_ptr = *ptr;
	*ptr = NULL;
	MTY_MemoryBarrier();
	return old_ptr;
}

And taking that block as an example, you'd simply do instead: MTY_RWLock *ctx = MTY_SecureNull(rwlock);

Disclaimer: I don't know how memory barriers work internally, i.e. if they're scoped to the executing function or not. If they are, my suggestion just make no sense, please ignore.

Copy link
Author

Choose a reason for hiding this comment

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

What you're actually after here is an atomic pointer swap, which Bryan does have code for. But I just want to get this work in, because it covers the main issue. Atomically swapping the pointer is less of an improvement (but still one)

See https://github.com/parsec-cloud/libmatoya/commits/thread-safety-barrier/

@bmcnett bmcnett changed the base branch from memory-barrier-as-function to stable September 16, 2024 14:34
not yet, for this thing
remnants of the thing we don't want here
and, the spacing
@RonaldH-Parsec RonaldH-Parsec added 4. Impl. Reviewed Implementation has been reviewed by technical 2nd op. and is ready for cdd review. and removed 2. Review requested You have coded your code, and can now be reviewed. labels Sep 18, 2024
@RonaldH-Parsec RonaldH-Parsec merged commit afdb89d into stable Oct 9, 2024
@RonaldH-Parsec RonaldH-Parsec deleted the thread-safety branch October 9, 2024 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4. Impl. Reviewed Implementation has been reviewed by technical 2nd op. and is ready for cdd review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants