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

Refactor PROJ thread context handling #76

Closed
wants to merge 1 commit into from
Closed

Conversation

frewsxcv
Copy link
Member

@frewsxcv frewsxcv commented Feb 14, 2021

This pull request contains a new opaque struct ThreadContext that contains all thread context handling.

One benefit of this is the Drop implementations for Proj and ProjBuilder no longer need to handle destroying the thread context since that's now handled by the Drop implementation for ThreadContext. Also, ThreadContext now implements Clone, which gets us one step closer to implementing Clone for Proj. I chose the ThreadContext name because it's a little more descriptive than Context and the PROJ docs call this concept a "thread context". I created a separate file for it because I noticed proj.rs is getting on the larger side.

@frewsxcv frewsxcv marked this pull request as ready for review February 14, 2021 23:25
@urschrei
Copy link
Member

Bors try

bors bot added a commit that referenced this pull request Feb 15, 2021
@bors
Copy link
Contributor

bors bot commented Feb 15, 2021

try

Build succeeded:

@@ -892,7 +889,6 @@ impl Drop for Proj {
proj_area_destroy(area)
}
proj_destroy(self.c_proj);
Copy link
Member

@michaelkirk michaelkirk Feb 15, 2021

Choose a reason for hiding this comment

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

There is a change in ordering here, right?

previously:

proj_destroy(self.c_proj);
proj_context_destroy(self.ctx);
proj_cleanup();

Now we're effectively calling:

  proj_destroy(self.c_proj);
- proj_context_destroy(self.ctx);
  proj_cleanup();
+ proj_context_destroy(self.ctx); // via Drop impl on ThreadContext

Which seems at odds with the comment onproj_cleanup:

// NB do NOT call until proj_destroy and proj_context_destroy have both returned:

I don't actually know the repercussions of this though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wow wow great job catching this 😮

The only way I can think of to work around this is with https://doc.rust-lang.org/std/mem/struct.ManuallyDrop.html is clunky. Ugh

Copy link
Member Author

Choose a reason for hiding this comment

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

Also note to future selves: think about a way to write a regression test ensuring we call these FFI calls in the right order on drop

Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to move proj_cleanup into ThreadContext#drop?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe! Will need to think on it. Also relevant: #75

let ctx = unsafe { std::mem::replace(&mut self.ctx, proj_context_create()) };
Some(transform_string(ctx, definition)?)
pub fn proj(self, definition: &str) -> Option<Proj> {
Some(transform_string(self.ctx.clone(), definition)?)
Copy link
Member

Choose a reason for hiding this comment

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

neat - I didn't know about context_clone!

@frewsxcv
Copy link
Member Author

Closing this for now. I don't have a great solution for the issue @michaelkirk brought up, and no longer feeling confident in this strategy.

@frewsxcv frewsxcv closed this Feb 22, 2021
@urschrei urschrei mentioned this pull request Dec 15, 2021
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

Successfully merging this pull request may close these issues.

3 participants