misc/143807: pthread_create retval check fixup

Ruslan Ermilov ru at FreeBSD.org
Fri Feb 12 15:40:03 UTC 2010


The following reply was made to PR misc/143807; it has been noted by GNATS.

From: Ruslan Ermilov <ru at FreeBSD.org>
To: pluknet <pluknet at gmail.com>
Cc: bug-followup at 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 at FreeBSD.org
 FreeBSD committer


More information about the freebsd-bugs mailing list