feat: update tests

This commit is contained in:
Nemo D'ACREMONT 2025-03-28 14:47:04 +01:00
parent 9b2e156869
commit 8a3381695b
No known key found for this signature in database
GPG Key ID: 85F245EC3BB1E022
5 changed files with 182 additions and 2 deletions

View File

@ -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

View File

@ -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}

View File

@ -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;
}

84
tst/63-mutex-equity.c Normal file
View File

@ -0,0 +1,84 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>
#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;
}

94
tst/64-mutex-join.c Normal file
View File

@ -0,0 +1,94 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>
#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;
}