Home | History | Annotate | Download | only in atomics.flag
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <atomic>
     11 
     12 // struct atomic_flag
     13 
     14 // bool test_and_set(memory_order = memory_order_seq_cst);
     15 // bool test_and_set(memory_order = memory_order_seq_cst) volatile;
     16 
     17 #include <atomic>
     18 #include <cassert>
     19 
     20 int main()
     21 {
     22     {
     23         std::atomic_flag f;
     24         f.clear();
     25         assert(f.test_and_set() == 0);
     26         assert(f.test_and_set() == 1);
     27     }
     28     {
     29         std::atomic_flag f;
     30         f.clear();
     31         assert(f.test_and_set(std::memory_order_relaxed) == 0);
     32         assert(f.test_and_set(std::memory_order_relaxed) == 1);
     33     }
     34     {
     35         std::atomic_flag f;
     36         f.clear();
     37         assert(f.test_and_set(std::memory_order_consume) == 0);
     38         assert(f.test_and_set(std::memory_order_consume) == 1);
     39     }
     40     {
     41         std::atomic_flag f;
     42         f.clear();
     43         assert(f.test_and_set(std::memory_order_acquire) == 0);
     44         assert(f.test_and_set(std::memory_order_acquire) == 1);
     45     }
     46     {
     47         std::atomic_flag f;
     48         f.clear();
     49         assert(f.test_and_set(std::memory_order_release) == 0);
     50         assert(f.test_and_set(std::memory_order_release) == 1);
     51     }
     52     {
     53         std::atomic_flag f;
     54         f.clear();
     55         assert(f.test_and_set(std::memory_order_acq_rel) == 0);
     56         assert(f.test_and_set(std::memory_order_acq_rel) == 1);
     57     }
     58     {
     59         std::atomic_flag f;
     60         f.clear();
     61         assert(f.test_and_set(std::memory_order_seq_cst) == 0);
     62         assert(f.test_and_set(std::memory_order_seq_cst) == 1);
     63     }
     64     {
     65         volatile std::atomic_flag f;
     66         f.clear();
     67         assert(f.test_and_set() == 0);
     68         assert(f.test_and_set() == 1);
     69     }
     70     {
     71         volatile std::atomic_flag f;
     72         f.clear();
     73         assert(f.test_and_set(std::memory_order_relaxed) == 0);
     74         assert(f.test_and_set(std::memory_order_relaxed) == 1);
     75     }
     76     {
     77         volatile std::atomic_flag f;
     78         f.clear();
     79         assert(f.test_and_set(std::memory_order_consume) == 0);
     80         assert(f.test_and_set(std::memory_order_consume) == 1);
     81     }
     82     {
     83         volatile std::atomic_flag f;
     84         f.clear();
     85         assert(f.test_and_set(std::memory_order_acquire) == 0);
     86         assert(f.test_and_set(std::memory_order_acquire) == 1);
     87     }
     88     {
     89         volatile std::atomic_flag f;
     90         f.clear();
     91         assert(f.test_and_set(std::memory_order_release) == 0);
     92         assert(f.test_and_set(std::memory_order_release) == 1);
     93     }
     94     {
     95         volatile std::atomic_flag f;
     96         f.clear();
     97         assert(f.test_and_set(std::memory_order_acq_rel) == 0);
     98         assert(f.test_and_set(std::memory_order_acq_rel) == 1);
     99     }
    100     {
    101         volatile std::atomic_flag f;
    102         f.clear();
    103         assert(f.test_and_set(std::memory_order_seq_cst) == 0);
    104         assert(f.test_and_set(std::memory_order_seq_cst) == 1);
    105     }
    106 }
    107