Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libpthread add joins



details:   https://anonhg.NetBSD.org/src/rev/bded1c1a1894
branches:  trunk
changeset: 449413:bded1c1a1894
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Mar 06 01:20:15 2019 +0000

description:
add joins

diffstat:

 tests/lib/libpthread/h_resolv.c |  27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)

diffs (92 lines):

diff -r 9c06851b332c -r bded1c1a1894 tests/lib/libpthread/h_resolv.c
--- a/tests/lib/libpthread/h_resolv.c   Wed Mar 06 01:13:38 2019 +0000
+++ b/tests/lib/libpthread/h_resolv.c   Wed Mar 06 01:20:15 2019 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */
+/* $NetBSD: h_resolv.c,v 1.3 2019/03/06 01:20:15 christos Exp $ */
 
 /*-
  * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 #include <sys/cdefs.h>
 __COPYRIGHT("@(#) Copyright (c) 2008\
  The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $");
+__RCSID("$NetBSD: h_resolv.c,v 1.3 2019/03/06 01:20:15 christos Exp $");
 
 #include <pthread.h>
 #include <stdio.h>
@@ -56,7 +56,7 @@
 static void load(const char *);
 static void resolvone(int);
 static void *resolvloop(void *);
-static void run(int *);
+static pthread_t run(int *);
 
 static pthread_mutex_t stats = PTHREAD_MUTEX_INITIALIZER;
 
@@ -77,7 +77,7 @@
        char *line;
 
        if ((fp = fopen(fname, "r")) == NULL)
-               err(1, "Cannot open `%s'", fname);
+               err(EXIT_FAILURE, "Cannot open `%s'", fname);
        while ((line = fgetln(fp, &len)) != NULL) {
                char c = line[len];
                char *ptr;
@@ -130,18 +130,20 @@
        return NULL;
 }
 
-static void
+static pthread_t
 run(int *nhosts)
 {
        pthread_t self = pthread_self();
        if (pthread_create(&self, NULL, resolvloop, nhosts) != 0)
-               err(1, "pthread_create");
+               err(EXIT_FAILURE, "pthread_create");
+       return self;
 }
 
 int
 main(int argc, char *argv[])
 {
        int nthreads = NTHREADS;
+       pthread_t *threads;
        int nhosts = NHOSTS;
        int i, c, done, *nleft;
        hosts = sl_init();
@@ -170,16 +172,18 @@
                usage();
 
        if ((nleft = malloc(nthreads * sizeof(int))) == NULL)
-               err(1, "malloc");
+               err(EXIT_FAILURE, "malloc");
        if ((ask = calloc(hosts->sl_cur, sizeof(int))) == NULL)
-               err(1, "calloc");
+               err(EXIT_FAILURE, "calloc");
        if ((got = calloc(hosts->sl_cur, sizeof(int))) == NULL)
-               err(1, "calloc");
+               err(EXIT_FAILURE, "calloc");
+       if ((threads = malloc(nthreads * sizeof(pthread_t))) == NULL)
+               err(EXIT_FAILURE, "calloc");
 
 
        for (i = 0; i < nthreads; i++) {
                nleft[i] = nhosts;
-               run(&nleft[i]);
+               threads[i] = run(&nleft[i]);
        }
 
        for (done = 0; !done;) {
@@ -200,6 +204,9 @@
                        c++;
                }
        }
+       for (i = 0; i < nthreads; i++)
+               pthread_join(threads[i], NULL);
+       free(threads);
        free(nleft);
        free(ask);
        free(got);



Home | Main Index | Thread Index | Old Index