Re: New libc malloc patch

From: David Xu <davidxu_at_freebsd.org>
Date: Sun, 04 Dec 2005 11:34:18 +0800
David Xu wrote:
> Here is sample code to implement a mutex by using umtx syscalls:
> ...
> void
> unlock_mtx(struct umtx *mtx)
> {
>    volatile uintptr_t *m = (volatile uintptr_t *)mtx;
> 
>    for (;;) {
>        if (atomic_load_acq_ptr(m) == LCK_UNLOCKED)
>            err(1, "unlock a unlocked mutex\n");
>        if (atomic_load_acq_ptr(m) == LCK_LOCKED) {
>            if (atomic_cmpset_acq_ptr(m, LCK_LOCKED, LCK_UNLOCKED))
>                return;
>        }
>        if (atomic_load_acq_ptr(m) == LCK_CONTENDED) {
>            atomic_store_rel_ptr(m, LCK_UNLOCKED);
>            _umtx_op((struct umtx *)m, UMTX_OP_WAKE, 1, NULL, NULL);
OOP, should be:
	 _umtx_op((struct umtx *)m, UMTX_OP_WAKE, INT_MAX, NULL, NULL);

This line is not very optimal if there are lots of thread waiting there.
:-)

There is optimal version using transaction id:
http://www.dragonflybsd.org/cvsweb/src/lib/libthread_xu/thread/thr_umtx.c?rev=1.2&content-type=text/x-cvsweb-markup
Though, libthr in freebsd does not use these semantices, instead they 
are implemented in kernel.

David Xu
Received on Sun Dec 04 2005 - 02:34:16 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:48 UTC