Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does sig_atomic_t actually work?

Tags:

c

linux

signals

How does the compiler or OS distinguish between sig_atomic_t type and a normal int type variable, and ensures that the operation will be atomic? Programs using both have same assembler code. How extra care is taken to make the operation atomic?

like image 804
Chu Avatar asked Jul 24 '14 10:07

Chu


People also ask

Is Sig_atomic_t thread safe?

Note that sig_atomic_t is not thread-safe, only async-signal safe. Atomics involve two types of barriers: Compiler barrier. It makes sure that the compiler does not reorder reads/writes from/to an atomic variable relative to reads and writes to other variables.

What is a Sig_atomic_t?

The type sig_atomic_t is the integer type of an object that can be accessed as an atomic entity even in the presence of asynchronous interrupts.


2 Answers

sig_atomic_t is not an atomic data type. It is just the data type that you are allowed to use in the context of a signal handler, that is all. So better read the name as "atomic relative to signal handling".

To guarantee communication with and from a signal handler, only one of the properties of atomic data types is needed, namely the fact that read and update will always see a consistent value. Other data types (such as perhaps long long) could be written with several assembler instructions for the lower and higher part, e.g. sig_atomic_t is guaranteed to be read and written in one go.

So a platform may choose any integer base type as sig_atomic_t for which it can make the guarantee that volatile sig_atomic_t can be safely used in signal handlers. Many platforms chose int for this, because they know that for them int is written with a single instruction.

The latest C standard, C11, has atomic types, but which are a completely different thing. Some of them (those that are "lockfree") may also be used in signal handlers, but that again is a completely different story.

like image 157
Jens Gustedt Avatar answered Oct 21 '22 03:10

Jens Gustedt


Note that sig_atomic_t is not thread-safe, only async-signal safe.

Atomics involve two types of barriers:

  1. Compiler barrier. It makes sure that the compiler does not reorder reads/writes from/to an atomic variable relative to reads and writes to other variables. This is what volatile keyword does.
  2. CPU barrier and visibility. It makes sure that the CPU does not reorder reads and writes. On x86 all loads and stores to aligned 1,2,4,8-byte storage are atomic. Visibility makes sure that stores become visible to other threads. Again, on Intel CPUs, stores are visible immediately to other threads due to cache coherence and memory coherence protocol MESI. But that may change in the future. See §8.1 LOCKED ATOMIC OPERATIONS in Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3A for more details.

For comprehensive treatment of the subject watch atomic Weapons: The C++ Memory Model and Modern Hardware.

like image 38
Maxim Egorushkin Avatar answered Oct 21 '22 04:10

Maxim Egorushkin