1 | /*
|
---|
2 | * Copyright (c) 2005 Jakub Vana
|
---|
3 | * Copyright (c) 2005 Jakub Jermar
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include <errno.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <stddef.h>
|
---|
34 | #include <stdatomic.h>
|
---|
35 | #include <fibril.h>
|
---|
36 | #include <fibril_synch.h>
|
---|
37 | #include <inttypes.h>
|
---|
38 | #include "../tester.h"
|
---|
39 |
|
---|
40 | #define THREADS 150
|
---|
41 | #define ATTEMPTS 100
|
---|
42 |
|
---|
43 | #define E_10E8 UINT32_C(271828182)
|
---|
44 | #define PRECISION 100000000
|
---|
45 |
|
---|
46 | static FIBRIL_SEMAPHORE_INITIALIZE(threads_finished, 0);
|
---|
47 | static atomic_int threads_fault;
|
---|
48 |
|
---|
49 | static errno_t e(void *data)
|
---|
50 | {
|
---|
51 | for (unsigned int i = 0; i < ATTEMPTS; i++) {
|
---|
52 | double le = -1;
|
---|
53 | double e = 0;
|
---|
54 | double f = 1;
|
---|
55 |
|
---|
56 | for (double d = 1; e != le; d *= f, f += 1) {
|
---|
57 | le = e;
|
---|
58 | e = e + 1 / d;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if ((uint32_t) (e * PRECISION) != E_10E8) {
|
---|
62 | atomic_fetch_add(&threads_fault, 1);
|
---|
63 | break;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | fibril_semaphore_up(&threads_finished);
|
---|
68 | return EOK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | const char *test_float1(void)
|
---|
72 | {
|
---|
73 | int total = 0;
|
---|
74 |
|
---|
75 | atomic_store(&threads_fault, 0);
|
---|
76 | fibril_test_spawn_runners(THREADS);
|
---|
77 |
|
---|
78 | TPRINTF("Creating threads");
|
---|
79 | for (unsigned int i = 0; i < THREADS; i++) {
|
---|
80 | fid_t f = fibril_create(e, NULL);
|
---|
81 | if (!f) {
|
---|
82 | TPRINTF("\nCould not create thread %u\n", i);
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | fibril_detach(f);
|
---|
86 | fibril_add_ready(f);
|
---|
87 |
|
---|
88 | TPRINTF(".");
|
---|
89 | total++;
|
---|
90 | }
|
---|
91 |
|
---|
92 | TPRINTF("\n");
|
---|
93 |
|
---|
94 | for (int i = 0; i < total; i++) {
|
---|
95 | TPRINTF("Threads left: %d\n", total - i);
|
---|
96 | fibril_semaphore_down(&threads_finished);
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (atomic_load(&threads_fault) == 0)
|
---|
100 | return NULL;
|
---|
101 |
|
---|
102 | return "Test failed";
|
---|
103 | }
|
---|