harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexei Fedotov" <alexei.fedo...@gmail.com>
Subject Re: [general] c help needed
Date Fri, 11 Apr 2008 16:43:10 GMT
Hello Senaka,

Thanks for your help. The mutex approach is nice for a common case.
When read/write locks are used it has a nice read performance. When
CAS operations are used, there is no MUTual EXclusive write access to
the list, so it is impossible to consistently remove an element from
the list. For some use cases this may be fine nevertheless.

Sorry, a static counter won't help me. I want to create identifiers
for localized messages automatically, using a file_name + counter.
Execution tends to avoid corner points of the source code.

With best regards, Alexei


On Fri, Apr 11, 2008 at 8:17 PM, Senaka Fernando <senakafdo@gmail.com> wrote:
> Hi Alexei,
>
>  I believe that the mutex is the better approach, after seeing some of its
>  uses for similar needs in other projects. Also, I think that the static
>  counter scenario should work, but hasn't that got to be within a thread
>  lock? If not it wont be thread safe like most other defines. Also, you may
>  go ahead with "#define INC". '(' and ')' are not needed as the #defines are
>  subjected to a literal replacement.
>
>  Regards,
>  Senaka
>
>
>
>  On Thu, Apr 10, 2008 at 4:50 PM, Xiao-Feng Li <xiaofeng.li@gmail.com> wrote:
>
>  > 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
>  >
>



-- 
With best regards,
Alexei

Mime
View raw message