Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 28 Mar 2006 13:58:24 -0500
From:      John Baldwin <jhb@freebsd.org>
To:        "Dag-Erling =?iso-8859-1?q?Sm=F8rgrav?=" <des@des.no>
Cc:        cvs-src@freebsd.org, src-committers@freebsd.org, cvs-all@freebsd.org
Subject:   Re: cvs commit: src/sys/i386/include atomic.h
Message-ID:  <200603281358.26703.jhb@freebsd.org>
In-Reply-To: <86psk6wilj.fsf@xps.des.no>
References:  <200603281434.k2SEYmaR031447@repoman.freebsd.org> <200603281300.09419.jhb@freebsd.org> <86psk6wilj.fsf@xps.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tuesday 28 March 2006 13:05, Dag-Erling Sm=F8rgrav wrote:
> John Baldwin <jhb@freebsd.org> writes:
> > One reason for not having the casts, btw, is that you lose type
> > checking.
>=20
> Huh?  Before my patch, any use of atomic_*_ptr with warnings turned
> off would result in a slew of warnings because you'd be passing
> pointers to a function which is declared to take u_int.  The only way
> to make this type safe is to use inline functions instead of the
> macros I wrote.

s/off/on/ I trust

Not true.  The tinderbox would attest to that.  Please see code such as
this:

#define MTX_UNOWNED	0x00000004	/* Cookie for free mutex */

=2E..

/* Try to obtain mtx_lock once. */
#ifndef _obtain_lock
#define _obtain_lock(mp, tid)						\
	atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
#endif

=2E..

void
_mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
    int line)
{
	...
	uintptr_t v;
	...

	while (!_obtain_lock(m, tid)) {
		...
		v =3D m->mtx_lock;
		...

		/*
		 * If the mutex isn't already contested and a failure occurs
		 * setting the contested bit, the mutex was either released
		 * or the state of the MTX_RECURSED bit changed.
		 */
		if ((v & MTX_CONTESTED) =3D=3D 0 &&
		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
			...
			continue;
		}
		...
	}
	...
}

etc.  kern_rwlock.c has this as well.  The parts of the kernel that
do use pointers already use explicit casts to uintptr_t where needed:

dev/hatm/if_hatm_intr.c:                if (atomic_cmpset_ptr((uintptr_t *)=
list, (uintptr_t)buf->link,
dev/hatm/if_hatm_intr.c:                if (atomic_cmpset_ptr((uintptr_t *)=
&sc->mbuf_list[g],
dev/hatm/if_hatm_intr.c:                        if (atomic_cmpset_ptr((uint=
ptr_t *)&sc->mbuf_list[g],

Even userland uses casts when it uses void * rather than uintptr_t for
the underlying type.  See src/lib/libpthread/sys/lock.c or
src/lib/libthr/thr_umtx.h.

=2D-=20
John Baldwin <jhb@FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve"  =3D  http://www.FreeBSD.org



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200603281358.26703.jhb>