PatchworkOS
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1#include <stdatomic.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <sys/proc.h>
5#include <threads.h>
6
7#define PRIME_MAX (10000000)
8
9static atomic_long count;
10static atomic_long next;
11
13{
14 if (n <= 1)
15 {
16 return false;
17 }
18 if (n <= 3)
19 {
20 return true;
21 }
22 if (n % 2 == 0 || n % 3 == 0)
23 {
24 return false;
25 }
26
27 for (uint64_t i = 5; i * i <= n; i += 6)
28 {
29 if (n % i == 0 || n % (i + 2) == 0)
30 {
31 return false;
32 }
33 }
34
35 return true;
36}
37
39{
40 for (uint64_t i = start; i < end; i++)
41 {
42 if (is_prime(i))
43 {
45 }
46 }
47}
48
49static int thread_entry(void* arg)
50{
51 (void)arg;
52
54 while ((start = atomic_fetch_add(&next, 1000)) < PRIME_MAX)
55 {
56 uint64_t end = start + 1000;
57 if (end > PRIME_MAX)
58 {
59 end = PRIME_MAX;
60 }
61
62 count_primes(start, end);
63 }
64 return thrd_success;
65}
66
67static void benchmark(uint64_t threadAmount)
68{
69 printf("%d threads: starting...", threadAmount);
72
73 atomic_init(&count, 0);
74 atomic_init(&next, 0);
75
76 thrd_t threads[threadAmount];
77 for (uint64_t i = 0; i < threadAmount; i++)
78 {
80 {
81 printf(" (thrd_create error %d) ", i);
83 }
84 }
85
86 for (uint64_t i = 0; i < threadAmount; i++)
87 {
89 {
90 printf(" (thrd_join error %d) ", i);
92 }
93 }
94
95 clock_t end = uptime();
96 printf(" took %d ms to find %d primes\n", (end - start) / (CLOCKS_PER_SEC / 1000), atomic_load(&count));
97}
98
99int main(void)
100{
101 for (uint64_t threads = 1; threads <= 1024; threads *= 2)
102 {
104 }
105
106 printf("Testing complete.\n");
107 return 0;
108}
#define CLOCKS_PER_SEC
Definition clock_t.h:15
clock_t uptime(void)
System call for retreving the time since boot.
Definition uptime.c:6
#define NULL
Pointer error value.
Definition NULL.h:23
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static list_t threads
Definition thread.c:8
int main()
Definition main.c:97
static void start()
Definition main.c:542
static atomic_long next
Definition main.c:10
bool is_prime(uint64_t n)
Definition main.c:12
static void benchmark(uint64_t threadAmount)
Definition main.c:67
#define PRIME_MAX
Definition main.c:7
static void count_primes(uint64_t start, uint64_t end)
Definition main.c:38
static int thread_entry(void *arg)
Definition main.c:49
static atomic_long count
Definition main.c:9
#define atomic_load(object)
Definition stdatomic.h:288
#define atomic_fetch_add(object, operand)
Definition stdatomic.h:283
#define atomic_init(obj, value)
Definition stdatomic.h:75
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int fflush(FILE *stream)
Definition fflush.c:6
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:5
FILE * stdout
Definition std_streams.c:17
@ thrd_success
Definition threads.h:74
_PUBLIC int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Definition thrd_create.c:30
_PUBLIC int thrd_join(thrd_t thr, int *res)
Definition thrd_join.c:10