Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23112

Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

From Uncle Steve <stevet810@gmail.com>
Newsgroups comp.lang.c
Subject Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually
Date 2012-07-06 14:01 -0400
Organization A noiseless patient Spider
Message-ID <7ee0b658ce2dc96-3111f@gmail.com> (permalink)
References <94eafd27-6aba-4a17-9403-d42912312e6c@googlegroups.com> <0.8f83cddfeac55b9f2de1.20120705232249BST.87k3yhalvq.fsf@bsb.me.uk> <bca417a59725d29-f3cbf@gmail.com> <a5p35pFrmbU2@mid.individual.net>

Show all headers | View raw


On Sat, Jul 07, 2012 at 09:20:57AM +1200, Ian Collins wrote:
> On 07/ 7/12 05:08 AM, Uncle Steve wrote:
> >On Thu, Jul 05, 2012 at 11:22:49PM +0100, Ben Bacarisse wrote:
> >>
> >>To a very large extent, the C standard washes it hands of volatile
> >>objects.  Sure, it says a fair bit about what *may* happen but it also
> >>says "[w]hat constitutes an access to an object that has volatile-
> >>qualified type is implementation-defined" (6.7.3 p7) and "[a]n actual
> >>implementation need not evaluate part of an expression if it can deduce
> >>that [...] no needed side effects are produced (including any caused by
> >>[...] accessing a volatile object)" (5.1.2.3 p4).
> >>
> >>Together, these give an implementation so much leeway that you often
> >>need to refer to the implementation, not the standard.  For example, an
> >>implementation may well be able to deduce that the assignment is not a
> >>"needed side effect", coming as it does at the start of main.  Equally,
> >>an implementation may treat all accesses to volatile-qualified objects
> >>with kid gloves.
> >
> >This is a concern in my mind right now as I'm writing some posix
> >threads code, and so obviously there are a large number of variables
> >which ought to be declared volatile.
> 
> What leads you to that conclusion?  That line of though usually implies 
> you are misunderstanding the relationship between volatile and threads.
> 
> What are you trying to achieve with volatile?

Well, I have some data structures that will be accessed from arbitrary
threads, so I want to make sure there's no funny business happening
behind the scenes where the compiler decides that certain accesses are
optional.

Here's a little snippet of code which shows the issue, albeit without
nested structures:


#include <local_time.h>


struct sample_s {
   T64   a, b;
};

typedef struct sample_s sample;

#define SAMPLES   100



#define nsclock(p) { \
   volatile struct timespec T; \
   clock_gettime(CLOCK_MONOTONIC, &T); \
   p = T.tv_sec * 1000000000 + T.tv_nsec; \
}

void  do_timing(sample * p)
{
   volatile T64   s, e, v;
//   volatile unsigned int i;

   nsclock(s);
   nsclock(s);
   
//   for(i = 0; i < 1 ; i++);
   nsclock(e);

   p->a = (e - s);

   nsclock(e);

   p->b = (e - s);
   
   return;
}


void  emit_sample(sample *p)
{
   fprintf(stdout, "%-5d %-5d %-5d\n", p->a, p->b, p->b - p->a);

   return;
}




int   main(char *argv, int argc)
{
   sample tr[SAMPLES + 1];
   int i;

   tr[SAMPLES].a = tr[SAMPLES].b = 0;
   

   for(i = 0; i < SAMPLES; i++)
      do_timing(&tr[i]);

   fprintf(stdout, "Timing results for %d samples:\n\n", SAMPLES);

   fprintf(stdout, "%-5s %-5s %-5s\n", "A", "B", "B - A");
   
   for(i = 0; i < SAMPLES; i++)
      emit_sample(&tr[i]);

   exit(0);
}

 
So, without including all the crap in local_time.h, I have to explain
a few definitions.

T64 is an unsigned long long int representing microseconds since
epoch, but I ended up using it as if it represented nanoseconds since
epoch since my i3 dual core laptop has a nanosecond-resolution timer.

The variable declared volatile are required otherwise some variable
accesses are optimized out, giving inaccurate results during the data
acquisition phase.  The problems here are magnified when these access
patterns occur with competing threads.  IIRC, mutexes are not memory
access barriers and solve different problems.


Regards,

Uncle Steve

-- 
The moon has never been closer.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Myth__Buster <raghavanil4m@gmail.com> - 2012-07-05 10:44 -0700
  Re: Clarification on the applicability of compile-time optimization to a  struct variable which encompasses a mixture of fields declared as volatile  and non-volatile individually pete <pfiland@mindspring.com> - 2012-07-05 14:59 -0400
    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Myth__Buster <raghavanil4m@gmail.com> - 2012-07-07 12:53 -0700
  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-05 23:22 +0100
    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 13:08 -0400
      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ian Collins <ian-news@hotmail.com> - 2012-07-07 09:20 +1200
        Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 14:01 -0400
          Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-06 18:11 -0400
            Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 14:33 -0400
              Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ian Collins <ian-news@hotmail.com> - 2012-07-07 11:29 +1200
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 16:27 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ian Collins <ian-news@hotmail.com> - 2012-07-07 12:37 +1200
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 17:29 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ian Collins <ian-news@hotmail.com> - 2012-07-07 15:50 +1200
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 19:55 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-07 17:04 +0300
              Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-07 00:10 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Gareth Owen <gwowen@gmail.com> - 2012-07-07 10:00 +0100
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-07 10:55 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-07 15:13 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 04:40 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-07 17:20 +0300
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-07 10:33 -0700
              Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-07 08:16 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 05:12 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-07 09:24 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 06:07 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Keith Thompson <kst-u@mib.org> - 2012-07-07 13:26 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 16:04 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually James Kuyper <jameskuyper@verizon.net> - 2012-07-09 11:14 -0400
          Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-06 16:58 -0700
            Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 17:20 -0400
              Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-06 19:39 -0700
                Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 18:54 -0400
                Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-06 20:12 -0700
                Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 19:25 -0400
                Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-06 20:33 -0700
                Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 19:47 -0400
            Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Jens Gustedt <jens.gustedt@loria.fr> - 2012-07-07 15:47 +0200
              Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 06:58 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Philip Lantz <prl@canterey.us> - 2012-07-14 21:46 -0700
              Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-07 16:19 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Jens Gustedt <jens.gustedt@loria.fr> - 2012-07-08 09:40 +0200
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually James Kuyper <jameskuyper@verizon.net> - 2012-07-08 10:51 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Philip Lantz <prl@canterey.us> - 2012-07-13 23:25 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-14 14:37 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-14 14:46 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Philip Lantz <prl@canterey.us> - 2012-07-14 21:39 -0700
    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-06 17:24 -0700
      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-07 02:33 +0100
    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Myth__Buster <raghavanil4m@gmail.com> - 2012-07-07 12:53 -0700

csiph-web