pthread_mutex_timedlock()

289 views
Skip to first unread message

Brad Larson

unread,
Mar 3, 2009, 4:54:56 PM3/3/09
to android-ndk
Are there any plans to add pthread_mutex_timedlock() to bionic/libc/
bionic/pthread.c? It is defined in bionic/libc/include/pthread.h

Thanks!
-Brad

David Turner

unread,
Mar 4, 2009, 2:56:46 AM3/4/09
to andro...@googlegroups.com
No plan yet, you're the first to actually ask for it.
Please file an issue, I'll try to see what I can do, but this will probably not be implemented immediately.

And of course, patches welcome and all that.

thanks in advance

Brad Larson

unread,
Mar 4, 2009, 10:46:03 AM3/4/09
to android-ndk
Thanks for the reply!

I've created an issue at http://code.google.com/p/android/issues/detail?id=2138

Is there any code I could reference for how pthread_mutex_timedlock()
could be implemented? Is the other code in pthread.c based on
anything which might also implement timedlock? Or was it all written
from scratch?

Thanks!

-Brad

On Mar 4, 1:56 am, David Turner <di...@android.com> wrote:
> No plan yet, you're the first to actually ask for it.Please file an issue,
> I'll try to see what I can do, but this will probably not be implemented
> immediately.
>
> And of course, patches welcome and all that.
>
> thanks in advance
>

David Turner

unread,
Mar 4, 2009, 11:00:37 AM3/4/09
to andro...@googlegroups.com
On Wed, Mar 4, 2009 at 4:46 PM, Brad Larson <bkla...@gmail.com> wrote:

Thanks for the reply!

I've created an issue at http://code.google.com/p/android/issues/detail?id=2138

Is there any code I could reference for how pthread_mutex_timedlock()
could be implemented?  Is the other code in pthread.c based on
anything which might also implement timedlock?  Or was it all written
from scratch?

You will need to start from pthread_lock(), the only trick point should be that you'll
need to compute a relative timeout each time __futex_wait() is called, and also check
for time-outs explicitely before calling it

See __pthread_cond_timedwait() implementation and other sources here:

http://android.git.kernel.org/?p=platform/bionic.git;a=blob;f=libc/bionic/pthread.c;h=6114f40ebffbc8c809493342413fd15ae6170dd5;hb=c2f23bb1cdea20fc5e8e5b8101d9d0bd6d14b74b

Good luck
 

Brad Larson

unread,
Mar 4, 2009, 4:54:20 PM3/4/09
to android-ndk
Hi David,

Thanks again for your replies.

I found some other futex_mutex code in frameworks/base/libs/utils/
futex_synchro.c, which does implement timedlock. Do you know why this
code exists and frameworks/base/libs/utils/Threads.cpp is using it
instead of pthreads? Looking at the timedlock implementation, it
doesn't recalculate the relative timeout that gets passed to
__futex_wait()... is this a bug?

Thanks!
-Brad

On Mar 4, 10:00 am, David Turner <di...@android.com> wrote:
> On Wed, Mar 4, 2009 at 4:46 PM, Brad Larson <bklar...@gmail.com> wrote:
>
> > Thanks for the reply!
>
> > I've created an issue at
> >http://code.google.com/p/android/issues/detail?id=2138
>
> > Is there any code I could reference for how pthread_mutex_timedlock()
> > could be implemented?  Is the other code in pthread.c based on
> > anything which might also implement timedlock?  Or was it all written
> > from scratch?
>
> You will need to start from pthread_lock(), the only trick point should be
> that you'll
> need to compute a relative timeout each time __futex_wait() is called, and
> also check
> for time-outs explicitely before calling it
>
> See __pthread_cond_timedwait() implementation and other sources here:
>
> http://android.git.kernel.org/?p=platform/bionic.git;a=blob;f=libc/bi...

David Turner

unread,
Mar 4, 2009, 5:35:08 PM3/4/09
to andro...@googlegroups.com
I was not aware of this implementation. What you describe definitely looks like a bug,
you really need to recompute the relative timeout on each wait loop iteration.

I wonder if this code is really used in the first place. I'll have a look to check this.

David Turner

unread,
Mar 4, 2009, 8:14:23 PM3/4/09
to andro...@googlegroups.com
I just checked the sources. futex_mutex_lock() takes a milliseconds timeout parameter
(see frameworks/base/libs/utils/futex_synchro.c).

However all clients use FUTEX_WAIT_INFINITE and never exercise the timed lock feature, which implementation is broken.
Reply all
Reply to author
Forward
0 new messages