home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / volume.19 / text0031.txt < prev    next >
Encoding:
Internet Message Format  |  1990-05-17  |  7.8 KB

  1. From: Ronald Guilmette <rfg@ics.UCI.EDU>
  2.  
  3. In article <5106@rtech.rtech.com> daveb@llama.UUCP writes:
  4. >Posix 1003.4 is the "real time" extension to Posix.  It encompasses
  5. >shared memory and threads.  By including these features it introduces
  6. >some new restrictions on the compilation environment, the gist of
  7. >which are that almost everything needs to be treated as "partially
  8. >volatile" (my phrase).  The purpose of this note is to explore the
  9. >sense of the community tuned to ANSI C to see if this presents a
  10. >problem.  I *don't* have any problems with the proposed Posix
  11. >restrictions, and in fact consider them essential.  I do suspect that
  12. >some compiler writers may have some objections.  Some of the tricks
  13. >now used by "hyper-optimizing" compilers would be illegal.
  14. >
  15. >The Draft 1003.4 Std. says in section 13.2:
  16. >
  17. >    "The C translator must support at least the volatile key word.
  18.  
  19. What the story here?  ANSI C already requires this.  Does 1003.4 not
  20. require ANSI C as a basis?
  21.  
  22. >    This is one mechanism that satisfies the reference to global
  23. >    variables problems; however, it does not satisfy the code movement
  24. >    around synchronization points problem.
  25.  
  26. Is it just me or does this strike anyone else as being pure gibberish?
  27. Are these "problems" defined somewhere?  Perhaps with examples of how
  28. these "problems" could crop up in some actual code?
  29.  
  30. At the moment, I can only guess at what these "problems" are supposed to
  31. be.  Based upon my best guess (given the *very* limited information
  32. available here) I'd have to say that these are one and the same problem,
  33. but that the people doing 1003.4 don't know that.
  34.  
  35. This "code movement" problem is (I assume) just a question of being able to
  36. get the compiler *not* to move some code past some point.  I believe that
  37. it is possible to do just that via proper use of the volatile keyword.
  38.  
  39. Also, I believe that when the authors say "global variables" what they really
  40. mean here is "shared variables" (i.e. variables which may
  41. be accessed by two or more threads).  Shared variables need not be "global"
  42. in the traditional C sense (i.e. "declared" variables which are declared
  43. outside of all functions).  They could also be variables allocated via malloc()
  44. and possibly even local (auto) variables.
  45.  
  46. Anyway, insuring the proper synchronization of accesses to shared variables
  47. (via proper use of "volatile") also restricts allowed compiler optimizations
  48. such as code motion.
  49.  
  50. So as I say, I think these are really just one problem, but if somebody
  51. gave us some code examples, we would know for sure.
  52.  
  53. >    Additional facilities to
  54. >    guarantee the validity of synchronization points must be supplied
  55. >    (either in an implementation defined manner or in an ANSI C
  56. >    defined manner)."
  57.  
  58. Which synchronization points?  All synchronization points?  Some range of
  59. them?  Individual synchronization points?
  60.  
  61. >
  62. >It continues in the rationale:
  63. >
  64. >    13.3.1 "Standardization Issues"
  65. >
  66. >    "Because IEEE Std 1003.4-199x is a source level standard and
  67. >    because the majority of translators are designed without respect
  68. >    to multiple streams of execution accessing global data, IEEE Std
  69. >    1003.4-199x must specify that translators provide some means for
  70. >    the programmer to inform the translator that jointly accessed
  71. >    variables are not cached in registers (at least at synchronization
  72.  
  73. It's called "volatile" friends.
  74.  
  75. >    points).  It drastically hurts the portability of IEEE Std
  76. >    1003.4-199x conforming applications that we can not specify a
  77. >    mechanism that will work with all translators.  Doing so is
  78. >    outside the scope of IEEE Std 1003.4-199x.  However, some
  79. >    mechanism must be supplied or the {_POSIX_MEMORY_SHARING} and
  80. >    {_POSIX_THREADS} options can not be used.
  81. >
  82. >    . . .
  83. >
  84. >    13.3.1 "Rationale Relating to C language Requirements"
  85. >
  86. >    "ANSI C does not define any base assumptions that the compiler
  87. >    writer uses in choosing how the compiler will generate its code.
  88. >    ANSI C states only that the compiler must generate code which
  89. >    correctly executes an ANSI C-conforming programs.  The potential of
  90. >    a multi-threaded environment requires some base assumptions that
  91. >    could conflict with the assumptions made by a compiler writer in
  92. >    creating an ANSI C compiler but in no ways conflicts with the ANSI
  93. >    C specification.
  94. >
  95. >    "ANSI C does provide the keyword volatile; however, this can only
  96. >    solve the memory coherence problem.  It does not address the code
  97. >    re-ordering problem.
  98.  
  99. I'm appaled that the 1003.4 authors don't think that this "problem" is
  100. worthy of at least a coded example.  If we had one, I'll bet that we
  101. could stick "volatile" in all the right places, and that this would
  102. solve the "problem".
  103.  
  104. >    ... further, using the volatile keyword to solve
  105. >    memory coherence problems is both error prone and inefficient.
  106.  
  107. Compared to what?  Do the people writing this stuff come from marketing
  108. backgrounds?
  109.  
  110. >    It is error prone because every variable accessed by more than one
  111. >    stream of execution must be marked volatile, and if the mark is
  112. >    forgotten the program might exhibit nondeterministic behavior.
  113.  
  114. Yes.  If you write incorrect code, it will function incorrectly.  Is this
  115. news to anyone?
  116.  
  117. >    The keyword causes inefficient code to be generated because any
  118. >    reference or store into a volatile variable must be immediately
  119. >    reflected in all other streams of execution, defeating any
  120. >    optimization or caching.
  121.  
  122. Dead wrong.  Consider:
  123.  
  124.     int *p = malloc (sizeof (int));
  125.     volatile int *vp = p;
  126.  
  127. Now assume that two separate and independent threads diverge from this point
  128. onward.  One of these two accesses the "heap" variable indirectly via the
  129. pointer "p".  The other accesses the same variable via the pointer "vp".
  130. If the second thread stores indirectly through "vp" that store must be
  131. reflected immediately as having taken place at that point (a sequence
  132. point) for that thread only.  The other thread (or threads) need not
  133. become aware of the store until they next reach one of their own sequence
  134. points (i.e. definitely not "immediately" as is claimed).
  135.  
  136. So the judicious use of the volatile keyword need not necessarily lead to
  137. needless inefficiencies.  Further, it is *not* true that optimizations
  138. must be "defeated".  In the example I gave, optimizations (including
  139. code motion) could still be applied liberally within the second thread.
  140.  
  141. The issue of the effect on caching efficiency of the use of "volatile" is
  142. potentially a real one, but *only* for multiprocessors (or uni-processors
  143. with write-back caches), and *only* when certain types of cache coherency
  144. schemes or certain types of cache coherency hardware is used on the
  145. multiprocessors in question.  Even for such cases however, the effects
  146. will probably be small for any realistic programs on good hardware.
  147.  
  148. >    The volatile keyword is intended
  149. >    primarily for communication with I/O devices, not for
  150. >    communication between two streams of execution.
  151.  
  152. Does it say that in the ANSI C standard somewhere?  Does it say that in the
  153. ANSI C rationale?  If not, then where does this idea come from?
  154.  
  155. >    "A much better method is to specify a set of requirements on the
  156. >    translator and on the programmer minimum restrictions (it can
  157. >    re-arrange and caches accesses_, but guarantee that data will be
  158. >    consistent with respect to synchronization points.  The memory
  159. >    model provides a set of formal specifications that could be used
  160. >    by a C translator. It is our recommendation that something
  161. >    similar become part of the ANSI C definition."
  162. >
  163. >So, my question is, is the 1003.4 position controversial, or can I use
  164. >it to complain to compiler vendors?
  165.  
  166. "Controversial" is not the adjective I had in mind.
  167.  
  168. If these folks want to do language design, perhaps they should wait for the
  169. next revision of the ANSI C standard and see if anybody on the ANSI C
  170. committee will agree with these ? ideas.
  171.  
  172. Volume-Number: Volume 19, Number 32
  173.  
  174.