From 8a3381695b3d5196ebfc4431e10c4f4f52db6ad4 Mon Sep 17 00:00:00 2001 From: Nemo D'ACREMONT Date: Fri, 28 Mar 2025 14:47:04 +0100 Subject: [PATCH] feat: update tests --- Makefile | 2 + fetch_tests.sh | 2 +- tst/32-switch-many-join.c | 2 +- tst/63-mutex-equity.c | 84 ++++++++++++++++++++++++++++++++++ tst/64-mutex-join.c | 94 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 tst/63-mutex-equity.c create mode 100644 tst/64-mutex-join.c diff --git a/Makefile b/Makefile index 09c7f1e..982bba6 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,8 @@ bins+=33-switch-many-cascade bins+=51-fibonacci bins+=61-mutex bins+=62-mutex +bins+=63-mutex-equity +bins+=64-mutex-join bins+=71-preemption bins+=81-deadlock diff --git a/fetch_tests.sh b/fetch_tests.sh index 391cdcb..732baa4 100755 --- a/fetch_tests.sh +++ b/fetch_tests.sh @@ -1,7 +1,7 @@ #!/bin/sh tst_dir=tst -filenames="01-main 02-switch 03-equity 11-join 12-join-main 21-create-many 22-create-many-recursive 23-create-many-once 31-switch-many 32-switch-many-join 33-switch-many-cascade 51-fibonacci 61-mutex 62-mutex 71-preemption 81-deadlock" +filenames="01-main 02-switch 03-equity 11-join 12-join-main 21-create-many 22-create-many-recursive 23-create-many-once 31-switch-many 32-switch-many-join 33-switch-many-cascade 51-fibonacci 61-mutex 62-mutex 63-mutex-equity 64-mutex-join 71-preemption 81-deadlock" mkdir -p "${tst_dir}" for filename in ${filenames} diff --git a/tst/32-switch-many-join.c b/tst/32-switch-many-join.c index 5a0afc3..42fe908 100644 --- a/tst/32-switch-many-join.c +++ b/tst/32-switch-many-join.c @@ -42,6 +42,7 @@ static void * thfunc(void *_nbth) us = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec); printf("%d yield avec plein de threads dans join: %ld us\n", nbyield, us); + printf("%ld threads créés et détruits\n", nbthrd); printf("GRAPH;32;%ld;%d;%lu\n", nbthrd, nbyield, us); } return _nbth; @@ -59,6 +60,5 @@ int main(int argc, char *argv[]) thfunc((void*) nbthrd); - printf("%ld threads créés et détruits\n", nbthrd); return 0; } diff --git a/tst/63-mutex-equity.c b/tst/63-mutex-equity.c new file mode 100644 index 0000000..c450ab8 --- /dev/null +++ b/tst/63-mutex-equity.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include "thread.h" + +/* test pour verifier que prendre un mutex ne desactive pas l'equité envers les autres threads. + * + * valgrind doit etre content. + * Le programme doit finir. + * + * support nécessaire: + * - thread_create() + * - retour sans thread_exit() + * - thread_join() sans récupération de la valeur de retour + * - thread_mutex_init() + * - thread_mutex_destroy() + * - thread_mutex_lock() + * - thread_mutex_unlock() + */ + +int fini = 0; +thread_mutex_t lock; + +static void * thfunc(void *dummy __attribute__((unused))) +{ + unsigned i; + + /* on incremente progressivement fini jusque 5 pour debloquer le main */ + for(i=0; i<5; i++) { + thread_yield(); + fini++; + } + + /* on attend que main remette à 0 */ + thread_mutex_lock(&lock); + while (fini != 0) + thread_yield(); + thread_mutex_unlock(&lock); + + thread_exit(NULL); +} + +int main(void) +{ + thread_t th; + int err, i; + struct timeval tv1, tv2; + unsigned long us; + + gettimeofday(&tv1, NULL); + + /* on cree le mutex et le thread */ + if (thread_mutex_init(&lock) != 0) { + fprintf(stderr, "thread_mutex_init failed\n"); + return -1; + } + err = thread_create(&th, thfunc, NULL); + assert(!err); + + /* on prend le lock puis on attend que l'autre mette fini = 5 */ + thread_mutex_lock(&lock); + while (fini != 5) + thread_yield(); + thread_mutex_unlock(&lock); + + /* on baisse progressivement jusque 0 */ + for(i=0; i<5; i++) { + thread_yield(); + fini--; + } + + /* fini */ + err = thread_join(th, NULL); + assert(!err); + gettimeofday(&tv2, NULL); + thread_mutex_destroy(&lock); + + us = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec); + printf("%lu us\n", us); + return EXIT_SUCCESS; +} diff --git a/tst/64-mutex-join.c b/tst/64-mutex-join.c new file mode 100644 index 0000000..9f10e71 --- /dev/null +++ b/tst/64-mutex-join.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include "thread.h" + +/* test pour verifier que prendre un mutex ne casse pas le join + * + * valgrind doit etre content. + * Le programme doit finir. + * + * support nécessaire: + * - thread_create() + * - retour sans thread_exit() + * - thread_join() dans un mutex + * - thread_mutex_init() + * - thread_mutex_destroy() + * - thread_mutex_lock() + * - thread_mutex_unlock() + */ + +int pret = 0; +thread_mutex_t lock; + +static void * thfunc(void *dummy __attribute__((unused))) +{ + unsigned i; + int err; + + /* on prend le verrou puis on se marque pret */ + err = thread_mutex_lock(&lock); + assert(!err); + + pret = 1; + printf(" fils: verrouillé\n"); + for(i=0; i<5; i++) { + printf(" fils: yield() pour verifier que le père n'arrive pas aller plus loin\n"); + thread_yield(); + } + printf(" fils: va déverrouiller\n"); + pret = 2; + err = thread_mutex_unlock(&lock); + assert(!err); + + for(i=0; i<5; i++) { + printf(" fils: yield() avant de finir\n"); + thread_yield(); + } + thread_exit((void*) 0xdeadbeef); +} + +int main(void) +{ + thread_t th; + int err; + struct timeval tv1, tv2; + unsigned long us; + void *ret; + + gettimeofday(&tv1, NULL); + + /* on cree le mutex et le thread */ + if (thread_mutex_init(&lock) != 0) { + fprintf(stderr, "thread_mutex_init failed\n"); + return -1; + } + err = thread_create(&th, thfunc, NULL); + assert(!err); + + printf("pere: attend que le fils ait démarré\n"); + while (pret == 0) + thread_yield(); + + printf("pere: fils pret, on verrouille\n"); + err = thread_mutex_lock(&lock); + assert(!err); + printf("pere: verrouillé, on joine\n"); + err = thread_join(th, &ret); + assert(!err); + assert(ret == (void*)0xdeadbeef); + assert(pret == 2); + printf("pere: join réussi\n"); + + gettimeofday(&tv2, NULL); + err = thread_mutex_unlock(&lock); + assert(!err); + thread_mutex_destroy(&lock); + + us = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec); + printf("%lu us\n", us); + return EXIT_SUCCESS; +}