cs: Fix leaked pthreads
authorBarret Rhoden <brho@cs.berkeley.edu>
Tue, 9 Oct 2018 19:45:56 +0000 (15:45 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 9 Oct 2018 21:45:59 +0000 (17:45 -0400)
We were leaking pthreads, once for each job request.  This was a major
culprit in leaking memory when running go tests.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
tests/cs.c

index 454afa0..50252ef 100644 (file)
@@ -495,6 +495,7 @@ static void io(void)
        long n;
        uint8_t mdata[IOHDRSZ + Maxfdata];
        struct job *job;
+       pthread_attr_t pth_attr;
 
        /*
         * each request is handled via a thread. Somewhat less efficient than the
@@ -502,6 +503,8 @@ static void io(void)
         * cs but way cleaner.
         */
 
+       pthread_attr_init(&pth_attr);
+       pthread_attr_setdetachstate(&pth_attr, PTHREAD_CREATE_DETACHED);
        for (;;) {
                n = read9pmsg(mfd[0], mdata, sizeof(mdata));
                if (n <= 0)
@@ -519,7 +522,7 @@ static void io(void)
                /* stash the thread in the job so we can join them all
                 * later if we want to.
                 */
-               if (pthread_create(&job->thread, NULL, &job_thread, job)) {
+               if (pthread_create(&job->thread, &pth_attr, &job_thread, job)) {
                        error(1, 0, "%s: %r", "Failed to create job");
                        continue;
                }