PatchworkOS
10941b4
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
#include <
stdatomic.h
>
2
#include <
stdbool.h
>
3
#include <
stdint.h
>
4
#include <
stdio.h
>
5
#include <
sys/defs.h
>
6
#include <
threads.h
>
7
#include <
time.h
>
8
9
#define PRIME_MAX (10000000)
10
11
static
atomic_long
count
;
12
static
atomic_long
next
;
13
14
bool
is_prime
(
uint64_t
n)
15
{
16
if
(n <= 1)
17
{
18
return
false
;
19
}
20
if
(n <= 3)
21
{
22
return
true
;
23
}
24
if
(n % 2 == 0 || n % 3 == 0)
25
{
26
return
false
;
27
}
28
29
for
(
uint64_t
i
= 5;
i
*
i
<= n;
i
+= 6)
30
{
31
if
(n %
i
== 0 || n % (
i
+ 2) == 0)
32
{
33
return
false
;
34
}
35
}
36
37
return
true
;
38
}
39
40
static
void
count_primes
(
uint64_t
start
,
uint64_t
end)
41
{
42
for
(
uint64_t
i
=
start
;
i
< end;
i
++)
43
{
44
if
(
is_prime
(
i
))
45
{
46
atomic_fetch_add
(&
count
, 1);
47
}
48
}
49
}
50
51
static
int
thread_entry
(
void
* arg)
52
{
53
UNUSED
(arg);
54
55
uint64_t
start
;
56
while
((
start
=
atomic_fetch_add
(&
next
, 1000)) <
PRIME_MAX
)
57
{
58
uint64_t
end =
start
+ 1000;
59
if
(end >
PRIME_MAX
)
60
{
61
end =
PRIME_MAX
;
62
}
63
64
count_primes
(
start
, end);
65
}
66
return
thrd_success
;
67
}
68
69
static
void
benchmark
(
uint64_t
threadAmount
)
70
{
71
printf
(
"%d threads: starting..."
,
threadAmount
);
72
fflush
(
stdout
);
73
clock_t
start
=
clock
();
74
75
atomic_init
(&
count
, 0);
76
atomic_init
(&
next
, 0);
77
78
thrd_t
threads[
threadAmount
];
79
for
(
uint64_t
i
= 0;
i
<
threadAmount
;
i
++)
80
{
81
if
(
thrd_create
(&threads[
i
],
thread_entry
,
NULL
) !=
thrd_success
)
82
{
83
printf
(
" (thrd_create error %d) "
,
i
);
84
fflush
(
stdout
);
85
}
86
}
87
88
for
(
uint64_t
i
= 0;
i
<
threadAmount
;
i
++)
89
{
90
if
(
thrd_join
(threads[
i
],
NULL
) !=
thrd_success
)
91
{
92
printf
(
" (thrd_join error %d) "
,
i
);
93
fflush
(
stdout
);
94
}
95
}
96
97
clock_t
end =
clock
();
98
printf
(
"\ttook %d ms to find %d primes\n"
, (end -
start
) / (
CLOCKS_PER_SEC
/ 1000),
atomic_load
(&
count
));
99
}
100
101
int
main
(
void
)
102
{
103
for
(
uint64_t
threads = 1; threads <= 1024; threads *= 2)
104
{
105
benchmark
(threads);
106
}
107
108
printf
(
"Testing complete.\n"
);
109
return
0;
110
}
main
int main(void)
Definition
main.c:5
y
int64_t y
Definition
main.c:153
start
static void start()
Definition
main.c:542
CLOCKS_PER_SEC
#define CLOCKS_PER_SEC
Definition
clock_t.h:15
defs.h
UNUSED
#define UNUSED(x)
Mark a variable as unused.
Definition
defs.h:100
NULL
#define NULL
Pointer error value.
Definition
NULL.h:23
clock_t
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition
clock_t.h:13
next
static atomic_long next
Definition
main.c:12
is_prime
bool is_prime(uint64_t n)
Definition
main.c:14
benchmark
static void benchmark(uint64_t threadAmount)
Definition
main.c:69
PRIME_MAX
#define PRIME_MAX
Definition
main.c:9
count_primes
static void count_primes(uint64_t start, uint64_t end)
Definition
main.c:40
thread_entry
static int thread_entry(void *arg)
Definition
main.c:51
count
static atomic_long count
Definition
main.c:11
stdatomic.h
atomic_load
#define atomic_load(object)
Definition
stdatomic.h:288
atomic_fetch_add
#define atomic_fetch_add(object, operand)
Definition
stdatomic.h:283
atomic_init
#define atomic_init(obj, value)
Definition
stdatomic.h:75
stdbool.h
stdint.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
stdio.h
fflush
_PUBLIC int fflush(FILE *stream)
Definition
fflush.c:5
printf
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition
printf.c:3
stdout
FILE * stdout
Definition
std_streams.c:18
thrd_t
Definition
threads.h:38
threads.h
thrd_create
_PUBLIC int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Definition
thrd_create.c:11
thrd_join
_PUBLIC int thrd_join(thrd_t thr, int *res)
Definition
thrd_join.c:11
thrd_success
@ thrd_success
Definition
threads.h:74
time.h
clock
_PUBLIC clock_t clock(void)
Definition
clock.c:5
src
programs
utils
threadtest
main.c
Generated on Fri Jan 9 2026 06:47:13 for PatchworkOS by
1.9.8