Changeset 4360 for pjproject/trunk/pjnath/src/pjnath/ice_strans.c
- Timestamp:
- Feb 21, 2013 11:26:35 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjnath/ice_strans.c
r4314 r4360 127 127 128 128 /* Forward decls */ 129 static void ice_st_on_destroy(void *obj); 129 130 static void destroy_ice_st(pj_ice_strans *ice_st); 130 131 #define ice_st_perror(ice_st,msg,rc) pjnath_perror(ice_st->obj_name,msg,rc) 131 132 static void sess_init_update(pj_ice_strans *ice_st); 132 133 static void sess_add_ref(pj_ice_strans *ice_st);134 static pj_bool_t sess_dec_ref(pj_ice_strans *ice_st);135 133 136 134 /** … … 173 171 pj_ice_strans_cfg cfg; /**< Configuration. */ 174 172 pj_ice_strans_cb cb; /**< Application callback. */ 175 pj_ lock_t *init_lock; /**< Initialization mutex.*/173 pj_grp_lock_t *grp_lock; /**< Group lock. */ 176 174 177 175 pj_ice_strans_state state; /**< Session state. */ … … 184 182 pj_timer_entry ka_timer; /**< STUN keep-alive timer. */ 185 183 186 pj_atomic_t *busy_cnt; /**< To prevent destroy */187 184 pj_bool_t destroy_req;/**< Destroy has been called? */ 188 185 pj_bool_t cb_called; /**< Init error callback called?*/ … … 552 549 pj_log_push_indent(); 553 550 554 pj_ice_strans_cfg_copy(pool, &ice_st->cfg, cfg); 555 pj_memcpy(&ice_st->cb, cb, sizeof(*cb)); 556 557 status = pj_atomic_create(pool, 0, &ice_st->busy_cnt); 551 status = pj_grp_lock_create(pool, NULL, &ice_st->grp_lock); 558 552 if (status != PJ_SUCCESS) { 559 destroy_ice_st(ice_st); 560 return status; 561 } 562 563 status = pj_lock_create_recursive_mutex(pool, ice_st->obj_name, 564 &ice_st->init_lock); 565 if (status != PJ_SUCCESS) { 566 destroy_ice_st(ice_st); 553 pj_pool_release(pool); 567 554 pj_log_pop_indent(); 568 555 return status; 569 556 } 557 558 pj_grp_lock_add_ref(ice_st->grp_lock); 559 pj_grp_lock_add_handler(ice_st->grp_lock, pool, ice_st, 560 &ice_st_on_destroy); 561 562 pj_ice_strans_cfg_copy(pool, &ice_st->cfg, cfg); 563 ice_st->cfg.stun.cfg.grp_lock = ice_st->grp_lock; 564 ice_st->cfg.turn.cfg.grp_lock = ice_st->grp_lock; 565 pj_memcpy(&ice_st->cb, cb, sizeof(*cb)); 570 566 571 567 ice_st->comp_cnt = comp_cnt; … … 579 575 * called before we finish initialization. 580 576 */ 581 pj_ lock_acquire(ice_st->init_lock);577 pj_grp_lock_acquire(ice_st->grp_lock); 582 578 583 579 for (i=0; i<comp_cnt; ++i) { 584 580 status = create_comp(ice_st, i+1); 585 581 if (status != PJ_SUCCESS) { 586 pj_ lock_release(ice_st->init_lock);582 pj_grp_lock_release(ice_st->grp_lock); 587 583 destroy_ice_st(ice_st); 588 584 pj_log_pop_indent(); … … 592 588 593 589 /* Done with initialization */ 594 pj_ lock_release(ice_st->init_lock);595 596 PJ_LOG(4,(ice_st->obj_name, "ICE stream transport created"));590 pj_grp_lock_release(ice_st->grp_lock); 591 592 PJ_LOG(4,(ice_st->obj_name, "ICE stream transport %p created", ice_st)); 597 593 598 594 *p_ice_st = ice_st; … … 606 602 } 607 603 604 /* REALLY destroy ICE */ 605 static void ice_st_on_destroy(void *obj) 606 { 607 pj_ice_strans *ice_st = (pj_ice_strans*)obj; 608 609 PJ_LOG(4,(ice_st->obj_name, "ICE stream transport %p destroyed", obj)); 610 611 /* Done */ 612 pj_pool_release(ice_st->pool); 613 } 614 608 615 /* Destroy ICE */ 609 616 static void destroy_ice_st(pj_ice_strans *ice_st) … … 611 618 unsigned i; 612 619 613 PJ_LOG(5,(ice_st->obj_name, "ICE stream transport destroying..")); 620 PJ_LOG(5,(ice_st->obj_name, "ICE stream transport %p destroy request..", 621 ice_st)); 614 622 pj_log_push_indent(); 623 624 pj_grp_lock_acquire(ice_st->grp_lock); 625 626 if (ice_st->destroy_req) { 627 pj_grp_lock_release(ice_st->grp_lock); 628 return; 629 } 630 631 ice_st->destroy_req = PJ_TRUE; 615 632 616 633 /* Destroy ICE if we have ICE */ … … 624 641 if (ice_st->comp[i]) { 625 642 if (ice_st->comp[i]->stun_sock) { 626 pj_stun_sock_set_user_data(ice_st->comp[i]->stun_sock, NULL);627 643 pj_stun_sock_destroy(ice_st->comp[i]->stun_sock); 628 644 ice_st->comp[i]->stun_sock = NULL; 629 645 } 630 646 if (ice_st->comp[i]->turn_sock) { 631 pj_turn_sock_set_user_data(ice_st->comp[i]->turn_sock, NULL);632 647 pj_turn_sock_destroy(ice_st->comp[i]->turn_sock); 633 648 ice_st->comp[i]->turn_sock = NULL; … … 635 650 } 636 651 } 637 ice_st->comp_cnt = 0; 638 639 /* Destroy mutex */ 640 if (ice_st->init_lock) { 641 pj_lock_acquire(ice_st->init_lock); 642 pj_lock_release(ice_st->init_lock); 643 pj_lock_destroy(ice_st->init_lock); 644 ice_st->init_lock = NULL; 645 } 646 647 /* Destroy reference counter */ 648 if (ice_st->busy_cnt) { 649 pj_assert(pj_atomic_get(ice_st->busy_cnt)==0); 650 pj_atomic_destroy(ice_st->busy_cnt); 651 ice_st->busy_cnt = NULL; 652 } 653 654 PJ_LOG(4,(ice_st->obj_name, "ICE stream transport destroyed")); 655 656 /* Done */ 657 pj_pool_release(ice_st->pool); 652 653 pj_grp_lock_dec_ref(ice_st->grp_lock); 654 pj_grp_lock_release(ice_st->grp_lock); 655 658 656 pj_log_pop_indent(); 659 657 } … … 740 738 PJ_DEF(pj_status_t) pj_ice_strans_destroy(pj_ice_strans *ice_st) 741 739 { 742 PJ_ASSERT_RETURN(ice_st, PJ_EINVAL); 743 744 sess_add_ref(ice_st); 745 ice_st->destroy_req = PJ_TRUE; 746 if (sess_dec_ref(ice_st)) { 747 PJ_LOG(5,(ice_st->obj_name, 748 "ICE strans object is busy, will destroy later")); 749 return PJ_EPENDING; 750 } 751 740 destroy_ice_st(ice_st); 752 741 return PJ_SUCCESS; 753 742 } 754 743 755 756 /*757 * Increment busy counter.758 */759 static void sess_add_ref(pj_ice_strans *ice_st)760 {761 pj_atomic_inc(ice_st->busy_cnt);762 }763 764 /*765 * Decrement busy counter. If the counter has reached zero and destroy766 * has been requested, destroy the object and return FALSE.767 */768 static pj_bool_t sess_dec_ref(pj_ice_strans *ice_st)769 {770 int count = pj_atomic_dec_and_get(ice_st->busy_cnt);771 pj_assert(count >= 0);772 if (count==0 && ice_st->destroy_req) {773 destroy_ice_st(ice_st);774 return PJ_FALSE;775 } else {776 return PJ_TRUE;777 }778 }779 744 780 745 /* … … 841 806 status = pj_ice_sess_create(&ice_st->cfg.stun_cfg, ice_st->obj_name, role, 842 807 ice_st->comp_cnt, &ice_cb, 843 local_ufrag, local_passwd, &ice_st->ice); 808 local_ufrag, local_passwd, 809 ice_st->grp_lock, 810 &ice_st->ice); 844 811 if (status != PJ_SUCCESS) 845 812 return status; … … 1256 1223 unsigned msec; 1257 1224 1258 sess_add_ref(ice_st);1225 pj_grp_lock_add_ref(ice_st->grp_lock); 1259 1226 1260 1227 pj_gettimeofday(&t); … … 1338 1305 } 1339 1306 1340 sess_dec_ref(ice_st);1307 pj_grp_lock_dec_ref(ice_st->grp_lock); 1341 1308 } 1342 1309 … … 1427 1394 ice_st = comp->ice_st; 1428 1395 1429 sess_add_ref(ice_st);1396 pj_grp_lock_add_ref(ice_st->grp_lock); 1430 1397 1431 1398 if (ice_st->ice == NULL) { … … 1452 1419 } 1453 1420 1454 return sess_dec_ref(ice_st);1421 return pj_grp_lock_dec_ref(ice_st->grp_lock) ? PJ_FALSE : PJ_TRUE; 1455 1422 } 1456 1423 … … 1483 1450 ice_st = comp->ice_st; 1484 1451 1485 sess_add_ref(ice_st);1452 pj_grp_lock_add_ref(ice_st->grp_lock); 1486 1453 1487 1454 /* Wait until initialization completes */ 1488 pj_ lock_acquire(ice_st->init_lock);1455 pj_grp_lock_acquire(ice_st->grp_lock); 1489 1456 1490 1457 /* Find the srflx cancidate */ … … 1496 1463 } 1497 1464 1498 pj_ lock_release(ice_st->init_lock);1465 pj_grp_lock_release(ice_st->grp_lock); 1499 1466 1500 1467 /* It is possible that we don't have srflx candidate even though this … … 1503 1470 */ 1504 1471 if (cand == NULL) { 1505 return sess_dec_ref(ice_st);1472 return pj_grp_lock_dec_ref(ice_st->grp_lock) ? PJ_FALSE : PJ_TRUE; 1506 1473 } 1507 1474 … … 1619 1586 } 1620 1587 1621 return sess_dec_ref(ice_st);1588 return pj_grp_lock_dec_ref(ice_st->grp_lock)? PJ_FALSE : PJ_TRUE; 1622 1589 } 1623 1590 … … 1638 1605 } 1639 1606 1640 sess_add_ref(comp->ice_st);1607 pj_grp_lock_add_ref(comp->ice_st->grp_lock); 1641 1608 1642 1609 if (comp->ice_st->ice == NULL) { … … 1665 1632 } 1666 1633 1667 sess_dec_ref(comp->ice_st);1634 pj_grp_lock_dec_ref(comp->ice_st->grp_lock); 1668 1635 } 1669 1636 … … 1687 1654 pj_log_push_indent(); 1688 1655 1689 sess_add_ref(comp->ice_st);1656 pj_grp_lock_add_ref(comp->ice_st->grp_lock); 1690 1657 1691 1658 if (new_state == PJ_TURN_STATE_READY) { … … 1701 1668 1702 1669 /* Wait until initialization completes */ 1703 pj_ lock_acquire(comp->ice_st->init_lock);1670 pj_grp_lock_acquire(comp->ice_st->grp_lock); 1704 1671 1705 1672 /* Find relayed candidate in the component */ … … 1712 1679 pj_assert(cand != NULL); 1713 1680 1714 pj_ lock_release(comp->ice_st->init_lock);1681 pj_grp_lock_release(comp->ice_st->grp_lock); 1715 1682 1716 1683 /* Update candidate */ … … 1745 1712 comp->turn_sock = NULL; 1746 1713 1747 /* Set session to fail if we're still initializing */ 1748 if (comp->ice_st->state < PJ_ICE_STRANS_STATE_READY) { 1749 sess_fail(comp->ice_st, PJ_ICE_STRANS_OP_INIT, 1750 "TURN allocation failed", info.last_status); 1751 } else if (comp->turn_err_cnt > 1) { 1752 sess_fail(comp->ice_st, PJ_ICE_STRANS_OP_KEEP_ALIVE, 1753 "TURN refresh failed", info.last_status); 1754 } else { 1755 PJ_PERROR(4,(comp->ice_st->obj_name, info.last_status, 1756 "Comp %d: TURN allocation failed, retrying", 1757 comp->comp_id)); 1758 add_update_turn(comp->ice_st, comp); 1759 } 1760 } 1761 1762 sess_dec_ref(comp->ice_st); 1714 /* Set session to fail on error. last_status PJ_SUCCESS means normal 1715 * deallocation, which should not trigger sess_fail as it may have 1716 * been initiated by ICE destroy 1717 */ 1718 if (info.last_status != PJ_SUCCESS) { 1719 if (comp->ice_st->state < PJ_ICE_STRANS_STATE_READY) { 1720 sess_fail(comp->ice_st, PJ_ICE_STRANS_OP_INIT, 1721 "TURN allocation failed", info.last_status); 1722 } else if (comp->turn_err_cnt > 1) { 1723 sess_fail(comp->ice_st, PJ_ICE_STRANS_OP_KEEP_ALIVE, 1724 "TURN refresh failed", info.last_status); 1725 } else { 1726 PJ_PERROR(4,(comp->ice_st->obj_name, info.last_status, 1727 "Comp %d: TURN allocation failed, retrying", 1728 comp->comp_id)); 1729 add_update_turn(comp->ice_st, comp); 1730 } 1731 } 1732 } 1733 1734 pj_grp_lock_dec_ref(comp->ice_st->grp_lock); 1763 1735 1764 1736 pj_log_pop_indent();
Note: See TracChangeset
for help on using the changeset viewer.