Skip to contentSkip to conversation

test/pcm-multi-thread: Remove pthread_cancel, add snd_pcm_close

PATCH In review David Henningsson 2 participants 2 messages Sep 1, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test/pcm-multi-thread: Remove pthread_cancel, add snd_pcm_close

Because
1) Apparently, our thread cancellation policy is "don't even think about it" and we shouldn't break our own rules
2) It is good practice to close pcm after usage

---
 test/pcm-multi-thread.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
test/pcm-multi-thread.c +6 -3
index da1b87c..26de7db 100644
@@ -219,18 +219,22 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	if (setup_params())
+	if (setup_params()) {
+		snd_pcm_close(pcm);
 		return 1;
+	}
 
 	buf = calloc(1, snd_pcm_format_size(format, bufsize) * channels);
 	if (!buf) {
 		fprintf(stderr, "cannot alloc buffer\n");
+		snd_pcm_close(pcm);
 		return 1;
 	}
 
 	for (i = 0; i < num_threads; i++) {
 		if (pthread_create(&peeper_threads[i], NULL, peeper, (void *)(long)i)) {
 			fprintf(stderr, "pthread_create error\n");
+			snd_pcm_close(pcm);
 			return 1;
 		}
 	}
@@ -255,9 +259,8 @@ int main(int argc, char **argv)
 
 	running = 0;
 	for (i = 0; i < num_threads; i++)
-		pthread_cancel(peeper_threads[i]);
-	for (i = 0; i < num_threads; i++)
 		pthread_join(peeper_threads[i], NULL);
 
+	snd_pcm_close(pcm);
 	return 1;
 }
Signature · 1 line
1.9.1
#1 lore raw

test/pcm-multi-thread.c int main(int argc, char **argv) · line 240

Quoted 10 lines · David Henningsson

2) It is good practice to close pcm after usage

[...]

test/pcm-multi-thread.c +1 -0
 	for (i = 0; i < num_threads; i++) {
 		if (pthread_create(&peeper_threads[i], NULL, peeper, (void *)(long)i)) {
 			fprintf(stderr, "pthread_create error\n");
+			snd_pcm_close(pcm);
 			return 1;
 		}
 	}

Any previously created threads are already running. Calling some snd_* function on an already-closed device is worse than letting the OS clean up after the program exits.

If we'd wanted to be strictly correct, we would have to send a message to the threads and wait for them to exit (as the cleanup code below already does).

Quoted 1 line

running = 0;

This variable should be volatile.

test/pcm-multi-thread.c int main(int argc, char **argv) · line 262

Quoted 4 lines
test/pcm-multi-thread.c +0 -2
-	for (i = 0; i < num_threads; i++)
-		pthread_cancel(peeper_threads[i]);
 	for (i = 0; i < num_threads; i++)
 		pthread_join(peeper_threads[i], NULL);

Regards,
Clemens