New libc malloc patch

David Xu davidxu at freebsd.org
Sat Dec 3 19:34:17 PST 2005


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



More information about the freebsd-current mailing list