class V8_BASE_EXPORT Mutex final {
public:
Mutex();
~Mutex();
// Locks the given mutex. If the mutex is currently unlocked, it becomes
// locked and owned by the calling thread, and immediately. If the mutex
// is already locked by another thread, suspends the calling thread until
// the mutex is unlocked.
void Lock();
// Unlocks the given mutex. The mutex is assumed to be locked and owned by
// the calling thread on entrance.
void Unlock();
// Tries to lock the given mutex. Returns whether the mutex was
// successfully locked.
bool TryLock() V8_WARN_UNUSED_RESULT;//注意这个后面V8_WARN_UNUSED_RESULT是什么意思
...
}
看定义:
// Annotate a function indicating the caller must examine the return value.
// Use like:
// int foo() V8_WARN_UNUSED_RESULT;
#if V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT
#define V8_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define V8_WARN_UNUSED_RESULT /* NOT SUPPORTED */
#endif
V8_WARN_UNUSED_RESULT使用attribute宏用来修饰函数这里是强调需要使用函数的返回值,如果没有使用返回值则编译器会报warning,很好的一个写法。
attribute实际是GNU的一个宏指令,声明的时候提供一些属性,让编译阶段做多样化的错误检查和高级优化,可以修饰函数、变量、类型。
再看一个常用属性:deprecated
1)定义一个DECLARE_DEPRECATED宏,使用deprecated属性
#ifndef DECLARE_DEPRECATED
# if defined(OPENSSL_NO_DEPRECATED)
# define DECLARE_DEPRECATED(f)
# else
# define DECLARE_DEPRECATED(f) f;
# ifdef __GNUC__
# if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 0)
# undef DECLARE_DEPRECATED
# define DECLARE_DEPRECATED(f) f __attribute__ ((deprecated));
# endif
# endif
# endif
#endif
2)定义一个宏DEPRECATEDIN_0_9_8,使用DECLARE_DEPRECATED宏
#if OPENSSL_API_COMPAT < 0x00908000L
# define DEPRECATEDIN_0_9_8(f) DECLARE_DEPRECATED(f)
#else
# define DEPRECATEDIN_0_9_8(f)
#endif
3)DEPRECATEDIN_0_9_8修饰函数,表明该版本函数过期
/* Deprecated version */
DEPRECATEDIN_0_9_8(RSA *RSA_generate_key(int bits, unsigned long e, void
(*callback) (int, int, void *),
void *cb_arg));
如果未来版本升级有些函数将不再使用或暴露,就可以加一个类似的宏告诉编译器已经过时,如果使用了,会报过时警告。