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


Groups > comp.lang.c > #23133

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

Path csiph.com!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Uncle Steve <stevet810@gmail.com>
Newsgroups comp.lang.c
Subject 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
Date Fri, 06 Jul 2012 18:54:38 -0400
Organization A noiseless patient Spider
Lines 103
Message-ID <c365530590f39fd-5d0be@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> <7ee0b658ce2dc96-3111f@gmail.com> <3ad80f94-cbba-4bf4-98ff-000177e7383e@q29g2000vby.googlegroups.com> <046b22079b2080c-b9239@gmail.com> <b85c6e84-07dc-4b1c-b94e-d36ea0af03fb@n33g2000vbi.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 8bit
Injection-Info mx04.eternal-september.org; posting-host="M1JqkNNJU2aX/HiSUuiK1A"; logging-data="12590"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18rKjC3fNkAp9Ko0PyGpcao"
User-Agent nn 6.7.3
Content-Length 5047
Cancel-Lock sha1:wh/sfbgXg+7hUM1KMvLsmvty5bM=
Xref csiph.com comp.lang.c:23133

Show key headers only | View raw


On Fri, Jul 06, 2012 at 07:39:56PM -0700, Joshua Maurice wrote:
> On Jul 6, 2:20 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > On Fri, Jul 06, 2012 at 04:58:09PM -0700, Joshua Maurice wrote:
> > > To be topical, here's the answer for C11: volatile is useless as a
> > > portable threading construct. Do not use it. It's (maybe) useful in a
> > > portable way only for 3 obscure situations: MMIO, setjmp and longjmp
> > > stuff, and signal handling. If you properly use C11 mutexes, there is
> > > absolutely no need for volatile. To paraphrase you else-thread, there
> > > will be no "funny business" if you use mutexes correctly. It tells the
> > > compiler, the hardware, and anything else relevant, to obey the
> > > semantics of the mutex. volatile is similarly useless when you
> > > correctly use the new "weak" C11 atomics. volatile adds nothing
> > > (portably).
> >
> > > Now, to finally answer your question, C11 threading semantics,
> > > including the lack of useful volatile semantics, is basically copied
> > > wholesale from pthreads. Don't use volatile as a portable threading
> > > construct with pthreads either. pthread mutexes do everything you
> > > need. volatile adds absolutely nothing (portably).
> >
> > > (Yes, you can accomplish useful code with volatile for specific
> > > platform and specific compilers with specific settings. That's an
> > > "advanced" topic that I won't cover now.)
> >
> > > Now, what do you mean by "mutexes are not memory access barriers"?
> > > Mutexes do two things: they provide blocking semantics for mutual
> > > exclusion, and they guarantee visibility ala the usual "happens-
> > > before" semantics. Mutexes are implemented with hardware memory
> > > fences / barriers. This is all true for mutexes in pthreads, win32
> > > threads, Boost threads, ACE threads, C11 threads, C++11 threads, and
> > > every case I can recall seeing the word "mutex".
> >
> > Well, mutexes are barriers for the memory accesses associated with the
> > mutex itself, but not in general.  Reading the above I can tell that
> > you think I'm using POSIX mutexes, however that is not the case.
> 
> I strongly suggest that you don't use the word "mutex" for your code
> because you will confuse most readers. Most readers will associate
> "mutex" with both mutual exclusion and "happens-before" global memory
> visibility. That's how the term has been commonly used (ex: posix
> pthreads), and how it will continue to commonly be used (ex: C11).
> 
> > I am
> > using a custom locking scheme that allows for concurrent readers, but
> > is exclusive for writers.  As a consequence, the behavior of
> > variables marked volatile in my code is critical, so I don't want to
> > mistakenly use it in a way that results in surprises.
> >
> > Now you can say that what I am doing is non-portable, but you would
> > be wrong there since my algorithms are in fact portable as long as I
> > am following the rules of the C standard.  Perhaps the proper term is
> > limited portability as I rely on a tiny amount of platform specific
> > asm.  (One instruction.)
> 
> I will say it again: the C89 and C99 standards do not mean what you
> think. What the compiler writers and thread library writers understand
> volatile to mean is not what you understand volatile to mean, and at
> the end of the day what the compiler writers and thread library
> writers think is paramount. You may be able to get this to work for
> specific platforms with specific compilers. I suggest you ask in a
> location specific to such implementations for such platform specific
> hackery. However, do not look at the C standard for guidance on this.

Um, what is it that compiler writers and thread library writers
understand volatile to mean?
 
> In the C11 parlance, what you're trying to write requires the "weak"
> atomics, such as relaxed, acquire, release, and consume memory
> operations. Attempting to write stuff like this without C11 support
> with the C11 primitives is a wholly platform specific affair, and
> neither C89 nor C90 can help you there. volatile specifically cannot
> help you (barring platform specific documentation to the contrary).

You seem to be making too many assumptions about my code.
 
> From else-thread:
> On Jul 6, 2:29 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > On Sat, Jul 07, 2012 at 12:37:59PM +1200, Ian Collins wrote:
> > > Then use your platform's native threading primitives.
> >
> > Doing so would incur a performance penalty in comparison to my
> > synchronization primitives in some circumstances.
> 
> That's your problem. You have broken code. Your options are:
> 1- broken code (e.g. using volatile and asm memory barriers absent
> platform specific guarantees)
> 2- code that relies on platform specific guarantees (e.g. with using
> volatile and asm memory barriers)
> 3- "slow" code (e.g. with posix mutexes)
> 4- C11 code

How do you know, a priori, that my code is broken?  Do I have to
change my passwords again?


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