harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Xiao-Feng Li" <xiaofeng...@gmail.com>
Subject Re: [general] c help needed
Date Thu, 10 Apr 2008 11:20:54 GMT
On Thu, Apr 10, 2008 at 6:08 PM, Alexei Fedotov
<alexei.fedotov@gmail.com> wrote:
> Thanks, Alexei, Xiao Feng,
>  This was helpful.
>
>  BTW, as far as I understand using mutexes, I need to protect reads as
>  well. If I use CAS for adding a new element, the list is always
>  consistent for reading from other threads without any protection.
>

Well, it depends on if you are using a read-write lock. Read-write
lock permits concurrent readings.

Thanks,
xiaofeng

>
>  On Wed, Apr 9, 2008 at 6:00 PM, Xiao-Feng Li <xiaofeng.li@gmail.com> wrote:
>  > Right.
>  >
>  >  To avoid the CAS loop, you can use Mutex.
>  >
>  >  mutex_lock();
>  >  //critical section;
>  >  mutex_unlock();
>  >
>  >  Thanks,
>  >  xiaofeng
>  >
>  >
>  >
>  >  On Wed, Apr 9, 2008 at 8:35 PM, Aleksey Shipilev
>  >  <aleksey.shipilev@gmail.com> wrote:
>  >  > IMHO here:
>  >  >
>  >  >  1. Using CAS infers using busy-loop, e.g. "we are trying until
>  >  >  operation succeed". You might tolerate spinning there by using PAUSE
>  >  >  instruction or some other way.
>  >  >  2. How about "static int counter = 0; #define INC() counter++;"? It
>  >  >  will be done in runtime though.
>  >  >
>  >  >  Thanks,
>  >  >  Aleksey.
>  >  >
>  >  >
>  >  >
>  >  >  On Wed, Apr 9, 2008 at 3:57 PM, Alexei Fedotov <alexei.fedotov@gmail.com>
wrote:
>  >  >  > Hello folks,
>  >  >  >  Could you please help?
>  >  >  >
>  >  >  >  1. Is it possible to add to a thread safe linked list using CAS
>  >  >  >  without a busy loop?
>  >  >  >
>  >  >  >  void add_site(LogSite* log_site) {
>  >  >  >     LogSite** p_site_head = (LogSite**) &get_logger()->log_site;
>  >  >  >     LogSite* old_value = *p_site_head;
>  >  >  >     do {
>  >  >  >         log_site->next = (LogSite*) old_value;
>  >  >  >         (LogSite*) old_value = (LogSite*) apr_atomic_casptr((volatile
>  >  >  >  void **) p_site_head,
>  >  >  >             (void*) log_site, log_site->next);
>  >  >  >     } while (old_value != log_site->next); // busy loop
>  >  >  >  }
>  >  >  >
>  >  >  >  2. Is it possible to create an auto-increment macro within a
>  >  >  >  compilation unit using C preprocessor?
>  >  >  >
>  >  >  >  #define INC() ...
>  >  >  >
>  >  >  >     INC() // == 1
>  >  >  >     INC() // == 2
>  >  >  >     ...
>  >  >  >
>  >  >  >  Thank you for sharing your expertise.
>  >  >  >  --
>  >  >  >  With best regards,
>  >  >  >  Alexei
>  >  >  >
>  >  >
>  >
>  >
>  >
>  >  --
>  >  http://xiao-feng.blogspot.com
>  >
>
>
>
>  --
>  With best regards,
>  Alexei
>



-- 
http://xiao-feng.blogspot.com

Mime
View raw message