source: mainline/kernel/test/mm/falloc2.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: 4.1 KB
RevLine 
[078a0a1]1/*
[df4ed85]2 * Copyright (c) 2006 Sergey Bondari
[078a0a1]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
[96348adc]28
[078a0a1]29#include <test.h>
30#include <mm/page.h>
31#include <mm/frame.h>
[aafed15]32#include <stdlib.h>
[078a0a1]33#include <arch/mm/page.h>
[d99c1d2]34#include <typedefs.h>
[23684b7]35#include <atomic.h>
[078a0a1]36#include <proc/thread.h>
[b169619]37#include <memw.h>
[4a2f4bb]38#include <arch.h>
[078a0a1]39
[b0c2075]40#define MAX_FRAMES 256
[078a0a1]41
[cb01e1e]42#define THREAD_RUNS 1
43#define THREADS 8
[078a0a1]44
[31e15be]45static atomic_size_t thread_fail;
[078a0a1]46
[cb01e1e]47static void falloc(void *arg)
[4a2f4bb]48{
[7f1c620]49 uint8_t val = THREAD->tid % THREADS;
[a35b458]50
[8cbf1c3]51 uintptr_t *frames = (uintptr_t *)
[11b285d]52 malloc(MAX_FRAMES * sizeof(uintptr_t));
[96348adc]53 if (frames == NULL) {
[abfc9f3]54 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
55 "Unable to allocate frames\n", THREAD->tid, CPU->id);
[96348adc]56 atomic_inc(&thread_fail);
57 return;
58 }
[a35b458]59
[abfc9f3]60 for (unsigned int run = 0; run < THREAD_RUNS; run++) {
[b0c2075]61 for (size_t count = 1; count <= MAX_FRAMES; count++) {
62 size_t bytes = FRAMES2SIZE(count);
[a35b458]63
[abfc9f3]64 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
[b0c2075]65 "Allocating %zu frames blocks (%zu bytes) ... \n", THREAD->tid,
66 CPU->id, count, bytes);
[a35b458]67
[abfc9f3]68 unsigned int allocated = 0;
[b0c2075]69 for (unsigned int i = 0; i < (MAX_FRAMES / count); i++) {
[cd3b380]70 frames[allocated] = frame_alloc(count, FRAME_ATOMIC, 0);
[7ee0e2f]71 if (frames[allocated]) {
[cd3b380]72 memsetb((void *) PA2KA(frames[allocated]), bytes, val);
[078a0a1]73 allocated++;
[96348adc]74 } else
[078a0a1]75 break;
76 }
[a35b458]77
[abfc9f3]78 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
79 "%u blocks allocated.\n", THREAD->tid, CPU->id,
80 allocated);
81 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
82 "Deallocating ... \n", THREAD->tid, CPU->id);
[a35b458]83
[abfc9f3]84 for (unsigned int i = 0; i < allocated; i++) {
[b0c2075]85 for (size_t k = 0; k < bytes; k++) {
[cd3b380]86 if (((uint8_t *) PA2KA(frames[i]))[k] != val) {
[abfc9f3]87 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
[8cbf1c3]88 "Unexpected data (%c) in block %zu offset %zu\n",
[cd3b380]89 THREAD->tid, CPU->id, ((char *) PA2KA(frames[i]))[k],
[abfc9f3]90 frames[i], k);
[96348adc]91 atomic_inc(&thread_fail);
92 goto cleanup;
[bd6e392]93 }
94 }
[cd3b380]95 frame_free(frames[i], count);
[078a0a1]96 }
[a35b458]97
[abfc9f3]98 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
99 "Finished run.\n", THREAD->tid, CPU->id);
[078a0a1]100 }
101 }
[a35b458]102
[cb01e1e]103cleanup:
[4a2f4bb]104 free(frames);
[a35b458]105
[abfc9f3]106 TPRINTF("Thread #%" PRIu64 " (cpu%u): Exiting\n",
107 THREAD->tid, CPU->id);
[078a0a1]108}
109
[a000878c]110const char *test_falloc2(void)
[4a2f4bb]111{
[e3306d04]112 atomic_store(&thread_fail, 0);
[a35b458]113
[0f4f1b2]114 thread_t *threads[THREADS] = { };
115
[abfc9f3]116 for (unsigned int i = 0; i < THREADS; i++) {
[6eef3c4]117 thread_t *thrd = thread_create(falloc, NULL, TASK,
118 THREAD_FLAG_NONE, "falloc2");
[96348adc]119 if (!thrd) {
[cb01e1e]120 TPRINTF("Could not create thread %u\n", i);
[96348adc]121 break;
122 }
[0f4f1b2]123 thread_start(thrd);
124 threads[i] = thrd;
[078a0a1]125 }
[a35b458]126
[0f4f1b2]127 for (unsigned int i = 0; i < THREADS; i++) {
128 if (threads[i] != NULL)
129 thread_join(threads[i]);
130
131 TPRINTF("Threads left: %u\n", THREADS - i - 1);
[96348adc]132 }
[a35b458]133
[036e97c]134 if (atomic_load(&thread_fail) == 0)
[96348adc]135 return NULL;
[a35b458]136
[96348adc]137 return "Test failed";
[078a0a1]138}
Note: See TracBrowser for help on using the repository browser.