-
Notifications
You must be signed in to change notification settings - Fork 96
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
Creating error callbacks #215
Conversation
Can you use |
Okay, I've done two things:
|
@ChristianBeilschmidt Thanks for the implementation. Since concurrency is a bit hard to debug / reason about, I wanted to discuss a few points on the overall impl.
Edit: fix lock-free logic to not require leaking the box ptr. |
I guess we can't. GDAL doesn't take the ownership of that pointer, it treats it as a reference. Thus, we would leak if we transfer the ownership to GDAL nevertheless and it gets discarded by a call to
I changed it here: bd4beaa
I've addressed it here: bd4beaa
if you only use one Box and you move it into the Mutex, the address changes. The inner Box makes the address stable.
I could try it and see if we don't introduce new race conditions. However, the function is most likely not called in huge concurrency. So I don't see the benefit in not locking, here. |
can we merge it like it is or do we need more discussion / changes? |
I'm suggesting not using user-data of GDAL at all, and only using our locked structure. So the only pointer being passed to GDAL is the
If you move a let mut boxed = Box::new(0usize);
let ptr: *mut _ = boxed.as_mut() as *mut _;
let mutex = Mutex::new(boxed);
assert_eq!(
mutex.lock().unwrap().as_mut() as *mut _,
ptr
);
Ok. @jdroenner A few minor suggestions, mostly for clarity and remove extra allocations. |
I hesitate to switch to
In your example, you have a pointer to a |
Rust actually never drops any static variable, so even now things won't get dropped when the program exits unless
See this playground for example of boxing a Also, pls. add a line in CHANGES.md about this feature. |
The playground, unfortunately, disregards one aspect as far as I see:
|
Hmm, that's interesting. Could you add the explanations in the code for future reference? I suppose we can avoid the extra allocation if we don't use user-data at all, and just use the lock within the error handler. |
I'll go ahead and approve the pr as I don't see any major issues. Thanks @ChristianBeilschmidt |
I'm also learning new things with this FFI stuff. So it is good that you look carefully at these PRs 😄. |
CHANGES.md
if knowledge of this change could be valuable to users.I've attached two options for setting up the global GDAL error callback. One is thread-safe and comes with a new dependecy and the other one is not.
The reason for storing the callback in a static variable is that we cannot pass the ownership to GDAL. We can access a generic pointer via the
pUserData
field and cast it to our function type. Therefore, in order not to leak ourFnMut
, we have to store it somehow and drop it later on.When we decide on which version to use, I can remove the other one and convert the draft to a PR.