Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Android/Bionic doesn't implement rand_r #1470

Closed
wants to merge 1 commit into from

7 participants

@mikrohard

This fixes Android build after #1448 was merged.

@Montellese
Owner

This does not exactly the same thing as on other platforms but I'm not sure how to handle it best. Calling srand() with the same number for every call to GetRandomNumber() will always result in the same number being returned from rand(). Calling srand() with time(NULL) as an argument would probably work but the numbers generated in sequence aren't really random either because there's a different seed for every number. And if multiple calls to GetRandomNumber happen within the same second we have the same problem that all the numbers returned by rand() will be the same.

The problem is that srand() and rand() are thread-local and GetRandomNumber can be called from different threads. I'm not sure myself how to solve this.

@mikrohard

Well... I thought that s_randomSeed is randomized over time. But I didn't check.

I think that calling srand with time(NULL) should be ok until a better solution is found. At least the build isn't broken..

@mikrohard mikrohard Android/Bionic doesn't implement rand_r
This fixes Android build after #1448 was merged.
92f6902
@topfs2
Collaborator

srand is not reentrant or threadsafe. So you would need to lock afaict

@fape

What do you think about simple add rand_r implementation manualy?
Like this: http://gitorious.org/android-pc/external-sshfs/commit/dca49dbc512de2c9dd170d2e402048e7db540ecf

@t-nelson

We don't want to maintain libc. This might be a case for our second line of java. :( Thoughts @theuni ?

@mikrohard

Just a thought... What about using Boost.Random? Boost is already a dependency...

Boost.Random does not maintain global state that would need protection from multi-threading.

Boost.Random is thread-safe as long as you don't access any given object from two threads simultaneously. (Accessing two different objects is ok, as long as they don't share an engine). If you require that kind of safety, it's trivial to roll that on your own with an appropriate mutex wrapper.

@davilla

nak on using Boost.Random

@davilla

adding the source for rand_r into android specific's might be the best until Google sees a need to add it.

@theuni
Owner

We don't want boost.random, our current boost usage is header-only, and we need to keep it that way.

I actually prefer fape's proposal, but tweaked a bit. We already need the crystax ndk to build, and I've had the suspicion for a while that we'd need to modify it eventually. Looks like this is it. In fact.. one of our deps (samba maybe?) I've already had to hack to bypass rand_r.

Edit: Posted a request for crystax, maybe we can avoid forking for now. See here: http://www.crystax.net/trac/ndk/ticket/102

Until then, I'm ok with adding it the same way we added getdelim.

@theuni
Owner

Update: see above link, crystax will add rand_r for the next release. I'm going to create a ticket for getdelim as well.

For now, I added a local version in 92e2a05.

Thanks all.

@theuni theuni closed this
@tru tru referenced this pull request from a commit in plexinc/plex-home-theater-public
@tru tru Improved token verification behaviour.
PHT will now handle the token being reset from plex.tv much more
reliable. In the process MyPlexManager also logs much better
information on errors.

Fixes #1470
086a89e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 24, 2012
  1. @mikrohard

    Android/Bionic doesn't implement rand_r

    mikrohard authored
    This fixes Android build after #1448 was merged.
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 0 deletions.
  1. +2 −0  xbmc/Util.cpp
View
2  xbmc/Util.cpp
@@ -2618,6 +2618,8 @@ int CUtil::GetRandomNumber()
unsigned int number;
if (rand_s(&number) == 0)
return (int)number;
+#elif defined(TARGET_ANDROID)
+ srand(time(NULL));
#else
return rand_r(&s_randomSeed);
#endif
Something went wrong with that request. Please try again.