Date: Thu, 06 May 1999 15:51:42 -0700 From: Rahul Dhesi <dhesi@rahul.net> To: freebsd-questions@freebsd.org Subject: looking for test-and-set or atomic swap instruction Message-ID: <199905062251.AA19855@bolero-x.rahul.net>
index | next in thread | raw e-mail
I am looking for a machine instruction that could be used to implement
mutual exclusion in software, similar to the traditional test-and-set or
atomic swap instructions, and some way of invoking it from within C code
from within FreeBSD 3.x. Any suggestions? More info below.
I don't expect to be using a multiprocessing system, so perhaps that
should make things easier.
I can't use any lock that requires a system call. I am making accesses
to common shared memory from multiple processes, and a system call would
be too costly. The rate of shared memory access will be high but
conflicts will be very rare, so a spin lock will do just fine.
A test-and-set instruction will set a specified byte to 1, and return
the previous value of the byte in a status flag. If implemented as a
test_and_set() function, we would use it like this:
char volatile lockbyte;
/* get lock */
while (test_and_set(lockbyte)) {
; /* wait in spin lock */
}
...
/* release lock */
lockbyte = 0;
An atomic swap does the same thing, if we can atomically swap the value
of a memory location with a register. The new value in the register
gives us the old value in the memory location, same as the flag set by
test-and-set.
Rahul Dhesi <dhesi@rahul.net>
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199905062251.AA19855>
