Re: atomic_t

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

On Jan 3, 2008 4:45 PM, Manish Katiyar <mkatiyar@xxxxxxxxx> wrote:
> As everyone mentioned, it is to upgrade the data structure, without
> affecting the existing code. It also prevents coders who liberally use
> int with other datatypes like long etc. from doing so and thus also
> prevents some unnoticed errors.
>
> Hope that helps.........
>
> One of the reasons
>
>
> On Jan 3, 2008 7:59 PM, Thanos McAtos <mcatos@xxxxxxxxxxxx> wrote:
> > Because that way the data type is more generic. If, in the future, it is
> > decided to be changed from int to something else, you won't have to change
> > anything in your code. This is a foundamental concept in ADTs and
> > generally a good way of programming.
> >
> >
> > > I wonder why atomic_t is defined as a structure containing an integer
> > > rather than just a simple integer?
> > > What are the advantages?
> > >
> > > Hope somebody can explain this, please cc me.
> > >

I would like to add to the other replies the following:
even if the atomic_t type would be int on every architecture, without
special requirements (24 bit ...), and this would be guaranteed not to
change in the future, even then there is a point in using atomic_t
instead of int.

The point is that atomic_t is a so-called opaque type, with the
purpose of hiding the actual type (int) to the user. The only
operations you are allowed to do on atomic_t variables are through the
atomic interface, for example atomic_inc(). You should never do
integer operations on variables of type atomic_t, like
atomic_t my_atom;
my_atom = 3;
my_atom++;

because you would loose the atomicity.If you would know that atomic_t
is actually an int, you could be tempted to do it this way.

By making atomic_t a single-field structure, this type of programming
is even made impossible, and trying to to my_atom++ will be caught by
the compiler.

Best regards,
Thomas

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux