cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: how to write the lock funcition in share interface

From: Yi Li <Michael.li_at_playstarmusic.com>
Date: Mon, 23 Jan 2006 17:30:59 -0700

Daniel Stenberg wrote:

> On Mon, 23 Jan 2006, Yi Li wrote:
>
Hi, Daniel

thanks for your help very much. Now I guess to share the DNS cache, the
following code is correct.

pthread_mutex_t locker = PTHREAD_MUTEX_INITIALIZER;

.....
......

void lock(CURL *handle, curl_lock_data data, curl_lock_access
access,void *useptr ){

  (void)handle;
  (void)access;
   
    pthread_mutex_lock(&locker);

}

/* unlock callback */
void unlock(CURL *handle, curl_lock_data data, void *useptr ){
 
  (void)handle;
    pthread_mutex_unlock(&locker);

}

....
....

in main function:

CURLSH* share;
  share = curl_share_init();

  CURLSHcode shareError;

  shareError = curl_share_setopt( share, CURLSHOPT_LOCKFUNC, lock);
  if(shareError){
    cerr<<"share set opt wrong"<<endl;
    curl_share_cleanup(share);
    exit(1);
  }
  shareError = curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, unlock);
  if(shareError){
    cerr<<"share set opt wrong"<<endl;
    curl_share_cleanup(share);
    exit(1);
  }
  shareError = curl_share_setopt(share,CURLSHOPT_SHARE,CURL_LOCK_DATA_DNS);
  if(shareError){
    cerr<<"share set opt wrong"<<endl;
    curl_share_cleanup(share);
    exit(1);
  }

.....
....

in each thread

easyHandler = curl_easy_init();
curl_easy_setopt(easyHandler,CURLOPT_SHARE,share);

Is the above code correct?

Cheers!

Michael

>> In order to share DNS cache among multi_threads, I will need to
>> achieve the following steps.
>>
>> 1: call curl_share_init() to create a share handler
>> 2: call curl_share_setopt( sharehandler, CURLSHOPT_LOCKFUNC, lock);
>> 3: call curl_share_setopt(sharehandler,CURLSHOPT_UNLOCKFUNC,unlock);
>> 4: call curl_share_setopt(share,CURLSHOPT_SHARE,CURL_LOCK_DATA_DNS);
>> 5: create an easy handler and call
>> curl_easy_setopt(easyHandler,CURLOPT_SHARE,shareHandler);
>
>
> Yes, that seems correct.
>
>> in step 4, I telled curl to share DNS cache. is it right?
>
>
> Yes.
>
>> Do it means that curl liberary will provide some kind of mutex
>> mechanisem to share the DNS or should I write my "own" mutex to tell
>> curl lib how to share ? If I need to write my own mutex, where I
>> write it? in Lock and UnLock?
>
>
> Well, given that you pass this share object to more than one easy
> handle with curl_easy_setopt() + CURLOPT_SHARE, then yes.
>
> Then libcurl will call the lock and unlock function callbacks you set
> and it will assume that you do the mutex magic for them: When libcurl
> locks the DNS for single access, you must not let another handle get
> the lock until the first handle unlocks the DNS again.
>
>> I'm still not clear about what I should write in lock and unlock
>> function.
>
>
> Call your thread system's mutex functions.
>
>> in void lock(CURL *handle, curl_lock_data data, curl_lock_access
>> access,void *useptr ), I understand "data" can only be
>
>
>> CURL_LOCK_DATA_SHARE: to share user own data
>> CURL_LOCK_DATA_DNS: to share dns
>> CURL_LOCK_DATA_COOKIE: to share cookies.
>
>
> Correct. That tells your function what particular data libcurl wants
> to lock access to. You can then have a single mutex for all kinds of
> data, or opt to have one mutext for each kind. Your call!
>
>> I understand if I want to share my own data among multi_threads, the
>> "useptr" should point to my own data.
>
>
> That pointer is the one you set with CURLSHOPT_USERDATA. You can set
> that to whatever you want, and use it for whatever purpose you want.
>
>> But how about DNS and cookies. what pointer I should passed?
>
>
> For libcurl it doesn't matter.
>
Received on 2006-01-24