Article View: net.micro.16k
Article #247Re: Need word version of test-and-set
From: Steve Villee
Date: Thu, 26 Dec 1985 19:43
Date: Thu, 26 Dec 1985 19:43
57 lines
2866 bytes
2866 bytes
> I am looking for a fast way to exit a critical section and see if anyone
> else was waiting to get in. This is relatively easy (if rather obscure) to
> do on other processors (like PDP-11, S/370, VAX, MC68000). Despite being
> a NS32000 booster, I can't think of a way to do this on the NS32000.
There is a way to do this on any processor that has a test-and-set primitive.
The basic idea is to have a counter which is -1 if the resource is free, and
otherwise the number of processes waiting to use the resource. This counter
is updated using ordinary (non-atomic) instructions, but access to it is gated
via an outer gate using the test-and-set primitive. You just spin on the outer
gate until you get in, quickly examine and update the counter, and then open
the outer gate. If done properly, contention for the outer gate should be
minimal, and there should be very little actual "spinning" on it.
My National code is a little rusty, but here goes:
counter: .word -1
outer_gate: .byte 0
; ...
Seize_resource:
sbitib 0,outer_gate ; Try to shut the outer gate.
bfs Seize_resource ; Already shut - loop.
addqw 1,counter ; Update the counter.
cmpqw 0,counter ; See if resource was free. (Ugh!)
movqb 0,outer_gate ; Open gate in any case.
beq label_a ; Yes - the resource is ours now.
; (Perform a P-op on the semaphore associated with the resource.)
label_a: ; We now have the resource.
; ...
Release_resource:
sbitib 0,outer_gate ; Try to shut the outer gate.
bfs Release_resource ; Already shut - loop.
addqw -1,counter ; Update the counter.
movqb 0,outer_gate ; Open gate.
bcc label_b ; It went to -1, so nobody waiting.
; (Perform a V-op on the semaphore associated with the resource.)
label_b: ; We no longer have the resource.
By the way, I don't think this is any easier on the Motorola 68000. As far
as I know, its only atomic instruction is tas. The 68020 brings in a compare
and swap. Did you have another idea for the 68000?
--- Steve Villee (ihnp4!terak!anasazi!steve)
International Anasazi, Inc.
7500 North Dreamy Draw Drive, Suite 120
Phoenix, Arizona 85020
(602) 870-3330
Technical Details
Message-ID:
<450@anasazi.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!gamma!epsilon!zeta!sabre!petrus!bellcore!decvax!genrad!panda!talcott!harvard!seismo!hao!noao!terak!mot!anasazi!steveReferences:
<1813@utcsri.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site anasazi.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!gamma!epsilon!zeta!sabre!petrus!bellcore!decvax!genrad!panda!talcott!harvard!seismo!hao!noao!terak!mot!anasazi!steve From: steve@anasazi.UUCP (Steve Villee) Newsgroups: net.micro.16k Subject: Re: Need word version of test-and-set Message-ID: <450@anasazi.UUCP> Date: Thu, 26-Dec-85 14:43:47 EST Article-I.D.: anasazi.450 Posted: Thu Dec 26 14:43:47 1985 Date-Received: Sat, 28-Dec-85 04:36:58 EST References: <1813@utcsri.UUCP> Organization: Anasazi, Phoenix Az. Lines: 57