[master] a5a3c14d8 cocci: Patch to migrate away from usleep()

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Feb 5 16:56:06 UTC 2025


commit a5a3c14d88f9ad7e169afaa5a2f51b32b764186e
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Wed Feb 5 17:37:27 2025 +0100

    cocci: Patch to migrate away from usleep()

diff --git a/tools/coccinelle/usleep.cocci b/tools/coccinelle/usleep.cocci
new file mode 100644
index 000000000..d8d245bd5
--- /dev/null
+++ b/tools/coccinelle/usleep.cocci
@@ -0,0 +1,45 @@
+/* This patch turns hard-coded usleep() usage into a VTIM_sleep() equivalent.
+ *
+ * Unfortunately, if a file contains the same usleep() call twice, each call
+ * site will be substituted twice.
+ *
+ * This code:
+ *
+ *     usleep(10000);
+ *     usleep(10000);
+ *
+ * Will turn into this:
+ *
+ *     VTIM_sleep(0.01)VTIM_sleep(0.01);
+ *     VTIM_sleep(0.01)VTIM_sleep(0.01);
+ *
+ * Fortunately, it does not compile and cannot go unnoticed.
+ */
+
+ at found@
+constant int usec;
+@@
+
+usleep(usec)
+
+ at script:python conv@
+usec << found.usec;
+sec;
+@@
+
+coccinelle.sec = cocci.make_expr("{:g}".format(int(usec) / 1000000.0))
+
+ at uncast@
+constant int found.usec;
+@@
+
+- (void)usleep(usec)
++ usleep(usec)
+
+ at replace@
+constant int found.usec;
+expression conv.sec;
+@@
+
+-  usleep(usec)
+++ VTIM_sleep(sec)


More information about the varnish-commit mailing list