| 398 | static pthread_mutex_t consumer_sqlthds_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 399 | |
| 400 | static int luabb_trigger_register(Lua L, trigger_reg_t *reg, int register_timeoutms) |
| 401 | { |
| 402 | int rc; |
| 403 | SP sp = getsp(L); |
| 404 | sp->num_instructions = 0; |
| 405 | int retry = round(register_timeoutms / 1000.0); |
| 406 | if (retry <= 0) { |
| 407 | retry = 1; |
| 408 | } |
| 409 | |
| 410 | struct thdpool *pool = get_sql_pool(sp->clnt); |
| 411 | Pthread_mutex_lock(&consumer_sqlthds_mutex); |
| 412 | thdpool_add_waitthd(pool); |
| 413 | Pthread_mutex_unlock(&consumer_sqlthds_mutex); |
| 414 | |
| 415 | while ((rc = trigger_register_req(reg)) != CDB2_TRIG_REQ_SUCCESS) { |
| 416 | /* trigger_register_req() can take up to 1 second. Tick up immediately |
| 417 | after this so that it's guaranteed that the appsock thread observes |
| 418 | a good query state for the next heartbeat. */ |
| 419 | comdb2_sql_tick_no_recover_deadlock(); |
| 420 | if (register_timeoutms) { |
| 421 | if (retry == 0) { |
| 422 | luabb_error(L, sp, "trigger:%s registration timeout %dms", reg->spname, register_timeoutms); |
| 423 | rc = -2; |
| 424 | goto out; |
| 425 | } |
| 426 | --retry; |
| 427 | } |
| 428 | if (check_retry_conditions(L, reg, 1) != 0) { |
| 429 | rc = luabb_error(L, sp, sp->error); |
| 430 | goto out; |
| 431 | } |
| 432 | if (rc != NET_SEND_FAIL_TIMEOUT) { |
| 433 | sleep(1); |
| 434 | comdb2_sql_tick_no_recover_deadlock(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | out: |
| 439 | Pthread_mutex_lock(&consumer_sqlthds_mutex); |
| 440 | thdpool_remove_waitthd(pool); |
| 441 | Pthread_mutex_unlock(&consumer_sqlthds_mutex); |
| 442 | |
| 443 | return rc; |
| 444 | } |
| 445 | |
| 446 | static void luabb_trigger_unregister(Lua L, dbconsumer_t *q) |
| 447 | { |
no test coverage detected