libc: Prevent writer starvation in fibril_rwlock_t.
[helenos.git] / uspace / app / tester / float / float1.c
blobe6bc7ae9a2bb2607b54402bdcd5fb5444c85f433
1 /*
2 * Copyright (c) 2005 Jakub Vana
3 * Copyright (c) 2005 Jakub Jermar
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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.
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"
40 #define THREADS 150
41 #define ATTEMPTS 100
43 #define E_10E8 UINT32_C(271828182)
44 #define PRECISION 100000000
46 static FIBRIL_SEMAPHORE_INITIALIZE(threads_finished, 0);
47 static atomic_int threads_fault;
49 static errno_t e(void *data)
51 for (unsigned int i = 0; i < ATTEMPTS; i++) {
52 double le = -1;
53 double e = 0;
54 double f = 1;
56 for (double d = 1; e != le; d *= f, f += 1) {
57 le = e;
58 e = e + 1 / d;
61 if ((uint32_t) (e * PRECISION) != E_10E8) {
62 atomic_fetch_add(&threads_fault, 1);
63 break;
67 fibril_semaphore_up(&threads_finished);
68 return EOK;
71 const char *test_float1(void)
73 int total = 0;
75 atomic_store(&threads_fault, 0);
76 fibril_test_spawn_runners(THREADS);
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;
85 fibril_detach(f);
86 fibril_add_ready(f);
88 TPRINTF(".");
89 total++;
92 TPRINTF("\n");
94 for (int i = 0; i < total; i++) {
95 TPRINTF("Threads left: %d\n", total - i);
96 fibril_semaphore_down(&threads_finished);
99 if (atomic_load(&threads_fault) == 0)
100 return NULL;
102 return "Test failed";