1

Recently when I was reading about lock-less programming, I came across 'Atomic operations'. I started digging deep into it. All links explain how to write atomic operations and their usages.

However, I am looking for some details on atomic operations.

  1. Do atomic operations need any hardware capabilities?
  2. Do languages provide APIs for it? if yes, how are atomic APIs implemented?
  3. Are these limited only to kernel space programming, or are they available for user-space programming too?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3691131
  • 31
  • 1
  • 4
  • 1
    If I'm not mistaken, the short answer is proper locking, so #1 is a no: https://en.m.wikipedia.org/wiki/Dekker%27s_algorithm, https://en.m.wikipedia.org/wiki/Peterson%27s_algorithm. Hardware support makes everything easier if course – Mad Physicist Nov 16 '18 at 06:11
  • 1
    Yes. On x86 and x64 this kind of "lockless" code is usually implemented with the LOCK instruction prefix :) Not an api, the compiler's back-end knows about the intrinsics. – Hans Passant Nov 16 '18 at 08:41

2 Answers2

2

Do atomic operations need any hardware capabilities?

Sure, CPUs guarantee that some of their instructions are atomic. Some of those instructions are "special", i.e. differ from other instructions (prefixed, or have other mnemonics), but some instructions might be "normal". For example, aligned stores and loads are guaranteed to be atomic on most CPUs.

Do languages provide APIs for it? if yes, how are atomic APIs implemented?

Sure, have a look for example at C++ implementation: https://en.cppreference.com/w/cpp/atomic/atomic

Are these limited only to kernel space programming, or are they available for user-space programming too?

Sure, those instructions do not require any privileges, so they are available for userspace. There is a variety of libraries and data structures which leverage atomic operations.

The keywords for the search are "lockless" or "non-blocking". Here is an example: https://en.wikipedia.org/wiki/Non-blocking_linked_list

Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33
-1

Does atomic operation need any hardware capabilities.

In practice, yes. In principle, the C++ (read n3337) or C (read n1570) standards don't even require a computer like the one we are using (you could, in theory and unethically, use a bunch of human slaves instead; a nicer variant of that is a teacher using the students of his classroom to "run" a tiny C or C++ program; this is a very good way of teaching programming).

See also this and that answers of mine (to questions similar to yours).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    Interesting how that was the first alternative to a computer that came to your mind. But on second thought, I couldn't find anything better that isn't essentially derivative :) – Mad Physicist Nov 16 '18 at 06:14
  • 1
    @user4581301 ...trained to meticulously execute machine instructions, sure. I feel like you'd need underpaid humans to teach the monkeys though :) – Mad Physicist Nov 16 '18 at 06:26
  • 2
    But if the monkeys can only carry one byte at a time, they might not be able to move a word atomically. You will need a mechanism to ensure that no monkey carries in a new ls byte, while another monkey is busy moving the ms byte. For example a mechanism the gives the approaching monkey a banana if it patiently waits for the first monkey to carry away both bytes, before dropping a new one. Btw, are monkeys big or little endian? – Lundin Nov 16 '18 at 09:35