Permalink
Browse files

only evaluate argument once in MCHECK macro.

  • Loading branch information...
1 parent 732bed7 commit cf2d41b8a2e31f47401d5c6dc22812fa3f3d750f @chenshuo committed Aug 29, 2013
Showing with 13 additions and 2 deletions.
  1. +1 −0 CMakeLists.txt
  2. +12 −2 muduo/base/Mutex.h
View
@@ -10,6 +10,7 @@ set(CXX_FLAGS
-g
# -DVALGRIND
# -DMUDUO_STD_STRING
+ -DCHECK_PTHREAD_RETURN_VALUE
-D_FILE_OFFSET_BITS=64
-Wall
-Wextra
View
@@ -11,6 +11,8 @@
#include <assert.h>
#include <pthread.h>
+#ifdef CHECK_PTHREAD_RETURN_VALUE
+
#ifdef NDEBUG
__BEGIN_DECLS
extern void __assert_perror_fail (int errnum,
@@ -21,8 +23,16 @@ extern void __assert_perror_fail (int errnum,
__END_DECLS
#endif
-#define MCHECK(errnum) ((errnum) == 0 ? (void) 0 : \
- __assert_perror_fail ((errnum), __FILE__, __LINE__, __func__))
+#define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \
+ if (__builtin_expect(errnum != 0, 1)) \
+ __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);})
+
+#else // CHECK_PTHREAD_RETURN_VALUE
+
+#define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \
+ assert(errnum == 0); (void) errnum;})
+
+#endif // CHECK_PTHREAD_RETURN_VALUE
namespace muduo
{

0 comments on commit cf2d41b

Please sign in to comment.