Skip to contentSkip to conversation

alsa-utils: Remove usleep usage

PATCH In review Rosen Penev 2 participants 2 messages Sep 2, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alsa-utils: Remove usleep usage

usleep was deprecated in POSIX 2008 and is not available without deprecated APIs enabled in uClibc-ng.

---
 alsactl/daemon.c  | 3 ++-
 alsaloop/pcmjob.c | 3 ++-
 amidi/amidi.c     | 6 ++++--
 bat/bat.c         | 5 +++--
 4 files changed, 11 insertions(+), 6 deletions(-)
alsactl/daemon.c +2 -1
index 1d98caa..3b84cf6 100644
@@ -348,7 +348,8 @@ static int check_another_instance(const char *pidfile)
 	if (pid >= 0) {
 		/* invoke new card rescan */
 		if (kill(pid, SIGUSR1) >= 0) {
-			usleep(1000);
+			struct timespec req = { 0, 1 * 1000 * 1000 };
+			nanosleep(&req, NULL);
 			pid = read_pid_file(pidfile);
 			if (pid >= 0)
 				return 1;
alsaloop/pcmjob.c +2 -1
index 29d1aba..d0347dc 100644
@@ -662,10 +662,11 @@ static int xrun(struct loopback_handle *lhandle)
 
 static int suspend(struct loopback_handle *lhandle)
 {
+	struct timespec req = { 0, 1000};
 	int err;
 
 	while ((err = snd_pcm_resume(lhandle->handle)) == -EAGAIN)
-		usleep(1);
+		nanosleep(&req, NULL);
 	if (err < 0)
 		return xrun(lhandle);
 	return 0;
amidi/amidi.c +4 -2
index b6e05e1..3b2953d 100644
@@ -254,10 +254,12 @@ static int send_midi_interleaved(void)
 			snd_rawmidi_status(output, st);
 			do {
 				/* 320 µs per byte as noted in Page 1 of MIDI spec */
-				usleep((buffer_size - snd_rawmidi_status_get_avail(st)) * 320);
+				struct timespec req = { 0, (buffer_size - snd_rawmidi_status_get_avail(st)) * 320 * 1000};
+				nanosleep(&req, NULL);
 				snd_rawmidi_status(output, st);
 			} while(snd_rawmidi_status_get_avail(st) < buffer_size);
-			usleep(sysex_interval * 1000);
+			struct timespec sysex = { 0, sysex_interval * 1000 * 1000};
+			nanosleep(&sysex, NULL);
 		}
 
 		/* find end of SysEx */
bat/bat.c +3 -2
index 8645770..27e7a34 100644
@@ -176,6 +176,7 @@ static void test_loopback(struct bat *bat)
 	pthread_t capture_id, playback_id;
 	int err;
 	int *thread_result_capture, *thread_result_playback;
+	struct timespec capture_delay = { 0, CAPTURE_DELAY * 1000 * 1000 };
 
 	/* start playback */
 	err = pthread_create(&playback_id, NULL,
@@ -188,7 +189,7 @@ static void test_loopback(struct bat *bat)
 
 	/* TODO: use a pipe to signal stream start etc - i.e. to sync threads */
 	/* Let some time for playing something before capturing */
-	usleep(CAPTURE_DELAY * 1000);
+	nanosleep(&capture_delay, NULL);
 
 	/* start capture */
 	err = pthread_create(&capture_id, NULL, (void *) bat->capture.fct, bat);
@@ -691,7 +692,7 @@ int main(int argc, char *argv[])
 			}
 
 			/* Waiting 500ms and start the next round */
-			usleep(CAPTURE_DELAY * 1000);
+			nanosleep(&capture_delay, NULL);
 		}
 		goto out;
 	}
Signature · 6 lines
2.17.1

_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#1 lore raw
Quoted 5 lines · Rosen Penev

usleep was deprecated in POSIX 2008 and is not available without deprecated APIs enabled in uClibc-ng.

Can we check the presence in configure script and add a compat usleep() wrapper instead? Replacing each place with the open code using nanosleep() is way too ugly, IMO.

thanks,

Takashi

Quoted 94 lines
---
 alsactl/daemon.c  | 3 ++-
 alsaloop/pcmjob.c | 3 ++-
 amidi/amidi.c     | 6 ++++--
 bat/bat.c         | 5 +++--
 4 files changed, 11 insertions(+), 6 deletions(-)
alsactl/daemon.c +2 -1
index 1d98caa..3b84cf6 100644
@@ -348,7 +348,8 @@ static int check_another_instance(const char *pidfile)
 	if (pid >= 0) {
 		/* invoke new card rescan */
 		if (kill(pid, SIGUSR1) >= 0) {
-			usleep(1000);
+			struct timespec req = { 0, 1 * 1000 * 1000 };
+			nanosleep(&req, NULL);
 			pid = read_pid_file(pidfile);
 			if (pid >= 0)
 				return 1;
alsaloop/pcmjob.c +2 -1
index 29d1aba..d0347dc 100644
@@ -662,10 +662,11 @@ static int xrun(struct loopback_handle *lhandle)
 
 static int suspend(struct loopback_handle *lhandle)
 {
+	struct timespec req = { 0, 1000};
 	int err;
 
 	while ((err = snd_pcm_resume(lhandle->handle)) == -EAGAIN)
-		usleep(1);
+		nanosleep(&req, NULL);
 	if (err < 0)
 		return xrun(lhandle);
 	return 0;
amidi/amidi.c +4 -2
index b6e05e1..3b2953d 100644
@@ -254,10 +254,12 @@ static int send_midi_interleaved(void)
 			snd_rawmidi_status(output, st);
 			do {
 				/* 320 µs per byte as noted in Page 1 of MIDI spec */
-				usleep((buffer_size - snd_rawmidi_status_get_avail(st)) * 320);
+				struct timespec req = { 0, (buffer_size - snd_rawmidi_status_get_avail(st)) * 320 * 1000};
+				nanosleep(&req, NULL);
 				snd_rawmidi_status(output, st);
 			} while(snd_rawmidi_status_get_avail(st) < buffer_size);
-			usleep(sysex_interval * 1000);
+			struct timespec sysex = { 0, sysex_interval * 1000 * 1000};
+			nanosleep(&sysex, NULL);
 		}
 
 		/* find end of SysEx */
bat/bat.c +3 -2
index 8645770..27e7a34 100644
@@ -176,6 +176,7 @@ static void test_loopback(struct bat *bat)
 	pthread_t capture_id, playback_id;
 	int err;
 	int *thread_result_capture, *thread_result_playback;
+	struct timespec capture_delay = { 0, CAPTURE_DELAY * 1000 * 1000 };
 
 	/* start playback */
 	err = pthread_create(&playback_id, NULL,
@@ -188,7 +189,7 @@ static void test_loopback(struct bat *bat)
 
 	/* TODO: use a pipe to signal stream start etc - i.e. to sync threads */
 	/* Let some time for playing something before capturing */
-	usleep(CAPTURE_DELAY * 1000);
+	nanosleep(&capture_delay, NULL);
 
 	/* start capture */
 	err = pthread_create(&capture_id, NULL, (void *) bat->capture.fct, bat);
@@ -691,7 +692,7 @@ int main(int argc, char *argv[])
 			}
 
 			/* Waiting 500ms and start the next round */
-			usleep(CAPTURE_DELAY * 1000);
+			nanosleep(&capture_delay, NULL);
 		}
 		goto out;
 	}
Signature · 6 lines
2.17.1

_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel

_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel