brpc futex應(yīng)用

分析環(huán)境為centos惧辈,還有其他的futex_wait_private實(shí)現(xiàn)刨晴,這里暫時(shí)忽略。

brpc中bthread主線程的等待操作wait_task使用futex_wait_private實(shí)現(xiàn)。

futex_wait_private的具體實(shí)現(xiàn)使用了系統(tǒng)調(diào)用SYS_futex染厅。
SYS_futex的優(yōu)勢(shì)從名字“futex - fast user-space locking”就可以看的出來,詳細(xì)實(shí)現(xiàn)相關(guān)后續(xù)自己閱讀了解泉哈,暫時(shí)先明白用法蛉幸。

futex_wait_private等在brpc中實(shí)現(xiàn)

inline int futex_wait_private(
    void* addr1, int expected, const timespec* timeout) {
    return syscall(SYS_futex, addr1, (FUTEX_WAIT | FUTEX_PRIVATE_FLAG),
                   expected, timeout, NULL, 0);
}

inline int futex_wake_private(void* addr1, int nwake) {
    return syscall(SYS_futex, addr1, (FUTEX_WAKE | FUTEX_PRIVATE_FLAG),
                   nwake, NULL, NULL, 0);
}

inline int futex_requeue_private(void* addr1, int nwake, void* addr2) {
    return syscall(SYS_futex, addr1, (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG),
                   nwake, NULL, addr2, 0);
}

futex說明:

NAME   futex - fast user-space locking

long futex(uint32_t *uaddr, int futex_op, uint32_t val,
                 const struct timespec *timeout,   /* or: uint32_t val2 */
                 uint32_t *uaddr2, uint32_t val3);
FUTEX_PRIVATE_FLAG (since Linux 2.6.22)
              This option bit can be employed with all futex operations.
              It tells the kernel that the futex is process-private and
              not shared with another process (i.e., it is being used
              for synchronization only between threads of the same
              process).  This allows the kernel to make some additional
              performance optimizations.

              As a convenience, <linux/futex.h> defines a set of
              constants with the suffix _PRIVATE that are equivalents of
              all of the operations listed below, but with the
              FUTEX_PRIVATE_FLAG ORed into the constant value.  Thus,
              there are FUTEX_WAIT_PRIVATE, FUTEX_WAKE_PRIVATE, and so
              on.
FUTEX_WAIT (since Linux 2.6.0)
              This operation tests that the value at the futex word
              pointed to by the address uaddr still contains the
              expected value val, and if so, then sleeps waiting for a
              FUTEX_WAKE operation on the futex word.  The load of the
              value of the futex word is an atomic memory access (i.e.,
              using atomic machine instructions of the respective
              architecture).  This load, the comparison with the
              expected value, and starting to sleep are performed
              atomically and totally ordered with respect to other futex
              operations on the same futex word.  If the thread starts
              to sleep, it is considered a waiter on this futex word.
              If the futex value does not match val, then the call fails
              immediately with the error EAGAIN.

              The purpose of the comparison with the expected value is
              to prevent lost wake-ups.  If another thread changed the
              value of the futex word after the calling thread decided
              to block based on the prior value, and if the other thread
              executed a FUTEX_WAKE operation (or similar wake-up) after
              the value change and before this FUTEX_WAIT operation,
              then the calling thread will observe the value change and
              will not start to sleep.
FUTEX_WAKE (since Linux 2.6.0)
              This operation wakes at most val of the waiters that are
              waiting (e.g., inside FUTEX_WAIT) on the futex word at the
              address uaddr.  Most commonly, val is specified as either
              1 (wake up a single waiter) or INT_MAX (wake up all
              waiters).  No guarantee is provided about which waiters
              are awoken (e.g., a waiter with a higher scheduling
              priority is not guaranteed to be awoken in preference to a
              waiter with a lower priority).

              The arguments timeout, uaddr2, and val3 are ignored.
FUTEX_REQUEUE (since Linux 2.6.0)
              This operation performs the same task as FUTEX_CMP_REQUEUE
              (see below), except that no check is made using the value
              in val3.  (The argument val3 is ignored.)

FUTEX_CMP_REQUEUE (since Linux 2.6.7)
              This operation first checks whether the location uaddr
              still contains the value val3.  If not, the operation
              fails with the error EAGAIN.  Otherwise, the operation
              wakes up a maximum of val waiters that are waiting on the
              futex at uaddr.  If there are more than val waiters, then
              the remaining waiters are removed from the wait queue of
              the source futex at uaddr and added to the wait queue of
              the target futex at uaddr2.  The val2 argument specifies
              an upper limit on the number of waiters that are requeued
              to the futex at uaddr2.

內(nèi)容參考:https://man7.org/linux/man-pages/man2/futex.2.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市丛晦,隨后出現(xiàn)的幾起案子奕纫,更是在濱河造成了極大的恐慌,老刑警劉巖烫沙,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匹层,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡锌蓄,警方通過查閱死者的電腦和手機(jī)升筏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瘸爽,“玉大人您访,你說我怎么就攤上這事〖艟觯” “怎么了灵汪?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵檀训,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我享言,道長(zhǎng)峻凫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任览露,我火速辦了婚禮荧琼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘肛循。我一直安慰自己铭腕,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布多糠。 她就那樣靜靜地躺著累舷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪夹孔。 梳的紋絲不亂的頭發(fā)上被盈,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音搭伤,去河邊找鬼只怎。 笑死,一個(gè)胖子當(dāng)著我的面吹牛怜俐,可吹牛的內(nèi)容都是我干的身堡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼拍鲤,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼贴谎!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起季稳,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤擅这,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后景鼠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體仲翎,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年铛漓,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了溯香。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡浓恶,死狀恐怖逐哈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情问顷,我是刑警寧澤昂秃,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布禀梳,位于F島的核電站,受9級(jí)特大地震影響肠骆,放射性物質(zhì)發(fā)生泄漏算途。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一蚀腿、第九天 我趴在偏房一處隱蔽的房頂上張望嘴瓤。 院中可真熱鬧,春花似錦莉钙、人聲如沸廓脆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽停忿。三九已至,卻和暖如春蚊伞,著一層夾襖步出監(jiān)牢的瞬間席赂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工时迫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留颅停,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓掠拳,卻偏偏與公主長(zhǎng)得像癞揉,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子溺欧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容