| 2122 | } |
| 2123 | |
| 2124 | int Socket::FightAuthentication(int* auth_error) { |
| 2125 | // Use relaxed fence since `bthread_id_trylock' ensures thread safety |
| 2126 | // Here `flag_error' just acts like a cache information |
| 2127 | uint64_t flag_error = _auth_flag_error.load(butil::memory_order_relaxed); |
| 2128 | if (flag_error & AUTH_FLAG) { |
| 2129 | // Already authenticated |
| 2130 | *auth_error = (int32_t)(flag_error & 0xFFFFFFFFul); |
| 2131 | return EINVAL; |
| 2132 | } |
| 2133 | if (0 == bthread_id_trylock(_auth_id, NULL)) { |
| 2134 | // Winner |
| 2135 | return 0; |
| 2136 | } else { |
| 2137 | // Use relaxed fence since `bthread_id_join' has acquire fence to ensure |
| 2138 | // `_auth_flag_error' to be the latest value |
| 2139 | bthread_id_join(_auth_id); |
| 2140 | flag_error = _auth_flag_error.load(butil::memory_order_relaxed); |
| 2141 | *auth_error = (int32_t)(flag_error & 0xFFFFFFFFul); |
| 2142 | return EINVAL; |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | void Socket::SetAuthentication(int error_code) { |
| 2147 | uint64_t expected = 0; |