9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Russ Cox" <rsc@plan9.bell-labs.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] Kernel question: i386 test-and-set problem
Date: Mon, 10 Jul 2000 08:40:20 -0400	[thread overview]
Message-ID: <200007101240.IAA27005@cse.psu.edu> (raw)

	The weird thing is that it looks as though this function only exchanged %eax
	and (%ebx) by XCHG instruction which is NOT atomic. The C source looks
	satisfied with it (I have no opportunity to try it, because I am not running

It looks atomic to me.  The Intel 386 instruction set
manual says

	XCHG exchanges two operands.  The operands can be in either
	order.  If a memory operand is involved, BUS LOCK is
	asserted for the duration of the exchange, regardless of the
	presence or absence of the LOCK prefix or of the value of
	the IOPL.

As for cas, ...

	By further searching, I found (in /sys/src/libthread/386cas.s ... or
	something) a routine called cas(). This routine uses the proper 386 atomic
	instruction CMPXCHG.

	Well, what's going on? I'd expect all those C functions to call cas()
	instead of tas(). Mys second question is why cas() is not the only tas()
	(test-end-set) within the 386 part of the kernel. My last question is why
	the cas() is outside the kernel tree.

The main problem with cas() is that it's not on the 386.
It is on the Pentium, and if memory serves, is on the 486.

Libthread is a user-level thread library and not part of
the kernel.  The lack of cas() for any othe architecture
suggests that it was written but never used, probably
for exactly the reason above.  I poked around in the
past and can't find any calls to it, ever.

You'll also note that 386xadd.s does not actually use
the XADD instruction, for the same reason.  Since we
only need increment and decrement, we use
INC and DEC instructions with a LOCK prefix, since
that will work on a 386 (which lacks XADD as well).

The choice of locking primitive is fairly arbitrary,
and tas got chosen a long time ago.  Since we don't
play funny games with lock-free data structures, the
absence of cas or cas2 is not felt.  You could have cas
instead, but it'd be clumsy to implement on a 386
(and maybe on the early Sparcs?).

Russ



             reply	other threads:[~2000-07-10 12:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-10 12:40 Russ Cox [this message]
2000-07-11  8:51 ` Jakub Jermar
  -- strict thread matches above, loose matches on Subject: below --
2000-08-03  9:56 miller
2000-08-02 16:24 presotto
2000-08-02 15:43 jmk
2000-08-02 14:51 miller
2000-08-02 13:20 presotto
2000-08-02  8:32 miller
2000-07-31 17:26 presotto
2000-07-23 14:41 miller
2000-07-21 13:15 presotto
2000-07-21  9:10 miller
2000-07-20 17:09 presotto
2000-07-20 13:54 miller
2000-07-20  2:03 jmk
2000-07-10 16:21 miller
2000-07-10  9:57 Jakub Jermar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200007101240.IAA27005@cse.psu.edu \
    --to=rsc@plan9.bell-labs.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).