MySQL 源码中的 ut_a 、 ut_ad

ut0dbg.h 中,可以看到如下宏

#ifdef UNIV_DEBUG
/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */
#define ut_ad(EXPR) ut_a(EXPR)
/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */
#define ut_d(EXPR) EXPR
#else
/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */
#define ut_ad(EXPR)
/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */
#define ut_d(EXPR)
#endif

可见, ut_ad 是在开启 UNIV_DEBUG 下的 ut_aut_d 是开启 UNIV_DEBUG 下才执行 EXPR;当 UNIV_DEBUG 关闭时, EXPR 均不执行。

/** Abort execution if EXPR does not evaluate to nonzero.
@param EXPR assertion expression that should hold */
#define ut_a(EXPR)                                               \
  do {                                                           \
    if (UNIV_UNLIKELY(!(ulint)(EXPR))) {                         \
      ut_dbg_assertion_failed(#EXPR, __FILE__, (ulint)__LINE__); \
    }                                                            \
  } while (0)
/* Tell the compiler that cond is unlikely to hold */
#define UNIV_UNLIKELY(cond) UNIV_EXPECT(cond, FALSE)

例子如下,在开启 UNIV_DEBUG 时,会执行 lock_mutex_own()trx_mutex_own(),并且会在其返回值为 0 时报错。

const trx_t *DeadlockChecker::check_and_resolve(const lock_t *lock,
                                                trx_t *trx) {
  ut_ad(lock_mutex_own());
  ut_ad(trx_mutex_own(trx));
  check_trx_state(trx);
  ut_ad(!srv_read_only_mode);
posted @ 2020-02-27 15:20  菁芜  阅读(976)  评论(0)    收藏  举报
编辑推荐:
· 别做抢活的导演:代码中的抽象层次原则
· 从 Redis 客户端超时到 .NET 线程池挑战
· C23和C++26的#embed嵌入资源指南
· 「EF Core」框架是如何识别实体类的属性和主键的
· 独立开发,这条路可行吗?
阅读排行:
· 阿里巴巴为什么禁止超过3张表join?
· 博客园众包线下沙龙第1期:云栖开发者基地,共建技术新天地
· 让 AI 帮我部署网站,太方便了!
· 别做抢活的导演:代码中的抽象层次原则
· .NET周刊【7月第1期 2025-07-06】
点击右上角即可分享
微信分享提示