Avoid C preprocessor trick where macro has the same name as a function.
authorBen Pfaff <blp@nicira.com>
Mon, 29 Jul 2013 22:24:45 +0000 (15:24 -0700)
committerBen Pfaff <blp@nicira.com>
Mon, 29 Jul 2013 22:24:45 +0000 (15:24 -0700)
In C, one can do preprocessor tricks by making a macro expansion include
the macro's own name.  We actually used this in the tree to automatically
provide function arguments, e.g.:

    int f(int x, const char *file, int line);
    #define f(x) f(x, __FILE__, __LINE__)

...

    f(1);    /* Expands to a call like f(1, __FILE__, __LINE__); */

However it's somewhat confusing, so this commit stops using that trick.

Reported-by: Ed Maste <emaste@freebsd.org>
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Ed Maste <emaste@freebsd.org>
lib/jsonrpc.c
lib/latch.c
lib/latch.h
lib/ovs-thread.c
lib/ovs-thread.h
lib/poll-loop.c
lib/poll-loop.h
lib/timer.c
lib/timer.h

index 6c482c2..a7d89f7 100644 (file)
@@ -352,7 +352,7 @@ void
 jsonrpc_recv_wait(struct jsonrpc *rpc)
 {
     if (rpc->status || rpc->received || !byteq_is_empty(&rpc->input)) {
-        (poll_immediate_wake)(rpc->name);
+        poll_immediate_wake_at(rpc->name);
     } else {
         stream_recv_wait(rpc->stream);
     }
index 9b13006..bf518b9 100644 (file)
@@ -75,9 +75,13 @@ latch_is_set(const struct latch *latch)
     return pfd.revents & POLLIN;
 }
 
-/* Causes the next poll_block() to wake up when 'latch' is set. */
+/* Causes the next poll_block() to wake up when 'latch' is set.
+ *
+ * ('where' is used in debug logging.  Commonly one would use latch_wait() to
+ * automatically provide the caller's source file and line number for
+ * 'where'.) */
 void
-(latch_wait)(const struct latch *latch, const char *where)
+latch_wait_at(const struct latch *latch, const char *where)
 {
-    (poll_fd_wait)(latch->fds[0], POLLIN, where);
+    poll_fd_wait_at(latch->fds[0], POLLIN, where);
 }
index 08f45b1..0b6e8a3 100644 (file)
@@ -36,7 +36,7 @@ bool latch_poll(struct latch *);
 void latch_set(struct latch *);
 
 bool latch_is_set(const struct latch *);
-void latch_wait(const struct latch *, const char *where);
-#define latch_wait(latch) latch_wait(latch, SOURCE_LOCATOR)
+void latch_wait_at(const struct latch *, const char *where);
+#define latch_wait(latch) latch_wait_at(latch, SOURCE_LOCATOR)
 
 #endif /* latch.h */
index 91bb3d9..abadeb9 100644 (file)
@@ -136,9 +136,13 @@ ovsthread_once_done(struct ovsthread_once *once)
 }
 \f
 /* Asserts that the process has not yet created any threads (beyond the initial
- * thread).  */
+ * thread).
+ *
+ * ('where' is used in logging.  Commonly one would use
+ * assert_single_threaded() to automatically provide the caller's source file
+ * and line number for 'where'.) */
 void
-(assert_single_threaded)(const char *where)
+assert_single_threaded_at(const char *where)
 {
     if (multithreaded) {
         VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
@@ -148,9 +152,13 @@ void
 
 /* Forks the current process (checking that this is allowed).  Aborts with
  * VLOG_FATAL if fork() returns an error, and otherwise returns the value
- * returned by fork().  */
+ * returned by fork().
+ *
+ * ('where' is used in logging.  Commonly one would use xfork() to
+ * automatically provide the caller's source file and line number for
+ * 'where'.) */
 pid_t
-(xfork)(const char *where)
+xfork_at(const char *where)
 {
     pid_t pid;
 
index 29a8637..98428fd 100644 (file)
@@ -377,11 +377,11 @@ ovsthread_once_start(struct ovsthread_once *once)
     ((ONCE)->done ? false : ({ OVS_ACQUIRE(ONCE); true; }))
 #endif
 \f
-void assert_single_threaded(const char *where);
-#define assert_single_threaded() assert_single_threaded(SOURCE_LOCATOR)
+void assert_single_threaded_at(const char *where);
+#define assert_single_threaded() assert_single_threaded_at(SOURCE_LOCATOR)
 
-pid_t xfork(const char *where);
-#define xfork() xfork(SOURCE_LOCATOR)
+pid_t xfork_at(const char *where);
+#define xfork() xfork_at(SOURCE_LOCATOR)
 
 void forbid_forking(const char *reason);
 bool may_fork(void);
index 97fc806..0f45d98 100644 (file)
 #include "timeval.h"
 #include "vlog.h"
 
-#undef poll_fd_wait
-#undef poll_timer_wait
-#undef poll_timer_wait_until
-#undef poll_immediate_wake
-
 VLOG_DEFINE_THIS_MODULE(poll_loop);
 
 COVERAGE_DEFINE(poll_fd_wait);
@@ -63,10 +58,11 @@ static struct poll_loop *poll_loop(void);
  * is affected.  The event will need to be re-registered after poll_block() is
  * called if it is to persist.
  *
- * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
- * for more information. */
+ * ('where' is used in debug logging.  Commonly one would use poll_fd_wait() to
+ * automatically provide the caller's source file and line number for
+ * 'where'.) */
 void
-poll_fd_wait(int fd, short int events, const char *where)
+poll_fd_wait_at(int fd, short int events, const char *where)
 {
     struct poll_loop *loop = poll_loop();
 
@@ -93,10 +89,11 @@ poll_fd_wait(int fd, short int events, const char *where)
  * is affected.  The timer will need to be re-registered after poll_block() is
  * called if it is to persist.
  *
- * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
- * for more information. */
+ * ('where' is used in debug logging.  Commonly one would use poll_timer_wait()
+ * to automatically provide the caller's source file and line number for
+ * 'where'.) */
 void
-poll_timer_wait(long long int msec, const char *where)
+poll_timer_wait_at(long long int msec, const char *where)
 {
     long long int now = time_msec();
     long long int when;
@@ -112,7 +109,7 @@ poll_timer_wait(long long int msec, const char *where)
         when = LLONG_MAX;
     }
 
-    poll_timer_wait_until(when, where);
+    poll_timer_wait_until_at(when, where);
 }
 
 /* Causes the following call to poll_block() to wake up when the current time,
@@ -124,10 +121,11 @@ poll_timer_wait(long long int msec, const char *where)
  * is affected.  The timer will need to be re-registered after poll_block() is
  * called if it is to persist.
  *
- * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
- * for more information. */
+ * ('where' is used in debug logging.  Commonly one would use
+ * poll_timer_wait_until() to automatically provide the caller's source file
+ * and line number for 'where'.) */
 void
-poll_timer_wait_until(long long int when, const char *where)
+poll_timer_wait_until_at(long long int when, const char *where)
 {
     struct poll_loop *loop = poll_loop();
     if (when < loop->timeout_when) {
@@ -139,12 +137,13 @@ poll_timer_wait_until(long long int when, const char *where)
 /* Causes the following call to poll_block() to wake up immediately, without
  * blocking.
  *
- * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
- * for more information. */
+ * ('where' is used in debug logging.  Commonly one would use
+ * poll_immediate_wake() to automatically provide the caller's source file and
+ * line number for 'where'.) */
 void
-poll_immediate_wake(const char *where)
+poll_immediate_wake_at(const char *where)
 {
-    poll_timer_wait(0, where);
+    poll_timer_wait_at(0, where);
 }
 
 /* Logs, if appropriate, that the poll loop was awakened by an event
index 6614ebe..0397853 100644 (file)
@@ -44,25 +44,24 @@ extern "C" {
 /* Schedule events to wake up the following poll_block().
  *
  * The poll_loop logs the 'where' argument to each function at "debug" level
- * when an event causes a wakeup.  Ordinarily, it is automatically filled in
- * with the location in the source of the call, and the caller should therefore
- * omit it.  But, if the function you are implementing is very generic, so that
- * its location in the source would not be very helpful for debugging, you can
- * avoid the macro expansion and pass a different argument, e.g.:
- *      (poll_fd_wait)(fd, events, where);
- * See timer_wait() for an example.
- */
-void poll_fd_wait(int fd, short int events, const char *where);
-#define poll_fd_wait(fd, events) poll_fd_wait(fd, events, SOURCE_LOCATOR)
+ * when an event causes a wakeup.  Each of these ways to schedule an event has
+ * a function and a macro wrapper.  The macro version automatically supplies
+ * the source code location of the caller.  The function version allows the
+ * caller to supply a location explicitly, which is useful if the caller's own
+ * caller would be more useful in log output.  See timer_wait_at() for an
+ * example. */
+void poll_fd_wait_at(int fd, short int events, const char *where);
+#define poll_fd_wait(fd, events) poll_fd_wait_at(fd, events, SOURCE_LOCATOR)
 
-void poll_timer_wait(long long int msec, const char *where);
-#define poll_timer_wait(msec) poll_timer_wait(msec, SOURCE_LOCATOR)
+void poll_timer_wait_at(long long int msec, const char *where);
+#define poll_timer_wait(msec) poll_timer_wait_at(msec, SOURCE_LOCATOR)
 
-void poll_timer_wait_until(long long int msec, const char *where);
-#define poll_timer_wait_until(msec) poll_timer_wait_until(msec, SOURCE_LOCATOR)
+void poll_timer_wait_until_at(long long int msec, const char *where);
+#define poll_timer_wait_until(msec)             \
+    poll_timer_wait_until_at(msec, SOURCE_LOCATOR)
 
-void poll_immediate_wake(const char *where);
-#define poll_immediate_wake() poll_immediate_wake(SOURCE_LOCATOR)
+void poll_immediate_wake_at(const char *where);
+#define poll_immediate_wake() poll_immediate_wake_at(SOURCE_LOCATOR)
 
 /* Wait until an event occurs. */
 void poll_block(void);
index e767db6..e90355b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 Nicira, Inc.
+ * Copyright (c) 2011, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,11 +32,15 @@ timer_msecs_until_expired(const struct timer *timer)
     }
 }
 
-/* Causes poll_block() to wake when 'timer' expires. */
+/* Causes poll_block() to wake when 'timer' expires.
+ *
+ * ('where' is used in debug logging.  Commonly one would use timer_wait() to
+ * automatically provide the caller's source file and line number for
+ * 'where'.) */
 void
-(timer_wait)(const struct timer *timer, const char *where)
+timer_wait_at(const struct timer *timer, const char *where)
 {
     if (timer->t < LLONG_MAX) {
-        (poll_timer_wait_until)(timer->t, where);
+        poll_timer_wait_until_at(timer->t, where);
     }
 }
index e9650ad..9afe3b7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 Nicira, Inc.
+ * Copyright (c) 2011, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -27,8 +27,8 @@ struct timer {
 };
 
 long long int timer_msecs_until_expired(const struct timer *);
-void timer_wait(const struct timer *, const char *where);
-#define timer_wait(timer) timer_wait(timer, SOURCE_LOCATOR)
+void timer_wait_at(const struct timer *, const char *where);
+#define timer_wait(timer) timer_wait_at(timer, SOURCE_LOCATOR)
 
 /* Causes 'timer' to expire when 'duration' milliseconds have passed.
  *