-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreads_memory.cpp
More file actions
94 lines (74 loc) · 2.17 KB
/
Copy paththreads_memory.cpp
File metadata and controls
94 lines (74 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <faasm/compare.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <vector>
#define N_THREADS 2
#define WASM_PAGE_SIZE 65536
#define DATA_SIZE 20
// Global array
bool successFlags[N_THREADS];
struct ThreadArgs
{
int threadIdx;
uint8_t* basePtr;
};
void* threadFunc(void* arg)
{
auto* threadArgs = (struct ThreadArgs*)arg;
int idx = threadArgs->threadIdx;
printf("Thread memory check %i\n", idx);
// Write a value to the global shared array
successFlags[idx] = true;
// Write to the mmapped shared memory
int offset = idx * WASM_PAGE_SIZE;
uint8_t* target = threadArgs->basePtr + offset;
std::vector<uint8_t> data(DATA_SIZE, idx);
::memcpy(target, data.data(), data.size());
return nullptr;
}
int main(int argc, char* argv[])
{
pthread_t threads[N_THREADS];
ThreadArgs inputs[N_THREADS];
// Shared memory
size_t memSize = 100 * WASM_PAGE_SIZE;
uint8_t* sharedMem = (uint8_t*)mmap(
nullptr, memSize, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
// Spawn the threads
for (int i = 0; i < N_THREADS; i++) {
// Initialise the global array
successFlags[i] = false;
inputs[i].threadIdx = i;
inputs[i].basePtr = sharedMem;
int ret = pthread_create(&threads[i], nullptr, threadFunc, &inputs[i]);
if (ret != 0) {
printf("Error creating thread (%i)\n", ret);
return 1;
}
}
// Join the threads
for (int i = 0; i < N_THREADS; i++) {
if (pthread_join(threads[i], nullptr)) {
printf("Error joining thread\n");
return 1;
}
}
// Check changes have been applied
for (int i = 0; i < N_THREADS; i++) {
if (!successFlags[i]) {
printf("Global success flag not set for %d\n", i);
return 1;
}
int offset = i * WASM_PAGE_SIZE;
uint8_t* target = sharedMem + offset;
std::vector<uint8_t> expected(DATA_SIZE, i);
if (!faasm::compareArrays<uint8_t>(
expected.data(), target, DATA_SIZE)) {
return 1;
}
}
return 0;
}