Remove __threaded variable

This commit is contained in:
Justine Tunney 2024-07-28 23:43:22 -07:00
parent 01b09bc817
commit cf1559c448
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
15 changed files with 51 additions and 71 deletions

View file

@ -189,8 +189,6 @@ int main(int argc, char *argv[]) {
_Exit(1);
}
__threaded = 1;
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
ASSERT_EQ(0, pthread_mutex_init(&mu, &attr));

View file

@ -179,47 +179,15 @@ void MallocFree(void) {
free(p);
}
void eat(void *p) {
}
void (*pEat)(void *) = eat;
BENCH(bulk_free, bench) {
/* pEat(pthread_create); */
EZBENCH2("free() bulk", BulkFreeBenchSetup(), FreeBulk());
EZBENCH2("bulk_free()", BulkFreeBenchSetup(),
bulk_free(bulk, ARRAYLEN(bulk)));
EZBENCH2("free(malloc(16)) ST", donothing, MallocFree());
__enable_threads();
EZBENCH2("free(malloc(16)) MT", donothing, MallocFree());
}
#define ITERATIONS 1000
#define THREADS 10
#define SIZE 1024
void *Worker(void *arg) {
for (int i = 0; i < ITERATIONS; ++i) {
char *p;
ASSERT_NE(NULL, (p = malloc(lemur64() % SIZE)));
ASSERT_NE(NULL, (p = realloc(p, max(lemur64() % SIZE, 1))));
free(p);
}
return 0;
}
TEST(malloc, torture) {
int i, n = THREADS;
pthread_t *t = gc(malloc(sizeof(pthread_t) * n));
if (!n)
return;
printf("\nmalloc torture test w/ %d threads and %d iterations\n", n,
ITERATIONS);
SPAWN(fork);
struct timespec t1 = timespec_real();
for (i = 0; i < n; ++i) {
ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
}
for (i = 0; i < n; ++i) {
ASSERT_EQ(0, pthread_join(t[i], 0));
}
struct timespec t2 = timespec_real();
printf("consumed %g wall and %g cpu seconds\n",
timespec_tomicros(timespec_sub(t2, t1)) * 1e-6,
(double)clock() / CLOCKS_PER_SEC);
EXITS(0);
EZBENCH2("free(malloc(16))", donothing, MallocFree());
}

View file

@ -0,0 +1,61 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2024 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/timespec.h"
#include "libc/intrin/safemacros.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#define ITERATIONS 1000
#define THREADS 10
#define SIZE 1024
void *Worker(void *arg) {
for (int i = 0; i < ITERATIONS; ++i) {
char *p;
ASSERT_NE(NULL, (p = malloc(lemur64() % SIZE)));
ASSERT_NE(NULL, (p = realloc(p, max(lemur64() % SIZE, 1))));
free(p);
}
return 0;
}
TEST(malloc, torture) {
int i, n = THREADS;
pthread_t *t = gc(malloc(sizeof(pthread_t) * n));
if (!n)
return;
printf("\nmalloc torture test w/ %d threads and %d iterations\n", n,
ITERATIONS);
SPAWN(fork);
struct timespec t1 = timespec_real();
for (i = 0; i < n; ++i)
ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
for (i = 0; i < n; ++i)
ASSERT_EQ(0, pthread_join(t[i], 0));
struct timespec t2 = timespec_real();
printf("consumed %g wall and %g cpu seconds\n",
timespec_tomicros(timespec_sub(t2, t1)) * 1e-6,
(double)clock() / CLOCKS_PER_SEC);
EXITS(0);
}

View file

@ -48,7 +48,6 @@ void *ForceThreadingMode(void *arg) {
}
TEST(pthread_atfork, test) {
__enable_threads();
SPAWN(fork);
ASSERT_EQ(0, pthread_atfork(prepare1, parent1, child1));
ASSERT_EQ(0, pthread_atfork(prepare2, parent2, child2));