public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: linux-kernel@vger.kernel.org
Subject: Re: Atomic operations
Date: 3 Jun 2002 10:27:55 -0700	[thread overview]
Message-ID: <adg8ur$13g$1@cesium.transmeta.com> (raw)
In-Reply-To: <EE83E551E08D1D43AD52D50B9F5110927E7A10@ntserver2>

Followup to:  <EE83E551E08D1D43AD52D50B9F5110927E7A10@ntserver2>
By author:    Gregory Giguashvili <Gregoryg@ParadigmGeo.com>
In newsgroup: linux.dev.kernel
>
> Hello,
> 
> I wonder if someone can help me to change the behaviour of the atomic
> functions available in <asm/atomic.h> include file. The operations I need to
> implement are described below:
> 
> atomic_t test_and_set (int i, atomic_t* v)
> {
>    atomic_t old = *v;
>    v->counter = i;
>    return old;
> }
> 

This is not a test and set operation.

On i386:

atomic_t atomic_exchange (atomic_t i, atomic_t *v)
{
   asm volatile("xchgl %0,%1" : "+m" (*v), "+r" (i));
   return i;
}

> atomic_t test_then_add (int i, atomic_t* v)
> {
>    atomic_t old = *v;
>    v->counter += i;
>    return old;
> }

There is no way to do this (without waiting and trying again type
code) that I know of on i386.  However, you can test for zeroness of
the result, or for <= 0, or a few other options.

int test_and_add (atomic_t i, atomic_t *v)
{
  char was_nonzero;	/* MUST BE CHAR!!! */

  asm volatile("lock; addl %2,%0; setz %1"
    : "+m" (*v), "=rm" (was_nonzero)
    : "g" (i));

  return was_nonzero;
}

  

-- 
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt	<amsp@zytor.com>

  reply	other threads:[~2002-06-03 17:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-03 15:04 Atomic operations Gregory Giguashvili
2002-06-03 17:27 ` H. Peter Anvin [this message]
2002-06-03 18:08 ` Richard B. Johnson
2002-06-03 19:36   ` Thunder from the hill
2002-06-03 21:30     ` Richard B. Johnson
2002-06-03 18:39 ` Brian Gerst
2002-06-03 19:49   ` H. Peter Anvin
  -- strict thread matches above, loose matches on Subject: below --
2002-06-03 15:58 Gregory Giguashvili
2002-06-03 19:09 Gregory Giguashvili
2002-06-03 18:43 ` H. Peter Anvin
2002-06-04  9:23 Gregory Giguashvili

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='adg8ur$13g$1@cesium.transmeta.com' \
    --to=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    /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