C11 and C++11 Atomics Support

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
GyroVorbis
Elysian Shadows Developer
Elysian Shadows Developer
Posts: 1874
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Mon Mar 22, 2004 4:55 pm
Location: #%^&*!!!11one Super Sonic
Has thanked: 80 times
Been thanked: 62 times
Contact:

C11 and C++11 Atomics Support

Post by GyroVorbis »

I come bearing new language-level concurrency gifts... This time for C11 and C++11's atomics.

This allows you to create thread-safe variables which can be loaded, stored, fetched, incremented, decremented, and generally operated on in a thread-safe manner. The primitive type atomics (64-bit and smaller) are implemented by simply disabling interrupts then reenabling them around the "atomic" operation on the given variable, so they are going to be fast and do not require an external mutex or synchronization primitive.

We also support atomic operations on arbitrarily sized buffers and structures. These are going to be locking operations, but they do so using a hash table of spinlocks which are implicitly being mapped to different regions of memory, so you still don't actually have to manually store or handle locking primitives for these.

A great big example that also doubles as a toolchain/KOS back-end test can be found here: https://github.com/KallistiOS/KallistiO ... /atomics.c

Lets note some of the highlights... You can declare atomic variables like so:

Code: Select all

/* Generic buffer structure we will use with atomics. */
typedef struct {
   uint8_t values[BUFFER_SIZE]; 
} Buffer;

/* Atomic data our threads will be competing over accessing. */
static atomic_flag      flag_atomic     = ATOMIC_FLAG_INIT;
static atomic_bool      bool_atomic     = false;
static atomic_int       int_atomic      = INT_MAX;
static _Atomic uint64_t longlong_atomic = 0;
static _Atomic(uint8_t) byte_atomic     = 0;
static atomic_short     short_atomic    = 0;
static atomic_ptrdiff_t ptrdiff_atomic  = 0;
static _Atomic(Buffer)  buffer_atomic   = { {0} };
Then you can perform "atomic" operations on these types, as shown here: https://en.cppreference.com/w/c/atomic

NOTE: Please note that while the example is for C11, I exhaustively tested the same thing for C++11's std::atomic<> templates and found them all to be working as well. I spent a lot of time with modern C++20 concurency in general, and everything is in working order it seems:

https://twitter.com/falco_girgis/status ... 4117050551

Anyway, happy coedzing, and may your concurrency be lockless!
These users thanked the author GyroVorbis for the post (total 2):
Ian Robinson|darc|
User avatar
Ian Robinson
DC Developer
DC Developer
Posts: 116
Joined: Mon Mar 11, 2019 7:12 am
Has thanked: 209 times
Been thanked: 41 times

Re: C11 and C++11 Atomics Support

Post by Ian Robinson »

Thank you that's a lot of work we need to promote it :)
These users thanked the author Ian Robinson for the post:
GyroVorbis
User avatar
Ian Robinson
DC Developer
DC Developer
Posts: 116
Joined: Mon Mar 11, 2019 7:12 am
Has thanked: 209 times
Been thanked: 41 times

Re: C11 and C++11 Atomics Support

Post by Ian Robinson »

Bump come on dc dev's lol we got new fancy stuff lol
These users thanked the author Ian Robinson for the post (total 2):
|darc|GyroVorbis
User avatar
lerabot
Insane DCEmu
Insane DCEmu
Posts: 134
Joined: Sun Nov 01, 2015 8:25 pm
Has thanked: 2 times
Been thanked: 19 times

Re: C11 and C++11 Atomics Support

Post by lerabot »

This is just too fancy for me :P But Good job Falco for bringing this to the DC <3
These users thanked the author lerabot for the post:
GyroVorbis
Post Reply