Changeset 5826 for pjproject


Ignore:
Timestamp:
Jul 17, 2018 10:49:57 AM (6 years ago)
Author:
nanang
Message:

Close #2127: Replaced DNS resolver mutex with group lock.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib-util/src/pjlib-util/resolver.c

    r5612 r5826  
    170170    /* Internals */ 
    171171    pj_pool_t           *pool;          /**< Internal pool.                 */ 
    172     pj_mutex_t          *mutex;         /**< Mutex protection.              */ 
     172    pj_grp_lock_t       *grp_lock;      /**< Group lock protection.         */ 
    173173    pj_bool_t            own_timer;     /**< Do we own timer?               */ 
    174174    pj_timer_heap_t     *timer;         /**< Timer instance.                */ 
     
    237237                                      unsigned servers[]); 
    238238 
     239/* Destructor */ 
     240static void dns_resolver_on_destroy(void *member); 
    239241 
    240242/* Close UDP socket */ 
     
    285287    pj_bzero(&socket_cb, sizeof(socket_cb)); 
    286288    socket_cb.on_read_complete = &on_read_complete; 
    287     status = pj_ioqueue_register_sock(resv->pool, resv->ioqueue, 
    288                                       resv->udp_sock, resv, &socket_cb, 
    289                                       &resv->udp_key); 
     289    status = pj_ioqueue_register_sock2(resv->pool, resv->ioqueue, 
     290                                       resv->udp_sock, resv->grp_lock, 
     291                                       resv, &socket_cb, &resv->udp_key); 
    290292    if (status != PJ_SUCCESS) 
    291293        return status; 
     
    332334    pj_bzero(&socket_cb, sizeof(socket_cb)); 
    333335    socket_cb.on_read_complete = &on_read_complete; 
    334     status = pj_ioqueue_register_sock(resv->pool, resv->ioqueue, 
    335                                       resv->udp6_sock, resv, &socket_cb, 
    336                                       &resv->udp6_key); 
     336    status = pj_ioqueue_register_sock2(resv->pool, resv->ioqueue, 
     337                                       resv->udp6_sock, resv->grp_lock, 
     338                                       resv, &socket_cb, &resv->udp6_key); 
    337339    if (status != PJ_SUCCESS) 
    338340        return status; 
     
    403405    pj_strdup2_with_null(pool, &resv->name, name); 
    404406     
    405     /* Create the mutex */ 
    406     status = pj_mutex_create_recursive(pool, name, &resv->mutex); 
     407    /* Create group lock */ 
     408    status = pj_grp_lock_create_w_handler(pool, NULL, resv, 
     409                                          &dns_resolver_on_destroy, 
     410                                          &resv->grp_lock);  
    407411    if (status != PJ_SUCCESS) 
    408412        goto on_error; 
     413 
     414    pj_grp_lock_add_ref(resv->grp_lock); 
    409415 
    410416    /* Timer, ioqueue, and settings */ 
     
    418424    /* Create the timer heap if one is not specified */ 
    419425    if (resv->timer == NULL) { 
     426        resv->own_timer = PJ_TRUE; 
    420427        status = pj_timer_heap_create(pool, TIMER_SIZE, &resv->timer); 
    421428        if (status != PJ_SUCCESS) 
     
    425432    /* Create the ioqueue if one is not specified */ 
    426433    if (resv->ioqueue == NULL) { 
     434        resv->own_ioqueue = PJ_TRUE; 
    427435        status = pj_ioqueue_create(pool, MAX_FD, &resv->ioqueue); 
    428436        if (status != PJ_SUCCESS) 
     
    450458    pj_dns_resolver_destroy(resv, PJ_FALSE); 
    451459    return status; 
     460} 
     461 
     462 
     463void dns_resolver_on_destroy(void *member) 
     464{ 
     465    pj_dns_resolver *resolver = (pj_dns_resolver*)member; 
     466    pj_pool_safe_release(&resolver->pool); 
    452467} 
    453468 
     
    509524    } 
    510525 
    511     if (resolver->mutex) { 
    512         pj_mutex_destroy(resolver->mutex); 
    513         resolver->mutex = NULL; 
    514     } 
    515  
    516     pj_pool_safe_release(&resolver->pool); 
     526    pj_grp_lock_dec_ref(resolver->grp_lock); 
    517527 
    518528    return PJ_SUCCESS; 
     
    536546    PJ_ASSERT_RETURN(count < PJ_DNS_RESOLVER_MAX_NS, PJ_EINVAL); 
    537547 
    538     pj_mutex_lock(resolver->mutex); 
     548    pj_grp_lock_acquire(resolver->grp_lock); 
    539549 
    540550    if (count > PJ_DNS_RESOLVER_MAX_NS) 
     
    555565                                      (pj_uint16_t)(ports ? ports[i] : PORT)); 
    556566        if (status != PJ_SUCCESS) { 
    557             pj_mutex_unlock(resolver->mutex); 
     567            pj_grp_lock_release(resolver->grp_lock); 
    558568            return PJLIB_UTIL_EDNSINNSADDR; 
    559569        } 
     
    566576    resolver->ns_count = count; 
    567577 
    568     pj_mutex_unlock(resolver->mutex); 
     578    pj_grp_lock_release(resolver->grp_lock); 
    569579    return PJ_SUCCESS; 
    570580} 
     
    580590    PJ_ASSERT_RETURN(resolver && st, PJ_EINVAL); 
    581591 
    582     pj_mutex_lock(resolver->mutex); 
     592    pj_grp_lock_acquire(resolver->grp_lock); 
    583593    pj_memcpy(&resolver->settings, st, sizeof(*st)); 
    584     pj_mutex_unlock(resolver->mutex); 
     594    pj_grp_lock_release(resolver->grp_lock); 
    585595    return PJ_SUCCESS; 
    586596} 
     
    595605    PJ_ASSERT_RETURN(resolver && st, PJ_EINVAL); 
    596606 
    597     pj_mutex_lock(resolver->mutex); 
     607    pj_grp_lock_acquire(resolver->grp_lock); 
    598608    pj_memcpy(st, &resolver->settings, sizeof(*st)); 
    599     pj_mutex_unlock(resolver->mutex); 
     609    pj_grp_lock_release(resolver->grp_lock); 
    600610    return PJ_SUCCESS; 
    601611} 
     
    610620    PJ_ASSERT_ON_FAIL(resolver, return); 
    611621 
    612     pj_mutex_lock(resolver->mutex); 
     622    pj_grp_lock_acquire(resolver->grp_lock); 
    613623    pj_timer_heap_poll(resolver->timer, NULL); 
    614     pj_mutex_unlock(resolver->mutex); 
     624    pj_grp_lock_release(resolver->grp_lock); 
    615625 
    616626    pj_ioqueue_poll(resolver->ioqueue, timeout); 
     
    684694    delay.msec = resolver->settings.qretr_delay; 
    685695    pj_time_val_normalize(&delay); 
    686     status = pj_timer_heap_schedule(resolver->timer, &q->timer_entry, &delay); 
     696    status = pj_timer_heap_schedule_w_grp_lock(resolver->timer, 
     697                                               &q->timer_entry, 
     698                                               &delay, 1, 
     699                                               resolver->grp_lock); 
    687700    if (status != PJ_SUCCESS) { 
    688701        return status; 
     
    874887 
    875888    /* Start working with the resolver */ 
    876     pj_mutex_lock(resolver->mutex); 
     889    pj_grp_lock_acquire(resolver->grp_lock); 
    877890 
    878891    /* Get current time. */ 
     
    907920             */ 
    908921            cache->ref_cnt++; 
    909             pj_mutex_unlock(resolver->mutex); 
     922            pj_grp_lock_release(resolver->grp_lock); 
    910923 
    911924            /* This cached response is still valid. Just return this 
     
    917930 
    918931            /* Done. No host resolution is necessary */ 
    919             pj_mutex_lock(resolver->mutex); 
     932            pj_grp_lock_acquire(resolver->grp_lock); 
    920933 
    921934            /* Decrement the ref counter. Also check if it is time to free 
     
    934947             * Refer to ticket #1974. 
    935948             */ 
    936             pj_mutex_unlock(resolver->mutex); 
     949            pj_grp_lock_release(resolver->grp_lock); 
    937950            return status; 
    938951        } 
     
    10011014        *p_query = p_q; 
    10021015 
    1003     pj_mutex_unlock(resolver->mutex); 
     1016    pj_grp_lock_release(resolver->grp_lock); 
    10041017    return status; 
    10051018} 
     
    10161029    PJ_ASSERT_RETURN(query, PJ_EINVAL); 
    10171030 
    1018     pj_mutex_lock(query->resolver->mutex); 
     1031    pj_grp_lock_acquire(query->resolver->grp_lock); 
     1032 
     1033    if (query->timer_entry.id == 1) { 
     1034        pj_timer_heap_cancel_if_active(query->resolver->timer, 
     1035                                       &query->timer_entry, 0); 
     1036    } 
    10191037 
    10201038    cb = query->cb; 
     
    10241042        (*cb)(query->user_data, PJ_ECANCELLED, NULL); 
    10251043 
    1026     pj_mutex_unlock(query->resolver->mutex); 
     1044    pj_grp_lock_release(query->resolver->grp_lock); 
    10271045    return PJ_SUCCESS; 
    10281046} 
     
    15531571    resolver = q->resolver; 
    15541572 
    1555     pj_mutex_lock(resolver->mutex); 
     1573    pj_grp_lock_acquire(resolver->grp_lock); 
    15561574 
    15571575    /* Recheck that this query is still pending, since there is a slight 
     
    15611579    if (pj_hash_get(resolver->hquerybyid, &q->id, sizeof(q->id), NULL)==NULL) { 
    15621580        /* Yeah, this query is done. */ 
    1563         pj_mutex_unlock(resolver->mutex); 
     1581        pj_grp_lock_release(resolver->grp_lock); 
    15641582        return; 
    15651583    } 
     
    15721590        status = transmit_query(resolver, q); 
    15731591        if (status == PJ_SUCCESS) { 
    1574             pj_mutex_unlock(resolver->mutex); 
     1592            pj_grp_lock_release(resolver->grp_lock); 
    15751593            return; 
    15761594        } else { 
     
    15911609 
    15921610    /* Workaround for deadlock problem in #1565 (similar to #1108) */ 
    1593     pj_mutex_unlock(resolver->mutex); 
     1611    pj_grp_lock_release(resolver->grp_lock); 
    15941612 
    15951613    /* Call application callback, if any. */ 
     
    16061624 
    16071625    /* Workaround for deadlock problem in #1565 (similar to #1108) */ 
    1608     pj_mutex_lock(resolver->mutex); 
     1626    pj_grp_lock_acquire(resolver->grp_lock); 
    16091627 
    16101628    /* Clear data */ 
     
    16231641    pj_list_push_back(&resolver->query_free_nodes, q); 
    16241642 
    1625     pj_mutex_unlock(resolver->mutex); 
     1643    pj_grp_lock_release(resolver->grp_lock); 
    16261644} 
    16271645 
     
    16631681    } 
    16641682 
    1665     pj_mutex_lock(resolver->mutex); 
     1683    pj_grp_lock_acquire(resolver->grp_lock); 
    16661684 
    16671685 
     
    17461764 
    17471765    /* Workaround for deadlock problem in #1108 */ 
    1748     pj_mutex_unlock(resolver->mutex); 
     1766    pj_grp_lock_release(resolver->grp_lock); 
    17491767 
    17501768    /* Notify applications first, to allow application to modify the  
     
    17671785 
    17681786    /* Workaround for deadlock problem in #1108 */ 
    1769     pj_mutex_lock(resolver->mutex); 
     1787    pj_grp_lock_acquire(resolver->grp_lock); 
    17701788 
    17711789    /* Save/update response cache. */ 
     
    17961814                                 src_addr, src_addr_len); 
    17971815 
    1798     if (status != PJ_EPENDING) { 
     1816    if (status != PJ_EPENDING && status != PJ_ECANCELLED) { 
    17991817        char errmsg[PJ_ERR_MSG_SIZE]; 
    18001818 
     
    18061824    } 
    18071825 
    1808     pj_mutex_unlock(resolver->mutex); 
     1826    pj_grp_lock_release(resolver->grp_lock); 
    18091827} 
    18101828 
     
    18321850                     PJLIB_UTIL_EDNSNOANSWERREC); 
    18331851 
    1834     pj_mutex_lock(resolver->mutex); 
     1852    pj_grp_lock_acquire(resolver->grp_lock); 
    18351853 
    18361854    /* Build resource key for looking up hash tables */ 
     
    18541872    update_res_cache(resolver, &key, PJ_SUCCESS, set_ttl, pkt); 
    18551873 
    1856     pj_mutex_unlock(resolver->mutex); 
     1874    pj_grp_lock_release(resolver->grp_lock); 
    18571875 
    18581876    return PJ_SUCCESS; 
     
    18691887    PJ_ASSERT_RETURN(resolver, 0); 
    18701888 
    1871     pj_mutex_lock(resolver->mutex); 
     1889    pj_grp_lock_acquire(resolver->grp_lock); 
    18721890    count = pj_hash_count(resolver->hrescache); 
    1873     pj_mutex_unlock(resolver->mutex); 
     1891    pj_grp_lock_release(resolver->grp_lock); 
    18741892 
    18751893    return count; 
     
    18871905    pj_time_val now; 
    18881906 
    1889     pj_mutex_lock(resolver->mutex); 
     1907    pj_grp_lock_acquire(resolver->grp_lock); 
    18901908 
    18911909    pj_gettimeofday(&now); 
     
    19471965              pj_pool_get_used_size(resolver->pool))); 
    19481966 
    1949     pj_mutex_unlock(resolver->mutex); 
     1967    pj_grp_lock_release(resolver->grp_lock); 
    19501968#endif 
    19511969} 
Note: See TracChangeset for help on using the changeset viewer.