From nobody@FreeBSD.org  Thu Feb 11 16:53:14 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 918761065672
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 11 Feb 2010 16:53:14 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 6774D8FC0C
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 11 Feb 2010 16:53:14 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o1BGrEhd007279
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 11 Feb 2010 16:53:14 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o1BGrErU007278;
	Thu, 11 Feb 2010 16:53:14 GMT
	(envelope-from nobody)
Message-Id: <201002111653.o1BGrErU007278@www.freebsd.org>
Date: Thu, 11 Feb 2010 16:53:14 GMT
From: pluknet <pluknet@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: pthread_create retval check fixup
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         143807
>Category:       misc
>Synopsis:       [tools] [patch] pthread_create retval check fixup
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    ru
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Feb 11 17:00:01 UTC 2010
>Closed-Date:    Fri Feb 12 16:33:34 UTC 2010
>Last-Modified:  Fri Feb 12 16:40:03 UTC 2010
>Originator:     pluknet
>Release:        HEAD r203781
>Organization:
>Environment:
>Description:
I faced with incorrect return code checking in netrate's pthread_create(3) calls: pthread_create may return errcode > 0, while in netrate/ code pthread_create() checked for < 0 on error.

from pthread_create(3):
RETURN VALUES
     If successful, the pthread_create() function will return zero.  Otherwise
     an error number will be returned to indicate the error.

What's in thr_create.c:

                /*
                 * Translate EPROCLIM into well-known POSIX code EAGAIN.
                 */
                if (ret == EPROCLIM)
                        ret = EAGAIN;
[...]
        return (ret);

So.. Slightly modified code (let's call it tools/tools/netrate/juggle/juggle.c):
$ make NTHR=3000
$ ./juggle
[...]
create 2986
create 2987
create 2988
create 2989
create 2990
create 2991
create 2992
create 2993
create 2994
create 2995
create 2996
create 2997
create 2998
create 2999
^C^
[something wrong here!]

Fixed slightly modified code version:
create 1495
create 1496
create 1497
create 1498
create 1499
juggle: main: pthread_create 35: Too many processes

>How-To-Repeat:

>Fix:
Patch attached.


Patch attached with submission follows:

Index: tools/tools/netrate/http/http.c
===================================================================
--- tools/tools/netrate/http/http.c	(revision 203781)
+++ tools/tools/netrate/http/http.c	(working copy)
@@ -308,7 +308,7 @@
 		statep->hwd[i].hwd_count = 0;
 		if (threaded) {
 			if (pthread_create(&statep->hwd[i].hwd_thread, NULL,
-			    http_worker, &statep->hwd[i]) < 0)
+			    http_worker, &statep->hwd[i]) != 0)
 				err(-1, "pthread_create");
 		} else {
 			curthread = i;
Index: tools/tools/netrate/httpd/httpd.c
===================================================================
--- tools/tools/netrate/httpd/httpd.c	(revision 203781)
+++ tools/tools/netrate/httpd/httpd.c	(working copy)
@@ -280,7 +280,7 @@
 	for (i = 0; i < THREADS; i++) {
 		if (threaded) {
 			if (pthread_create(&statep->hts[i].hts_thread, NULL,
-			    httpd_worker, &statep->hts[i]) < 0)
+			    httpd_worker, &statep->hts[i]) != 0)
 				err(-1, "pthread_create");
 		} else {
 			pid = fork();
Index: tools/tools/netrate/juggle/juggle.c
===================================================================
--- tools/tools/netrate/juggle/juggle.c	(revision 203781)
+++ tools/tools/netrate/juggle/juggle.c	(working copy)
@@ -337,7 +337,7 @@
 	if (pthread_mutex_init(&threaded_mtx, NULL) < 0)
 		err(-1, "thread_juggle: pthread_mutex_init");
 
-	if (pthread_create(&thread, NULL, juggling_thread, &fd2) < 0)
+	if (pthread_create(&thread, NULL, juggling_thread, &fd2) != 0)
 		err(-1, "thread_juggle: pthread_create");
 
 	if (pthread_mutex_lock(&threaded_mtx) < 0)


>Release-Note:
>Audit-Trail:

From: pluknet <pluknet@gmail.com>
To: bug-followup@FreeBSD.org, pluknet@gmail.com
Cc:  
Subject: Re: misc/143807: pthread_create retval check fixup
Date: Thu, 11 Feb 2010 20:08:29 +0300

 --0016e6567e562982ec047f563345
 Content-Type: text/plain; charset=ISO-8859-1
 
 [After more thoughts.. ]
 
 all pthread_* functions used in netrate/ have to check for non-zero on
 error, so I prepared the second version.
 (perhaps subj should be called incorrect pthread(3) error checking in
 tools/tools/netrate/..)
 
 -- 
 wbr,
 pluknet
 
 --0016e6567e562982ec047f563345
 Content-Type: text/plain; charset=US-ASCII; name="netrate_pthread_errcode_fixup2.txt"
 Content-Disposition: attachment; 
 	filename="netrate_pthread_errcode_fixup2.txt"
 Content-Transfer-Encoding: base64
 X-Attachment-Id: f_g5jsz6xw0
 
 SW5kZXg6IHRvb2xzL3Rvb2xzL25ldHJhdGUvaHR0cC9odHRwLmMKPT09PT09PT09PT09PT09PT09
 PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PQotLS0gdG9v
 bHMvdG9vbHMvbmV0cmF0ZS9odHRwL2h0dHAuYwkocmV2aXNpb24gMjAzNzgxKQorKysgdG9vbHMv
 dG9vbHMvbmV0cmF0ZS9odHRwL2h0dHAuYwkod29ya2luZyBjb3B5KQpAQCAtMzAwLDcgKzMwMCw3
 IEBACiAKIAlpZiAodGhyZWFkZWQpIHsKIAkJaWYgKHB0aHJlYWRfYmFycmllcl9pbml0KCZzdGF0
 ZXAtPnN0YXJ0X2JhcnJpZXIsIE5VTEwsCi0JCSAgICBudW10aHJlYWRzKSA8IDApCisJCSAgICBu
 dW10aHJlYWRzKSAhPSA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KHB0aHJlYWRfam9pbih0
 aHJlYWQsIE5VTEwpIDwgMCkKKwlpZiAocHRocmVhZF9qb2luKHRocmVhZCwgTlVMTCkgIT0gMCkK
 IAkJZXJyKC0xLCAidGhyZWFkX2p1Z2dsZTogcHRocmVhZF9qb2luIik7CiAKIAl0aW1lc3BlY3N1
 YigmdGZpbmlzaCwgJnRzdGFydCk7Cg==
 --0016e6567e562982ec047f563345--

From: Ruslan Ermilov <ru@FreeBSD.org>
To: pluknet <pluknet@gmail.com>
Cc: bug-followup@FreeBSD.org
Subject: Re: misc/143807: pthread_create retval check fixup
Date: Fri, 12 Feb 2010 18:22:31 +0300

 On Thu, Feb 11, 2010 at 04:53:14PM +0000, pluknet wrote:
 > I faced with incorrect return code checking in netrate's pthread_create(3)
 > calls: pthread_create may return errcode > 0, while in netrate/ code
 > pthread_create() checked for < 0 on error.
 
 The scope of the problem isn't limited just to pthread_create().
 In fact, it seems to be a common problem (I only scanned
 src/tools/):
 
 %%%
 Index: tools/regression/file/newfileops_on_fork/newfileops_on_fork.c
 ===================================================================
 --- tools/regression/file/newfileops_on_fork/newfileops_on_fork.c	(revision 203016)
 +++ tools/regression/file/newfileops_on_fork/newfileops_on_fork.c	(working copy)
 @@ -113,7 +113,7 @@
  		err(-1, "bind");
  	if (listen(listen_fd, -1) <0)
  		err(-1, "listen");
 -	if (pthread_create(&accept_thread, NULL, do_accept, NULL) < 0)
 +	if (pthread_create(&accept_thread, NULL, do_accept, NULL) != 0)
  		err(-1, "pthread_create");
  	sleep(1);	/* Easier than using a CV. */;
  	do_fork();
 Index: tools/regression/gaithrstress/gaithrstress.c
 ===================================================================
 --- tools/regression/gaithrstress/gaithrstress.c	(revision 203016)
 +++ tools/regression/gaithrstress/gaithrstress.c	(working copy)
 @@ -241,7 +241,7 @@
  	fflush(stdout);
  	for (i = 0; i < nworkers; i++) {
  		if (pthread_create(&workers[i].w_thread, NULL, work,
 -		    &workers[i]) == -1)
 +		    &workers[i]) != 0)
  			err(1, "creating worker %u", i);
  		printf("%u%s", i, i == nworkers - 1 ? ".\n" : ", ");
  		fflush(stdout);
 Index: tools/tools/mctest/mctest.cc
 ===================================================================
 --- tools/tools/mctest/mctest.cc	(revision 203016)
 +++ tools/tools/mctest/mctest.cc	(working copy)
 @@ -368,7 +368,7 @@
          args[i].packets = received[i];
          args[i].number = number / clients;
  	args[i].client = base_port + i;
 -	if (pthread_create(&thread[i], NULL, server, &args[i]) < 0) {
 +	if (pthread_create(&thread[i], NULL, server, &args[i]) != 0) {
  	    perror("failed to create server thread");
  	    return -1;
          }
 @@ -393,7 +393,7 @@
      }
  
      for (int i = 0; i < clients; i++) {
 -        if (pthread_join(thread[i], NULL) < 0) {
 +        if (pthread_join(thread[i], NULL) != 0) {
   	    perror("failed to join thread");
   	    return -1;
          }
 Index: tools/tools/netrate/http/http.c
 ===================================================================
 --- tools/tools/netrate/http/http.c	(revision 203016)
 +++ tools/tools/netrate/http/http.c	(working copy)
 @@ -300,15 +300,15 @@
  
  	if (threaded) {
  		if (pthread_barrier_init(&statep->start_barrier, NULL,
 -		    numthreads) < 0)
 -			err(-1, "pthread_mutex_init");
 +		    numthreads) != 0)
 +			err(-1, "pthread_barrier_init");
  	}
  
  	for (i = 0; i < numthreads; i++) {
  		statep->hwd[i].hwd_count = 0;
  		if (threaded) {
  			if (pthread_create(&statep->hwd[i].hwd_thread, NULL,
 -			    http_worker, &statep->hwd[i]) < 0)
 +			    http_worker, &statep->hwd[i]) != 0)
  				err(-1, "pthread_create");
  		} else {
  			curthread = i;
 @@ -339,7 +339,7 @@
  	for (i = 0; i < numthreads; i++) {
  		if (threaded) {
  			if (pthread_join(statep->hwd[i].hwd_thread, NULL)
 -			    < 0)
 +			    != 0)
  				err(-1, "pthread_join");
  		} else {
  			pid = waitpid(statep->hwd[i].hwd_pid, NULL, 0);
 Index: tools/tools/netrate/httpd/httpd.c
 ===================================================================
 --- tools/tools/netrate/httpd/httpd.c	(revision 203016)
 +++ tools/tools/netrate/httpd/httpd.c	(working copy)
 @@ -280,7 +280,7 @@
  	for (i = 0; i < THREADS; i++) {
  		if (threaded) {
  			if (pthread_create(&statep->hts[i].hts_thread, NULL,
 -			    httpd_worker, &statep->hts[i]) < 0)
 +			    httpd_worker, &statep->hts[i]) != 0)
  				err(-1, "pthread_create");
  		} else {
  			pid = fork();
 @@ -299,7 +299,7 @@
  	for (i = 0; i < THREADS; i++) {
  		if (threaded) {
  			if (pthread_join(statep->hts[i].hts_thread, NULL)
 -			    < 0)
 +			    != 0)
  				err(-1, "pthread_join");
  		} else {
  			pid = waitpid(statep->hts[i].hts_pid, NULL, 0);
 Index: tools/tools/netrate/juggle/juggle.c
 ===================================================================
 --- tools/tools/netrate/juggle/juggle.c	(revision 203016)
 +++ tools/tools/netrate/juggle/juggle.c	(working copy)
 @@ -301,15 +301,15 @@
  
  	fd2 = *(int *)arg;
  
 -	if (pthread_mutex_lock(&threaded_mtx) < 0)
 +	if (pthread_mutex_lock(&threaded_mtx) != 0)
  		err(-1, "juggling_thread: pthread_mutex_lock");
  
  	threaded_child_ready = 1;
  
 -	if (pthread_cond_signal(&threaded_cond) < 0)
 +	if (pthread_cond_signal(&threaded_cond) != 0)
  		err(-1, "juggling_thread: pthread_cond_signal");
  
 -	if (pthread_mutex_unlock(&threaded_mtx) < 0)
 +	if (pthread_mutex_unlock(&threaded_mtx) != 0)
  		err(-1, "juggling_thread: pthread_mutex_unlock");
  
  	for (i = 0; i < NUMCYCLES; i++) {
 @@ -334,21 +334,21 @@
  
  	threaded_pipeline = pipeline;
  
 -	if (pthread_mutex_init(&threaded_mtx, NULL) < 0)
 +	if (pthread_mutex_init(&threaded_mtx, NULL) != 0)
  		err(-1, "thread_juggle: pthread_mutex_init");
  
 -	if (pthread_create(&thread, NULL, juggling_thread, &fd2) < 0)
 +	if (pthread_create(&thread, NULL, juggling_thread, &fd2) != 0)
  		err(-1, "thread_juggle: pthread_create");
  
 -	if (pthread_mutex_lock(&threaded_mtx) < 0)
 +	if (pthread_mutex_lock(&threaded_mtx) != 0)
  		err(-1, "thread_juggle: pthread_mutex_lock");
  
  	while (!threaded_child_ready) {
 -		if (pthread_cond_wait(&threaded_cond, &threaded_mtx) < 0)
 +		if (pthread_cond_wait(&threaded_cond, &threaded_mtx) != 0)
  			err(-1, "thread_juggle: pthread_cond_wait");
  	}
  
 -	if (pthread_mutex_unlock(&threaded_mtx) < 0)
 +	if (pthread_mutex_unlock(&threaded_mtx) != 0)
  		err(-1, "thread_juggle: pthread_mutex_unlock");
  
  	if (clock_gettime(CLOCK_REALTIME, &tstart) < 0)
 @@ -369,7 +369,7 @@
  	if (clock_gettime(CLOCK_REALTIME, &tfinish) < 0)
  		err(-1, "thread_juggle: clock_gettime");
  
 -	if (pthread_join(thread, NULL) < 0)
 +	if (pthread_join(thread, NULL) != 0)
  		err(-1, "thread_juggle: pthread_join");
  
  	timespecsub(&tfinish, &tstart);
 %%%
 
 
 Cheers,
 -- 
 Ruslan Ermilov
 ru@FreeBSD.org
 FreeBSD committer

From: pluknet <pluknet@gmail.com>
To: Ruslan Ermilov <ru@freebsd.org>
Cc: bug-followup@freebsd.org
Subject: Re: misc/143807: pthread_create retval check fixup
Date: Fri, 12 Feb 2010 18:53:33 +0300

 On 12 February 2010 18:22, Ruslan Ermilov <ru@freebsd.org> wrote:
 > On Thu, Feb 11, 2010 at 04:53:14PM +0000, pluknet wrote:
 >> I faced with incorrect return code checking in netrate's pthread_create(3)
 >> calls: pthread_create may return errcode > 0, while in netrate/ code
 >> pthread_create() checked for < 0 on error.
 >
 > The scope of the problem isn't limited just to pthread_create().
 > In fact, it seems to be a common problem (I only scanned
 > src/tools/):
 
 Yep, that's what I came with later in PR. Just tried to not affect too
 much at one step.
 
 -- 
 wbr,
 pluknet
State-Changed-From-To: open->closed 
State-Changed-By: ru 
State-Changed-When: Fri Feb 12 16:33:12 UTC 2010 
State-Changed-Why:  
Fixed in 9.0-CURRENT. 


Responsible-Changed-From-To: freebsd-bugs->ru 
Responsible-Changed-By: ru 
Responsible-Changed-When: Fri Feb 12 16:33:12 UTC 2010 
Responsible-Changed-Why:  

http://www.freebsd.org/cgi/query-pr.cgi?pr=143807 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: misc/143807: commit references a PR
Date: Fri, 12 Feb 2010 16:33:17 +0000 (UTC)

 Author: ru
 Date: Fri Feb 12 16:33:03 2010
 New Revision: 203800
 URL: http://svn.freebsd.org/changeset/base/203800
 
 Log:
   Fixed error checking of pthread(3) functions.
   
   PR:		143807
   Submitted by:	pluknet (partly)
 
 Modified:
   head/tools/regression/file/newfileops_on_fork/newfileops_on_fork.c
   head/tools/regression/gaithrstress/gaithrstress.c
   head/tools/tools/mctest/mctest.cc
   head/tools/tools/netrate/http/http.c
   head/tools/tools/netrate/httpd/httpd.c
   head/tools/tools/netrate/juggle/juggle.c
 
 Modified: head/tools/regression/file/newfileops_on_fork/newfileops_on_fork.c
 ==============================================================================
 --- head/tools/regression/file/newfileops_on_fork/newfileops_on_fork.c	Fri Feb 12 15:07:24 2010	(r203799)
 +++ head/tools/regression/file/newfileops_on_fork/newfileops_on_fork.c	Fri Feb 12 16:33:03 2010	(r203800)
 @@ -113,7 +113,7 @@ main(__unused int argc, __unused char *a
  		err(-1, "bind");
  	if (listen(listen_fd, -1) <0)
  		err(-1, "listen");
 -	if (pthread_create(&accept_thread, NULL, do_accept, NULL) < 0)
 +	if (pthread_create(&accept_thread, NULL, do_accept, NULL) != 0)
  		err(-1, "pthread_create");
  	sleep(1);	/* Easier than using a CV. */;
  	do_fork();
 
 Modified: head/tools/regression/gaithrstress/gaithrstress.c
 ==============================================================================
 --- head/tools/regression/gaithrstress/gaithrstress.c	Fri Feb 12 15:07:24 2010	(r203799)
 +++ head/tools/regression/gaithrstress/gaithrstress.c	Fri Feb 12 16:33:03 2010	(r203800)
 @@ -241,7 +241,7 @@ usage:
  	fflush(stdout);
  	for (i = 0; i < nworkers; i++) {
  		if (pthread_create(&workers[i].w_thread, NULL, work,
 -		    &workers[i]) == -1)
 +		    &workers[i]) != 0)
  			err(1, "creating worker %u", i);
  		printf("%u%s", i, i == nworkers - 1 ? ".\n" : ", ");
  		fflush(stdout);
 
 Modified: head/tools/tools/mctest/mctest.cc
 ==============================================================================
 --- head/tools/tools/mctest/mctest.cc	Fri Feb 12 15:07:24 2010	(r203799)
 +++ head/tools/tools/mctest/mctest.cc	Fri Feb 12 16:33:03 2010	(r203800)
 @@ -368,7 +368,7 @@ int source(char *interface, struct in_ad
          args[i].packets = received[i];
          args[i].number = number / clients;
  	args[i].client = base_port + i;
 -	if (pthread_create(&thread[i], NULL, server, &args[i]) < 0) {
 +	if (pthread_create(&thread[i], NULL, server, &args[i]) != 0) {
  	    perror("failed to create server thread");
  	    return -1;
          }
 @@ -393,7 +393,7 @@ int source(char *interface, struct in_ad
      }
  
      for (int i = 0; i < clients; i++) {
 -        if (pthread_join(thread[i], NULL) < 0) {
 +        if (pthread_join(thread[i], NULL) != 0) {
   	    perror("failed to join thread");
   	    return -1;
          }
 
 Modified: head/tools/tools/netrate/http/http.c
 ==============================================================================
 --- head/tools/tools/netrate/http/http.c	Fri Feb 12 15:07:24 2010	(r203799)
 +++ head/tools/tools/netrate/http/http.c	Fri Feb 12 16:33:03 2010	(r203800)
 @@ -300,15 +300,15 @@ main(int argc, char *argv[])
  
  	if (threaded) {
  		if (pthread_barrier_init(&statep->start_barrier, NULL,
 -		    numthreads) < 0)
 -			err(-1, "pthread_mutex_init");
 +		    numthreads) != 0)
 +			err(-1, "pthread_barrier_init");
  	}
  
  	for (i = 0; i < numthreads; i++) {
  		statep->hwd[i].hwd_count = 0;
  		if (threaded) {
  			if (pthread_create(&statep->hwd[i].hwd_thread, NULL,
 -			    http_worker, &statep->hwd[i]) < 0)
 +			    http_worker, &statep->hwd[i]) != 0)
  				err(-1, "pthread_create");
  		} else {
  			curthread = i;
 @@ -339,7 +339,7 @@ main(int argc, char *argv[])
  	for (i = 0; i < numthreads; i++) {
  		if (threaded) {
  			if (pthread_join(statep->hwd[i].hwd_thread, NULL)
 -			    < 0)
 +			    != 0)
  				err(-1, "pthread_join");
  		} else {
  			pid = waitpid(statep->hwd[i].hwd_pid, NULL, 0);
 
 Modified: head/tools/tools/netrate/httpd/httpd.c
 ==============================================================================
 --- head/tools/tools/netrate/httpd/httpd.c	Fri Feb 12 15:07:24 2010	(r203799)
 +++ head/tools/tools/netrate/httpd/httpd.c	Fri Feb 12 16:33:03 2010	(r203800)
 @@ -280,7 +280,7 @@ main(int argc, char *argv[])
  	for (i = 0; i < THREADS; i++) {
  		if (threaded) {
  			if (pthread_create(&statep->hts[i].hts_thread, NULL,
 -			    httpd_worker, &statep->hts[i]) < 0)
 +			    httpd_worker, &statep->hts[i]) != 0)
  				err(-1, "pthread_create");
  		} else {
  			pid = fork();
 @@ -299,7 +299,7 @@ main(int argc, char *argv[])
  	for (i = 0; i < THREADS; i++) {
  		if (threaded) {
  			if (pthread_join(statep->hts[i].hts_thread, NULL)
 -			    < 0)
 +			    != 0)
  				err(-1, "pthread_join");
  		} else {
  			pid = waitpid(statep->hts[i].hts_pid, NULL, 0);
 
 Modified: head/tools/tools/netrate/juggle/juggle.c
 ==============================================================================
 --- head/tools/tools/netrate/juggle/juggle.c	Fri Feb 12 15:07:24 2010	(r203799)
 +++ head/tools/tools/netrate/juggle/juggle.c	Fri Feb 12 16:33:03 2010	(r203800)
 @@ -301,15 +301,15 @@ juggling_thread(void *arg)
  
  	fd2 = *(int *)arg;
  
 -	if (pthread_mutex_lock(&threaded_mtx) < 0)
 +	if (pthread_mutex_lock(&threaded_mtx) != 0)
  		err(-1, "juggling_thread: pthread_mutex_lock");
  
  	threaded_child_ready = 1;
  
 -	if (pthread_cond_signal(&threaded_cond) < 0)
 +	if (pthread_cond_signal(&threaded_cond) != 0)
  		err(-1, "juggling_thread: pthread_cond_signal");
  
 -	if (pthread_mutex_unlock(&threaded_mtx) < 0)
 +	if (pthread_mutex_unlock(&threaded_mtx) != 0)
  		err(-1, "juggling_thread: pthread_mutex_unlock");
  
  	for (i = 0; i < NUMCYCLES; i++) {
 @@ -334,21 +334,21 @@ thread_juggle(int fd1, int fd2, int pipe
  
  	threaded_pipeline = pipeline;
  
 -	if (pthread_mutex_init(&threaded_mtx, NULL) < 0)
 +	if (pthread_mutex_init(&threaded_mtx, NULL) != 0)
  		err(-1, "thread_juggle: pthread_mutex_init");
  
 -	if (pthread_create(&thread, NULL, juggling_thread, &fd2) < 0)
 +	if (pthread_create(&thread, NULL, juggling_thread, &fd2) != 0)
  		err(-1, "thread_juggle: pthread_create");
  
 -	if (pthread_mutex_lock(&threaded_mtx) < 0)
 +	if (pthread_mutex_lock(&threaded_mtx) != 0)
  		err(-1, "thread_juggle: pthread_mutex_lock");
  
  	while (!threaded_child_ready) {
 -		if (pthread_cond_wait(&threaded_cond, &threaded_mtx) < 0)
 +		if (pthread_cond_wait(&threaded_cond, &threaded_mtx) != 0)
  			err(-1, "thread_juggle: pthread_cond_wait");
  	}
  
 -	if (pthread_mutex_unlock(&threaded_mtx) < 0)
 +	if (pthread_mutex_unlock(&threaded_mtx) != 0)
  		err(-1, "thread_juggle: pthread_mutex_unlock");
  
  	if (clock_gettime(CLOCK_REALTIME, &tstart) < 0)
 @@ -369,7 +369,7 @@ thread_juggle(int fd1, int fd2, int pipe
  	if (clock_gettime(CLOCK_REALTIME, &tfinish) < 0)
  		err(-1, "thread_juggle: clock_gettime");
  
 -	if (pthread_join(thread, NULL) < 0)
 +	if (pthread_join(thread, NULL) != 0)
  		err(-1, "thread_juggle: pthread_join");
  
  	timespecsub(&tfinish, &tstart);
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
