-
Notifications
You must be signed in to change notification settings - Fork 111
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
ndk-glue: Move native activity behind lock and remove it in onDestroy
#160
ndk-glue: Move native activity behind lock and remove it in onDestroy
#160
Conversation
10cbc84
to
81b225f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
That makes sense
probably not worth overdoing this |
It would be clean. Not everyone might use ndk_glue (though it is unlikely) and it would lead to a dangling pointer (though that's the least of our concerns) and ANR with no way to recover if it's lost. |
Sure if you want to do it |
81b225f
to
68af0ab
Compare
@dvc94ch Done :) |
So is the drop handler guaranteed to be executed before the mutex is released? |
@dvc94ch |
wait didn't this used to be a RwLock? |
I guess not, so ignore my comments :) |
It's probably not behind a lock because it's always "statically" set during activity execution and doesn't change (well, except when someone returns from |
I think the |
Yes, but only if the user calls |
Exactly. I think we might change |
well that's the point, it's two separate instructions, so if there are multiple threads there could be a race. But I'm probably overanalyzing this. |
So what I'm saying is the program does this:
while 1 and 2 could actually happen at the same time meaning it's UB. |
Reading or writing the pointers is only a single instruction. The point is to prevent other threads doing so while the thread holding the lock is doing different things with it. No such case with |
|
The only one making |
Yeah I think that's the point we're trying to make - or the convention we're trying to set - here. The crate shouldn't have any threads live after returning from |
We can possibly drop/ EDIT: I doubt the |
I think taking self by reference would avoid writing to a |
It's funny how the smaller the PR the larger the discussion hahha |
Yup the bigger churn is never an issue, but smaller controversial lines like these need to be thoroughly thought out and discussed. It's good that it's a single, isolated change in a PR anyway, and not hidden in some mega-PR :)
If we decide to expose drop/
That's possible. Just like normal Android In this instance automatically calling
Pretty sure we'd see segfaults or other UB if the user were to continue using After a
|
That sounds good yes.
I guess it won't be possible to use it with the glue, as we only expose a |
In hindsight I'm not too certain about the proposed changes above. The looper is explicitly associated with the current thread, and returning from
Yes a separate read and write lock function have to be exposed, or what I was experimenting with today: Perhaps it is a good idea to collect some more opinions on this complicated and controversial change. Maybe @msiglreith and @francesca64 have some suggestions? |
@msiglreith Checking out What do you think about the takeaway steps listed in #160 (comment)? In short:
|
@MarijnS95 I'm fine with these. Regarding
IIRC it just post some event to another thread/process and returns.
yeah. this seems to be the best way to move forward (even though a bit hacky). |
@msiglreith Thanks for your view, and apologies for letting this sit unattended for a while.
EDIT: Realizing now that this doesn't make much sense, given what I wrote below.
So the pointer to
It's already a pattern for |
68af0ab
to
fd98799
Compare
main()
onDestroy
fd98799
to
fd239d0
Compare
I don't think this PR is actually fixing "issue" #154 anymore. We should probably update the documentation and examples to call |
to be honest don't quite get the purpose of this change. also needs to be rebased. |
@dvc94ch No surprise, we've been going back and forth on possible issues and implementations of this. IIRC the gist is that we can call |
hi sirs pub fn main() { |
may be we need use atom bomb process::exit(1); |
fd239d0
to
37d16d0
Compare
Rebased and made it so that this does not fix #154 anymore; it's just correctness cleanup. Besides, it's not going to be very relevant anymore now that https://crates.io/crates/android-activity exists! |
37d16d0
to
5d93933
Compare
let mut native_activity_guard = NATIVE_ACTIVITY.write(); | ||
let native_activity = native_activity_guard.take().unwrap(); | ||
assert_eq!(native_activity.ptr().as_ptr(), activity); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NativeActivity
doesn't have a drop handler (that would previously call .finish()
in the original revision of this PR) so nothing funky is going to happen, just that users will now get None
out of fn native_activity()
.
`NativeActivity` is now behind a lock, and will be removed as soon as `onDestroy` is called. Due to the async nature of events, make sure to hold on to the lock received from `native_activity()` _beforehand_ if you wish to use it during handling of `Event::Destroy`.
5d93933
to
b27b2b7
Compare
When an application returns its control flow back frommain()
it is generally assumed to be done polling on the looper and wishes to terminate. Android is made aware of this by callingANativeActivity_finish
, otherwise it'll continue sending input eventsand eventually ANR as the app didn't consume them anymore.
NativeActivity
is now behind a lock, and will be removed as soon asonDestroy
is called. Due to the async nature of events, make sure to hold on to the lock received fromnative_activity()
beforehand if you wish to use it during handling ofEvent::Destroy
.TODO: I assume https://developer.android.com/ndk/reference/group/native-activity#anativeactivity_finish says thatDone!ANativeActivity
is invalid after this call? If so we should makefn finish(&self)
takeANativeActivty
by value instead (to force use of.take()
) - we could even have a drop handler?