Gsoc2012: Atomic operation for RTEMS

yangwei weiyang wei.a.yang at gmail.com
Tue May 15 16:07:43 UTC 2012


Hi all,

My proposal for Gsoc2012 is "atomic operation for RTEMS". Now i am
doing the first step of designing the atomic API for RTEMS. There are
some views need to discuss. So i send it to this list.

In my proposal[1] the goal of this project is to implement the atomic
operation for RTEMS which should be compatible with ISO C1X
definition[2] as far as possible. So after reading the C1X definition
i make a match for the atomic API definition between the C1X and
FreeBSD[3], NetBSD[4].

According to the C1X atomic API definition i compare the atomic API
definition of FreeBSD kernel which has the same or similar semantic.
The functions not ending in _explicit have the semantics of their
corresponding _explicit with memory_order arguments of
memory_order_seq_cst, So i just list the API definition ending in
_explicit.

1. The atomic_store generic functions
   C1X:
          void atomic_store_explicit(volatile A *object, C desired,
memory_order order);
   FreeBSD:
          void atomic_store_rel_<type>(volatile _type_ *p, _type_ v);

   FreeBSD API has the same semantic of C1X's API with memory_order
arguments of memory_order_release.

2. The atomic_load generic functions
    C1X:
           C atomic_load_explicit(volatile A *object, memory_order order);
    FreeBSD:
           _type_ atomic_load_acq_<type>(volatile _type_ *p);

    FreeBSD API has the same semantic of C1X's API with memory_order
arguments of memory_order_acquire.

3. The atomic_fetch-and-modify generic functions
   C1X:
          (1)C atomic_fetch_add_explicit(volatile A *object, M
operand, memory_order order);
          (2)C atomic_fetch_sub_explicit(volatile A *object, M
operand, memory_order order);
          (3)C atomic_fetch_or_explicit(volatile A *object, M operand,
memory_order order);
          (4)C atomic_fetch_xor_explicit(volatile A *object, M
operand, memory_order order);
          (5)C atomic_fetch_and_explicit(volatile A *object, M
operand, memory_order order);
   FreeBSD:
          (1)void atomic_add_[acq_|rel_]<type>(volatile _type_ *p, _type_ v);
          (2)void atomic_subtract_[acq_|rel_]<type>(volatile _type_
*p, _type_ v);
          (3)void atomic_set_[acq_|rel_]<type>(volatile _type_ *p, _type_ v);
          (4)void atomic_clear_[acq_|rel_]<type>(volatile _type_ *p, _type_ v);

    The FreeBSD API (1),(2),(3) have the same semantic of C1X's API
(1),(2),(3) with memory_order arguments of memory_order_acquire or
memory_order_release([acq_|rel_]). The FreeBSD API does not have the
same semantic with C1X's API(4), but the FreeBSD API(4) is similar
with C1X's API(5) where one uses &= the other uses &=~. So this part
is easy to adapt. But all the FreeBSD APIs do not return value.

4. The atomic_exchange generic functions
   C1X:
          C atomic_exchange_explicit(volatile A *object, C desired,
memory_order order);
   FreeBSD:
          XXX
   NetBSD:
          _type_ atomic_swap_<type>(volatile _type_ *ptr, _type_ new);

  The FreeBSD API does not support the atomic exchagne functions, but
NetBSD has the similar semantic of C1X's API only with memory_order
arguments of memory_order_relaxed.

5. The atomic_compare_exchange generic functions
    C1X:
           _Bool atomic_compare_exchange_weak_explicit(volatile A
*object, C *expected, C desired, memory_order success, memory_order
failure);
           _Bool atomic_compare_exchange_strong_explicit(volatile A
*object, C *expected, C desired, memory_order success, memory_order
failure);
     FreeBSD:
           int atomic_cmpset_[acq_|rel_]<type>(volatile _type_ *dst,
_type_ old, _type_ new);

     In the FreeBSD Atomically compare the value stored at *dst with
old and if the two values are equal, update the value of *dst with
new. Returns zero if the compare failed, nonzero otherwise. In the C1X
Atomically compares the value pointed to by object for equality with
that in expected, and if true, replaces the value pointed to by object
with desired, and if false, updates the value in expected with the
value pointed to by object.

     Summary:
                    From the above analysis i think we can design the
atomic API based on FreeBSD and make some modifies to be compatible
with C1X definition. Also we can refer the NetBSD atomic exchange API
definition to implement this function.
     Any comments are welcome.

PS: until now i do not consider the name style of atomic API, just
foucs on the atomic operations feature API design.

References:
[1]. https://docs.google.com/document/d/11O3i5dWlH4BmwY9SAycvARf40omeLJ-aD0Eg6E4fuh0/edit#
[2]. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
[3]. http://www.freebsd.org/cgi/man.cgi?query=atomic&sektion=9
[4]. http://www.daemon-systems.org/man/atomic_ops.3.html

-- 
Wei Yang
Best Regards

wei.a.yang at gmail.com



More information about the devel mailing list