why is pthread_mutex_lock a wrapper?

why is pthread_mutex_lock a wrapper?

Post by Thomas Maier-Komo » Wed, 22 Feb 2006 03:00:07



Hello,

as the subject says - I would like to know why the pthread_mutex_lock
function in libc wraps an implementation function?

$ dis -F pthread_mutex_lock /lib/libc.so.1
                ****   DISASSEMBLER  ****

disassembly for /lib/libc.so.1

section .text
pthread_mutex_lock()
    _private_mutex_lock:    82 10 00 0f  mov          %o7, %g1
    _private_mutex_lock+0x4: 92 10 20 00  clr          %o1
    _private_mutex_lock+0x8: 7f ff ff 7d  call         mutex_lock_impl
    _private_mutex_lock+0xc: 9e 10 00 01  mov          %g1, %o7
$ dis -F mutex_lock /lib/libc.so.1
                ****   DISASSEMBLER  ****

disassembly for /lib/libc.so.1

section .text
mutex_lock()
    _private_mutex_lock:    82 10 00 0f  mov          %o7, %g1
    _private_mutex_lock+0x4: 92 10 20 00  clr          %o1
    _private_mutex_lock+0x8: 7f ff ff 7d  call         mutex_lock_impl
    _private_mutex_lock+0xc: 9e 10 00 01  mov          %g1, %o7

So both mutex_lock and pthread_mutex_lock are effictively the same code.
Why don't their symbols point directly to mutex_lock_impl? In contrast
pthread_mutex_unlock and mutex_unlock refer directly to
_private_mutex_unlock...

Any idea?

Tom

 
 
 

why is pthread_mutex_lock a wrapper?

Post by roger.faulk.. » Wed, 22 Feb 2006 06:31:15



> Hello,

> as the subject says - I would like to know why the pthread_mutex_lock
> function in libc wraps an implementation function?

> $ dis -F pthread_mutex_lock /lib/libc.so.1
>                 ****   DISASSEMBLER  ****

> disassembly for /lib/libc.so.1

> section .text
> pthread_mutex_lock()
>     _private_mutex_lock:    82 10 00 0f  mov          %o7, %g1
>     _private_mutex_lock+0x4: 92 10 20 00  clr          %o1
>     _private_mutex_lock+0x8: 7f ff ff 7d  call         mutex_lock_impl
>     _private_mutex_lock+0xc: 9e 10 00 01  mov          %g1, %o7
> $ dis -F mutex_lock /lib/libc.so.1
>                 ****   DISASSEMBLER  ****

> disassembly for /lib/libc.so.1

> section .text
> mutex_lock()
>     _private_mutex_lock:    82 10 00 0f  mov          %o7, %g1
>     _private_mutex_lock+0x4: 92 10 20 00  clr          %o1
>     _private_mutex_lock+0x8: 7f ff ff 7d  call         mutex_lock_impl
>     _private_mutex_lock+0xc: 9e 10 00 01  mov          %g1, %o7

> So both mutex_lock and pthread_mutex_lock are effictively the same code.
> Why don't their symbols point directly to mutex_lock_impl? In contrast
> pthread_mutex_unlock and mutex_unlock refer directly to
> _private_mutex_unlock...

> Any idea?

> Tom

The mutex_lock_impl() function contains common code for both
pthread_mutex_lock() and pthread_mutex_timedlock().
It's an implementation detail, not to be worried about.

Also, pthread_mutex_lock() and mutex_lock() are not just effectively
the same code, they are exactly the same code.  Do this to see
the addresses of the functions in libc:
    nm -xvD /lib/libc.so.1 | grep mutex_lock
and you will see that the two functions have the same address.
They are synonyms of each other.

Roger Faulkner
Sun Microsystems

 
 
 

why is pthread_mutex_lock a wrapper?

Post by Thomas Maier-Komo » Wed, 22 Feb 2006 07:49:31




>> Hello,

>> as the subject says - I would like to know why the pthread_mutex_lock
>> function in libc wraps an implementation function?

>> $ dis -F pthread_mutex_lock /lib/libc.so.1
>>                 ****   DISASSEMBLER  ****

>> disassembly for /lib/libc.so.1

>> section .text
>> pthread_mutex_lock()
>>     _private_mutex_lock:    82 10 00 0f  mov          %o7, %g1
>>     _private_mutex_lock+0x4: 92 10 20 00  clr          %o1
>>     _private_mutex_lock+0x8: 7f ff ff 7d  call         mutex_lock_impl
>>     _private_mutex_lock+0xc: 9e 10 00 01  mov          %g1, %o7
>> $ dis -F mutex_lock /lib/libc.so.1
>>                 ****   DISASSEMBLER  ****

>> disassembly for /lib/libc.so.1

>> section .text
>> mutex_lock()
>>     _private_mutex_lock:    82 10 00 0f  mov          %o7, %g1
>>     _private_mutex_lock+0x4: 92 10 20 00  clr          %o1
>>     _private_mutex_lock+0x8: 7f ff ff 7d  call         mutex_lock_impl
>>     _private_mutex_lock+0xc: 9e 10 00 01  mov          %g1, %o7

>> So both mutex_lock and pthread_mutex_lock are effictively the same code.
>> Why don't their symbols point directly to mutex_lock_impl? In contrast
>> pthread_mutex_unlock and mutex_unlock refer directly to
>> _private_mutex_unlock...

>> Any idea?

>> Tom

> The mutex_lock_impl() function contains common code for both
> pthread_mutex_lock() and pthread_mutex_timedlock().
> It's an implementation detail, not to be worried about.

Ah, now I understand. Implementing those functions with a common
codebase makes them consistent without any further effort and reduces
the size of libc, too. Good point indeed.

Quote:> Also, pthread_mutex_lock() and mutex_lock() are not just effectively
> the same code, they are exactly the same code.  Do this to see
> the addresses of the functions in libc:
>     nm -xvD /lib/libc.so.1 | grep mutex_lock
> and you will see that the two functions have the same address.
> They are synonyms of each other.

Yes, the fact that they have the same address made me wondering why they
did not refer directly to mutex_lock_impl. But I forgot about
pthread_mutex_timedlock, which has much in common with them.

Quote:> Roger Faulkner
> Sun Microsystems

Thank you very much.

Cheers,
Tom