Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/core/pthread/emscripten_atomics.c
4154 views
1
#include <assert.h>
2
#include <emscripten/threading.h>
3
#include <stdio.h>
4
#include <stdint.h>
5
#include <string.h>
6
7
#define OLD 42
8
9
int main() {
10
uint8_t buffer[16];
11
memset(&buffer[0], OLD, sizeof(buffer));
12
13
uint8_t o8 = emscripten_atomic_exchange_u8(&buffer[0], 0x12);
14
assert(o8 == OLD);
15
assert(buffer[0] == 0x12);
16
assert(buffer[1] == OLD);
17
18
uint16_t o16 = emscripten_atomic_exchange_u16(&buffer[0], 0x3456);
19
assert((o16 & 0xff) == 0x12);
20
assert((o16 >> 8) == OLD);
21
assert(buffer[0] == 0x56);
22
assert(buffer[1] == 0x34);
23
assert(buffer[2] == OLD);
24
25
uint32_t o32 = emscripten_atomic_exchange_u32(&buffer[0], 0xabcdef91);
26
assert((o32 & 0xff) == 0x56);
27
assert(((o32 >> 8) & 0xff) == 0x34);
28
assert(((o32 >> 16) & 0xff) == OLD);
29
assert(((o32 >> 24) & 0xff) == OLD);
30
assert(buffer[0] == 0x91);
31
assert(buffer[1] == 0xef);
32
assert(buffer[2] == 0xcd);
33
assert(buffer[3] == 0xab);
34
assert(buffer[4] == OLD);
35
36
printf("OK\n");
37
}
38
39
40