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 11:16:41 -0700

Hi,

I would like to make the point clear.

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);

    in step 4, I telled curl to share DNS cache. is it right? 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?

 I'm still not clear about what I should write in lock and unlock
function. 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.
I understand if I want to share my own data among multi_threads, the
"useptr" should point to my own data. But how about DNS and cookies.
what pointer I should passed? Also, do I need to provide the mutex
here? for my own data, I could provide some thing like: lock
data do whatever on my own data unlock data. But on
dns and cookies, what operation I can do on the data?

Daniel Stenberg wrote:

> On Thu, 19 Jan 2006, Yi Li wrote:
>
>>> This program doesn't set the lock or unlock function callbacks with
>>> curl_share_setopt(), so using this share multi-threaded is bound to
>>> cause a memory mess.
>>
>
>> from the lib506.c in tests, it appears that the lock and unlock
>> callback are only needed when you want to mutex your data in
>> multi_threads.
>
>
> Not *your* data. libcurl's data that you told it to share between
> multiple handles.
>
>> In my program, I don't have such data.
>
>
> You told it to share DNS cache, right? It means that every time
> libcurl wants to resolve a host name it'll require a lock, do the
> resolve dance, and then unlock again.
>
>> in such case, those callbacks are still needed? What am I going to do
>> in those callbacks. Just give a empty function?
>
>
> You need to make sure that only one thread at a time has the same
> 'data' locked at any given time. A mutex!
>
Received on 2006-01-23