nvttest: Rework thread handling
This commit is contained in:
+20
-13
@@ -39,6 +39,11 @@ typedef struct {
|
|||||||
bool opt_rand_delay;
|
bool opt_rand_delay;
|
||||||
} topts_t;
|
} topts_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
pthread_t thr;
|
||||||
|
topts_t opts;
|
||||||
|
} thr_t;
|
||||||
|
|
||||||
|
|
||||||
static void _usage(int rc)
|
static void _usage(int rc)
|
||||||
{
|
{
|
||||||
@@ -48,15 +53,16 @@ static void _usage(int rc)
|
|||||||
|
|
||||||
static void *_worker(void *arg)
|
static void *_worker(void *arg)
|
||||||
{
|
{
|
||||||
topts_t *topts = arg;
|
thr_t *thr = arg;
|
||||||
int iloop;
|
int iloop;
|
||||||
int fd, err, nw;
|
int fd, err, nw;
|
||||||
struct termios ios;
|
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",
|
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);
|
fd = open(dev, O_RDWR);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
@@ -113,7 +119,7 @@ static void *_worker(void *arg)
|
|||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (topts->opt_rand_delay)
|
if (thr->opts.opt_rand_delay)
|
||||||
usleep(rand() & 0xfff);
|
usleep(rand() & 0xfff);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,8 +131,8 @@ int main(int argc, char **argv)
|
|||||||
int err;
|
int err;
|
||||||
int opt;
|
int opt;
|
||||||
int ithr, nthr;
|
int ithr, nthr;
|
||||||
topts_t topts = { }, *popts;
|
thr_t *threads, *thr;
|
||||||
pthread_t *ptids;
|
topts_t topts = { };
|
||||||
int debug, quiet;
|
int debug, quiet;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
@@ -173,17 +179,17 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
topts.nthread = nthr;
|
topts.nthread = nthr;
|
||||||
|
|
||||||
ptids = malloc(nthr * sizeof(pthread_t));
|
threads = malloc(nthr * sizeof(thr_t));
|
||||||
|
|
||||||
for (ithr = 0; ithr < nthr; ithr++)
|
for (ithr = 0; ithr < nthr; ithr++)
|
||||||
{
|
{
|
||||||
nvt_log(LOG_INFO, "Create worker thread %d/%d\n", ithr + 1, nthr);
|
nvt_log(LOG_INFO, "Create worker thread %d/%d\n", ithr + 1, nthr);
|
||||||
|
|
||||||
popts = malloc(sizeof(topts_t));
|
thr = &threads[ithr];
|
||||||
*popts = topts;
|
thr->opts = topts;
|
||||||
popts->ithread = ithr + 1;
|
thr->opts.ithread = ithr + 1;
|
||||||
|
|
||||||
err = pthread_create(&ptids[ithr], NULL, _worker, popts);
|
err = pthread_create(&thr->thr, NULL, _worker, thr);
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
nvt_log(LOG_ERR, "Failed to create thread %d/%d\n", ithr, nthr);
|
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++)
|
for (ithr = 0; ithr < nthr; ithr++)
|
||||||
{
|
{
|
||||||
if (ptids[ithr] == 0)
|
thr = &threads[ithr];
|
||||||
|
if (thr->thr == 0)
|
||||||
break;
|
break;
|
||||||
pthread_join(ptids[ithr], NULL);
|
pthread_join(thr->thr, NULL);
|
||||||
nvt_log(LOG_INFO, "Joined worker thread %d/%d\n", ithr + 1, nthr);
|
nvt_log(LOG_INFO, "Joined worker thread %d/%d\n", ithr + 1, nthr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user