| 159 | } |
| 160 | |
| 161 | void EndRunningUserCodeInPool(void (*fn)(void*), void* arg) { |
| 162 | InitUserCodeBackupPoolOnceOrDie(); |
| 163 | |
| 164 | g_usercode_inplace.fetch_sub(1, butil::memory_order_relaxed); |
| 165 | |
| 166 | // Not enough idle workers, run the code in backup threads to prevent |
| 167 | // all workers from being blocked and no responses will be processed |
| 168 | // anymore (deadlocked). |
| 169 | const UserCode usercode = { fn, arg }; |
| 170 | pthread_mutex_lock(&s_usercode_mutex); |
| 171 | s_usercode_pool->queue.push_back(usercode); |
| 172 | // If the queue has too many items, we can't drop the user code |
| 173 | // directly which often must be run, for example: client-side done. |
| 174 | // The solution is that we set a mark which is not cleared before |
| 175 | // queue becomes short again. RPC code checks the mark before |
| 176 | // submitting tasks that may generate more user code. |
| 177 | if ((int)s_usercode_pool->queue.size() >= |
| 178 | (FLAGS_usercode_backup_threads * |
| 179 | FLAGS_max_pending_in_each_backup_thread)) { |
| 180 | g_too_many_usercode = true; |
| 181 | } |
| 182 | pthread_mutex_unlock(&s_usercode_mutex); |
| 183 | pthread_cond_signal(&s_usercode_cond); |
| 184 | } |
| 185 | |
| 186 | } // namespace brpc |
no test coverage detected