| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2021 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | #include "support/atomic.h" | ||
| 21 | |||
| 22 | #include <stdbool.h> | ||
| 23 | #include <assert.h> | ||
| 24 | |||
| 25 | 1 | int main(int argc, char *argv[]) { | |
| 26 | |||
| 27 | 1 | atomic_int num = 0; | |
| 28 | |||
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( DLB_ATOMIC_ADD(&num, 1) == 0 ); // num = 1 |
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( DLB_ATOMIC_ADD_RLX(&num, 1) == 1 ); // num = 2 |
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( DLB_ATOMIC_ADD_FETCH(&num, 1) == 3 ); // num = 3 |
| 32 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( DLB_ATOMIC_ADD_FETCH_RLX(&num, 1) == 4 ); // num = 4 |
| 33 | |||
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( DLB_ATOMIC_SUB(&num, 1) == 4 ); // num = 3 |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( DLB_ATOMIC_SUB_RLX(&num, 1) == 3 ); // num = 2 |
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( DLB_ATOMIC_SUB_FETCH(&num, 1) == 1 ); // num = 1 |
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( DLB_ATOMIC_SUB_FETCH_RLX(&num, 1) == 0 ); // num = 0 |
| 38 | |||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | DLB_ATOMIC_ST(&num, 5); assert( num == 5); |
| 40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | DLB_ATOMIC_ST_RLX(&num, 6); assert( num == 6); |
| 41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | DLB_ATOMIC_ST_REL(&num, 7); assert( num == 7); |
| 42 | |||
| 43 | int val; | ||
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | val = DLB_ATOMIC_LD(&num); assert( num == val); |
| 45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | val = DLB_ATOMIC_LD_RLX(&num); assert( num == val); |
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | val = DLB_ATOMIC_LD_ACQ(&num); assert( num == val); |
| 47 | |||
| 48 | /* Test atomic bit functions */ | ||
| 49 | { | ||
| 50 | typedef enum MyFlags { | ||
| 51 | CLEAR = 0, | ||
| 52 | FLAG0 = 1 << 0, | ||
| 53 | FLAG1 = 1 << 1, | ||
| 54 | FLAG2 = 1 << 2, | ||
| 55 | FLAG3 = 1 << 3, | ||
| 56 | } flags_t; | ||
| 57 | bool b; | ||
| 58 | _Atomic(flags_t) flags; | ||
| 59 | 1 | atomic_int *_flags = (atomic_int*)&flags; | |
| 60 | |||
| 61 | /* set_bit */ | ||
| 62 | 1 | flags = FLAG0; | |
| 63 | 1 | b = set_bit(_flags, FLAG1); | |
| 64 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == true && flags == (FLAG0|FLAG1) ); |
| 65 | 1 | b = set_bit(_flags, FLAG1); | |
| 66 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == false && flags == (FLAG0|FLAG1) ); |
| 67 | |||
| 68 | /* clear_bit */ | ||
| 69 | 1 | flags = (flags_t)(FLAG0|FLAG1); | |
| 70 | 1 | b = clear_bit(_flags, FLAG1); | |
| 71 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == true && flags == FLAG0 ); |
| 72 | 1 | b = clear_bit(_flags, FLAG1); | |
| 73 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == false && flags == FLAG0 ); |
| 74 | |||
| 75 | /* cas_bit witFLAG1h a single bit */ | ||
| 76 | 1 | flags = FLAG0; | |
| 77 | 1 | b = cas_bit(_flags, FLAG0, FLAG1); | |
| 78 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == true && flags == FLAG1 ); |
| 79 | 1 | b = cas_bit(_flags, FLAG0, FLAG2); | |
| 80 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == false && flags == FLAG1 ); |
| 81 | |||
| 82 | /* cas_bit with n bits */ | ||
| 83 | 1 | flags = (flags_t)(FLAG0|FLAG1); | |
| 84 | 1 | b = cas_bit(_flags, FLAG1, FLAG2); | |
| 85 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == true && flags == (FLAG0|FLAG2) ); |
| 86 | 1 | b = cas_bit(_flags, FLAG1, FLAG3); | |
| 87 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == false && flags == (FLAG0|FLAG2) ); |
| 88 | 1 | b = cas_bit(_flags, (FLAG0|FLAG2), (FLAG1|FLAG3)); | |
| 89 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == true && flags == (FLAG1|FLAG3) ); |
| 90 | 1 | b = cas_bit(_flags, (FLAG0|FLAG2), (FLAG1|FLAG3)); | |
| 91 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == false && flags == (FLAG1|FLAG3) ); |
| 92 | |||
| 93 | /* test_set_clear_bit */ | ||
| 94 | 1 | flags = FLAG0; | |
| 95 | 1 | b = test_set_clear_bit(_flags, FLAG1, CLEAR); | |
| 96 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == true && flags == (FLAG0|FLAG1) ); |
| 97 | 1 | b = test_set_clear_bit(_flags, FLAG1, FLAG0); | |
| 98 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == false && flags == (FLAG0|FLAG1) ); |
| 99 | 1 | b = test_set_clear_bit(_flags, FLAG2, FLAG1); | |
| 100 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | assert( b == true && flags == (FLAG0|FLAG2) ); |
| 101 | } | ||
| 102 | |||
| 103 | 1 | return 0; | |
| 104 | } | ||
| 105 |