From owner-freebsd-questions Thu May 6 15:51:55 1999 Delivered-To: freebsd-questions@freebsd.org Received: from bolero-x.rahul.net (bolero.rahul.net [192.160.13.1]) by hub.freebsd.org (Postfix) with SMTP id B7F1A14CCB for ; Thu, 6 May 1999 15:51:46 -0700 (PDT) (envelope-from dhesi@rahul.net) Received: from q.bolero.rahul.net (bolero.rahul.net) by bolero-x.rahul.net with SMTP id AA19855 (5.67b8/IDA-1.5 for ); Thu, 6 May 1999 15:51:44 -0700 Message-Id: <199905062251.AA19855@bolero-x.rahul.net> Received: (qmail 19851 invoked from network); 6 May 1999 22:51:44 -0000 Received: from waltz.rahul.net (192.160.13.9) by bolero.rahul.net with SMTP; 6 May 1999 22:51:44 -0000 To: freebsd-questions@freebsd.org Subject: looking for test-and-set or atomic swap instruction Date: Thu, 06 May 1999 15:51:42 -0700 From: Rahul Dhesi Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message