Search code examples
cmultithreadinginterruptinterrupt-handling

How to implement TAS ("test and set")?


Did anyone know the implementation of TAS?

This code should later be able to protect the code between acquiring and release from multithread based loss of states.

typedef volatile long lock_t;

void acquire(lock_t* lock){
    while(TAS(lock))
        ;
}

void release(lock_t* lock){
    *lock = 0;
}

lock_t REQ_lock = 0;

int main(){
    acquire(&REQ_lock);
    //not Atomar Code
    release(&REQ_lock);
}

Solution

  • The C11 standard brings atomic types into the language.

    Among them is the atomic_flag type, which has an associated function atomic_flag_test_and_set Here it is from the standard:

    C11 working draft section 7.17.8.1:

    Synopsis

    #include <stdatomic.h>
    _Bool atomic_flag_test_and_set(
        volatile atomic_flag *object);
    _Bool atomic_flag_test_and_set_explicit(
        volatile atomic_flag *object,memory_order order);
    

    Description

    Atomically sets the value pointed to by object to true. Memory is affected according to the value of order . These operations are atomic read-modify-write operations

    Returns

    Atomically, the value of the object immediately before the effects.

    And along with it, you'll want its sister operation, atomic_flag_clear

    Section 7.17.8.2:

    Synopsis

    #include <stdatomic.h>
    void atomic_flag_clear(volatile atomic_flag *object);
    void atomic_flag_clear_explicit(
        volatile atomic_flag *object, memory_order order);
    

    Description

    The order argument shall not be memory_order_acquire nor memory_order_acq_rel. Atomically sets the value pointed to by object to false. Memory is affected according to the value of order.

    Returns

    The atomic_flag_clear functions return no value