aboutsummaryrefslogtreecommitdiff
path: root/04_no_block
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-06-05 10:13:28 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-06-05 10:13:28 +0200
commit73efecca42dbc44200e797e02f8c1e24fcff26ef (patch)
tree46969fed9d5c09a4164257dace56a71cad9092ab /04_no_block
parent2a8e7aeeef99161b6f0378ac14e297c23ca6227b (diff)
downloademscripten-tutorial-73efecca42dbc44200e797e02f8c1e24fcff26ef.tar.gz
emscripten-tutorial-73efecca42dbc44200e797e02f8c1e24fcff26ef.zip
Added two more examples
Diffstat (limited to '04_no_block')
-rwxr-xr-x04_no_block/build.sh6
-rw-r--r--04_no_block/index.html19
-rw-r--r--04_no_block/mime.txt1
-rw-r--r--04_no_block/primes.c53
-rwxr-xr-x04_no_block/run-server.sh6
-rw-r--r--04_no_block/script.mjs14
-rw-r--r--04_no_block/worker.mjs8
7 files changed, 107 insertions, 0 deletions
diff --git a/04_no_block/build.sh b/04_no_block/build.sh
new file mode 100755
index 0000000..2f08909
--- /dev/null
+++ b/04_no_block/build.sh
@@ -0,0 +1,6 @@
1#!/bin/sh
2
3mkdir -p build
4emcc -sEXPORTED_FUNCTIONS=_primes_in_range -sMODULARIZE -sEXPORT_NAME=Primes \
5 -pthread -sPTHREAD_POOL_SIZE=16 \
6 -o build/primes.mjs primes.c
diff --git a/04_no_block/index.html b/04_no_block/index.html
new file mode 100644
index 0000000..7a1fba7
--- /dev/null
+++ b/04_no_block/index.html
@@ -0,0 +1,19 @@
1<!doctype html>
2<html lang="en-US">
3<head>
4 <meta charset="utf-8" />
5 <meta name="viewport" content="width=device-width" />
6 <title>Primes in a range</title>
7 <script src="./script.mjs" type="module" defer></script>
8</head>
9
10<body>
11 <label for="aInput">Lower bound (included)</label><br />
12 <input id="aInput" name="aInput" /> x <br />
13 <label for="bInput">Upper bound (excluded)</label><br />
14 <input id="bInput" name="bInput" /> <br />
15 <button id="goButton">Compute</button> <br />
16 <p id="resultText"></p>
17</body>
18
19</html>
diff --git a/04_no_block/mime.txt b/04_no_block/mime.txt
new file mode 100644
index 0000000..6a9a425
--- /dev/null
+++ b/04_no_block/mime.txt
@@ -0,0 +1 @@
text/javascript mjs
diff --git a/04_no_block/primes.c b/04_no_block/primes.c
new file mode 100644
index 0000000..a5076c6
--- /dev/null
+++ b/04_no_block/primes.c
@@ -0,0 +1,53 @@
1#include <stdbool.h>
2#include <pthread.h>
3
4#define NTHREADS 16
5
6bool isprime(int);
7void *pthread_routine(void *);
8
9struct interval { int low; int high; int count; };
10
11int primes_in_range(int low, int high) {
12 pthread_t threads[NTHREADS];
13 struct interval args[NTHREADS];
14
15 if (low < 0 || high < low)
16 return 0;
17
18 int interval_size = (high-low)/NTHREADS + 1;
19 for (int i = 0; i < NTHREADS; i++) {
20 args[i].low = low + i*interval_size;
21 args[i].high = args[i].low + interval_size;
22 pthread_create(&threads[i], NULL, pthread_routine, &args[i]);
23 }
24
25 int result = 0;
26 for (int i = 0; i < NTHREADS; i++) {
27 pthread_join(threads[i], NULL);
28 result += args[i].count;
29 }
30
31 return result;
32}
33
34bool isprime(int n) {
35 if (n < 2)
36 return false;
37
38 for (int i = 2; i*i <= n; i++)
39 if (n % i == 0)
40 return false;
41 return true;
42}
43
44void *pthread_routine(void *arg) {
45 struct interval *interval = arg;
46
47 interval->count = 0;
48 for (int i = interval->low; i < interval->high; i++)
49 if (isprime(i))
50 interval->count++;
51
52 return NULL;
53}
diff --git a/04_no_block/run-server.sh b/04_no_block/run-server.sh
new file mode 100755
index 0000000..3a526f3
--- /dev/null
+++ b/04_no_block/run-server.sh
@@ -0,0 +1,6 @@
1#!/bin/sh
2
3darkhttpd . \
4 --mimetypes mime.txt \
5 --header 'Cross-Origin-Opener-Policy: same-origin' \
6 --header 'Cross-Origin-Embedder-Policy: require-corp'
diff --git a/04_no_block/script.mjs b/04_no_block/script.mjs
new file mode 100644
index 0000000..b9c27ea
--- /dev/null
+++ b/04_no_block/script.mjs
@@ -0,0 +1,14 @@
1var aInput = document.getElementById("aInput");
2var bInput = document.getElementById("bInput");
3var button = document.getElementById("goButton");
4var resultText = document.getElementById("resultText");
5
6var worker = new Worker("./worker.mjs", { type: "module" });
7
8button.addEventListener("click", () => worker.postMessage({
9 a: Number(aInput.value),
10 b: Number(bInput.value)
11}));
12
13worker.onmessage = (e) => resultText.innerText = "There are " +
14 e.data.result + " primes between " + e.data.a + " and " + e.data.b;
diff --git a/04_no_block/worker.mjs b/04_no_block/worker.mjs
new file mode 100644
index 0000000..6476203
--- /dev/null
+++ b/04_no_block/worker.mjs
@@ -0,0 +1,8 @@
1import Primes from "./build/primes.mjs";
2
3var primes = await Primes();
4
5onmessage = (e) => {
6 const count = primes._primes_in_range(e.data.a, e.data.b);
7 postMessage({ result: count, a: e.data.a, b: e.data.b });
8};

Generated with cgit - Back to sebastiano.tronto.net