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;//注意這個(gè)后面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宏用來(lái)修飾函數(shù)這里是強(qiáng)調(diào)需要使用函數(shù)的返回值溉旋,如果沒(méi)有使用返回值則編譯器會(huì)報(bào)warning观腊,很好的一個(gè)寫(xiě)法。
attribute實(shí)際是GNU的一個(gè)宏指令监憎,聲明的時(shí)候提供一些屬性婶溯,讓編譯階段做多樣化的錯(cuò)誤檢查和高級(jí)優(yōu)化,可以修飾函數(shù)迄委、變量类少、類型。
再看一個(gè)常用屬性:deprecated
1)定義一個(gè)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)定義一個(gè)宏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修飾函數(shù),表明該版本函數(shù)過(guò)期
/* Deprecated version */
DEPRECATEDIN_0_9_8(RSA *RSA_generate_key(int bits, unsigned long e, void
(*callback) (int, int, void *),
void *cb_arg));
如果未來(lái)版本升級(jí)有些函數(shù)將不再使用或暴露即彪,就可以加一個(gè)類似的宏告訴編譯器已經(jīng)過(guò)時(shí)活尊,如果使用了漏益,會(huì)報(bào)過(guò)時(shí)警告绰疤。