cvs commit: src/sys/i386/include atomic.h

John Baldwin jhb at freebsd.org
Tue Mar 28 18:57:43 UTC 2006


On Tuesday 28 March 2006 13:05, Dag-Erling Smørgrav wrote:
> John Baldwin <jhb at freebsd.org> writes:
> > One reason for not having the casts, btw, is that you lose type
> > checking.
> 
> 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 */

...

/* 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

...

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 = 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) == 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((uintptr_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.

-- 
John Baldwin <jhb at FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve"  =  http://www.FreeBSD.org


More information about the cvs-all mailing list