source: mainline/kernel/test/thread/thread1.c

Last change on this file was 0f4f1b2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 18 months ago

Add (and use) functions thread_start() and thread_detach()

Mostly cosmetic, with thread_start() replacing calls to thread_ready(),
but not consuming the passed reference, and thread_detach() being
synonym for thread_put(). Makes the code's function more obvious.

Also modify some threaded tests to use thread_join() for waiting,
instead of counting threads with atomics or semaphores.

  • Property mode set to 100644
File size: 2.4 KB
Line 
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 <test.h>
31#include <atomic.h>
32#include <stdbool.h>
33#include <proc/thread.h>
34
35#include <arch.h>
36
37#define THREADS 5
38
39static atomic_bool finish;
40
41static void threadtest(void *data)
42{
43 while (atomic_load(&finish)) {
44 TPRINTF("%" PRIu64 " ", THREAD->tid);
45 thread_usleep(100000);
46 }
47}
48
49const char *test_thread1(void)
50{
51 atomic_store(&finish, true);
52
53 thread_t *threads[THREADS] = { };
54
55 for (int i = 0; i < THREADS; i++) {
56 threads[i] = thread_create(threadtest, NULL,
57 TASK, THREAD_FLAG_NONE, "threadtest");
58
59 if (threads[i]) {
60 thread_start(threads[i]);
61 } else {
62 TPRINTF("Could not create thread %d\n", i);
63 break;
64 }
65 }
66
67 TPRINTF("Running threads for 10 seconds...\n");
68 thread_sleep(10);
69
70 atomic_store(&finish, false);
71
72 for (int i = 0; i < THREADS; i++) {
73 if (threads[i] != NULL)
74 thread_join(threads[i]);
75
76 TPRINTF("Threads left: %d\n", THREADS - i - 1);
77 }
78
79 return NULL;
80}
Note: See TracBrowser for help on using the repository browser.