nvttest: Rework thread handling

This commit is contained in:
Kim Woelders
2024-11-10 12:36:28 +01:00
parent 3bcbf9c8c1
commit da027d4de1
+20 -13
View File
@@ -39,6 +39,11 @@ typedef struct {
bool opt_rand_delay;
} topts_t;
typedef struct {
pthread_t thr;
topts_t opts;
} thr_t;
static void _usage(int rc)
{
@@ -48,15 +53,16 @@ static void _usage(int rc)
static void *_worker(void *arg)
{
topts_t *topts = arg;
thr_t *thr = arg;
int iloop;
int fd, err, nw;
struct termios ios;
for (iloop = 0; iloop < topts->nloop; iloop++)
for (iloop = 0; iloop < thr->opts.nloop; iloop++)
{
nvt_log(LOG_INFO, "Thread %d/%d run %d/%d\n",
topts->ithread, topts->nthread, iloop + 1, topts->nloop);
thr->opts.ithread, thr->opts.nthread,
iloop + 1, thr->opts.nloop);
{
fd = open(dev, O_RDWR);
if (fd < 0)
@@ -113,7 +119,7 @@ static void *_worker(void *arg)
close(fd);
}
if (topts->opt_rand_delay)
if (thr->opts.opt_rand_delay)
usleep(rand() & 0xfff);
}
@@ -125,8 +131,8 @@ int main(int argc, char **argv)
int err;
int opt;
int ithr, nthr;
topts_t topts = { }, *popts;
pthread_t *ptids;
thr_t *threads, *thr;
topts_t topts = { };
int debug, quiet;
struct stat st;
@@ -173,17 +179,17 @@ int main(int argc, char **argv)
topts.nthread = nthr;
ptids = malloc(nthr * sizeof(pthread_t));
threads = malloc(nthr * sizeof(thr_t));
for (ithr = 0; ithr < nthr; ithr++)
{
nvt_log(LOG_INFO, "Create worker thread %d/%d\n", ithr + 1, nthr);
popts = malloc(sizeof(topts_t));
*popts = topts;
popts->ithread = ithr + 1;
thr = &threads[ithr];
thr->opts = topts;
thr->opts.ithread = ithr + 1;
err = pthread_create(&ptids[ithr], NULL, _worker, popts);
err = pthread_create(&thr->thr, NULL, _worker, thr);
if (err)
{
nvt_log(LOG_ERR, "Failed to create thread %d/%d\n", ithr, nthr);
@@ -193,9 +199,10 @@ int main(int argc, char **argv)
for (ithr = 0; ithr < nthr; ithr++)
{
if (ptids[ithr] == 0)
thr = &threads[ithr];
if (thr->thr == 0)
break;
pthread_join(ptids[ithr], NULL);
pthread_join(thr->thr, NULL);
nvt_log(LOG_INFO, "Joined worker thread %d/%d\n", ithr + 1, nthr);
}