aboutsummaryrefslogtreecommitdiff
path: root/03_threads
diff options
context:
space:
mode:
Diffstat (limited to '03_threads')
-rwxr-xr-x03_threads/build.sh6
-rw-r--r--03_threads/index.html19
-rw-r--r--03_threads/mime.txt1
-rw-r--r--03_threads/primes.c53
-rw-r--r--03_threads/program.mjs6
-rwxr-xr-x03_threads/run-node.sh3
-rwxr-xr-x03_threads/run-server.sh6
-rw-r--r--03_threads/script.mjs16
8 files changed, 110 insertions, 0 deletions
diff --git a/03_threads/build.sh b/03_threads/build.sh
new file mode 100755
index 0000000..2f08909
--- /dev/null
+++ b/03_threads/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/03_threads/index.html b/03_threads/index.html
new file mode 100644
index 0000000..29cfaf8
--- /dev/null
+++ b/03_threads/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>Multiply two numbers</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/03_threads/mime.txt b/03_threads/mime.txt
new file mode 100644
index 0000000..6a9a425
--- /dev/null
+++ b/03_threads/mime.txt
@@ -0,0 +1 @@
text/javascript mjs
diff --git a/03_threads/primes.c b/03_threads/primes.c
new file mode 100644
index 0000000..a5076c6
--- /dev/null
+++ b/03_threads/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/03_threads/program.mjs b/03_threads/program.mjs
new file mode 100644
index 0000000..a5a89ce
--- /dev/null
+++ b/03_threads/program.mjs
@@ -0,0 +1,6 @@
1import Primes from "./build/primes.mjs"
2
3var primes = await Primes();
4
5const count = primes._primes_in_range(1, 100);
6console.log("There are " + count + " primes betwees 1 and 100");
diff --git a/03_threads/run-node.sh b/03_threads/run-node.sh
new file mode 100755
index 0000000..a6c9aa8
--- /dev/null
+++ b/03_threads/run-node.sh
@@ -0,0 +1,3 @@
1#!/bin/sh
2
3node program.mjs
diff --git a/03_threads/run-server.sh b/03_threads/run-server.sh
new file mode 100755
index 0000000..3a526f3
--- /dev/null
+++ b/03_threads/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/03_threads/script.mjs b/03_threads/script.mjs
new file mode 100644
index 0000000..4b52ae0
--- /dev/null
+++ b/03_threads/script.mjs
@@ -0,0 +1,16 @@
1import Primes from "./build/primes.mjs";
2
3var primes = await Primes();
4
5var aInput = document.getElementById("aInput");
6var bInput = document.getElementById("bInput");
7var button = document.getElementById("goButton");
8var resultText = document.getElementById("resultText");
9
10button.addEventListener("click", () => {
11 var a = Number(aInput.value);
12 var b = Number(bInput.value);
13 const count = primes._primes_in_range(a, b);
14 resultText.innerText = "There are " + count + " primes between " +
15 a + " and " + b;
16});

Generated with cgit - Back to sebastiano.tronto.net