Advertisement
brucehoult

d2c and atomicity

Feb 4th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. Examples of use of d2c's two word "General Representation".
  2.  
  3. Int32, character, and float (single) values are stored in the "dataword" field, with the heapptr field containing a pointer to a single global proxy object for the appropriate class.
  4.  
  5. Objects of all other classes (including #T and #F) are stored simply as a pointer to the heap allocated object placed in the heapptr field. The dataword field is unused (the assignment to 0 is redundant).
  6.  
  7. There is no possible race condition in assigning a heap allocated object to a two-word descriptor. Storing the pointer into .heapptr sets both the type and the value.
  8.  
  9. There is a possible race condition in assigning an integer, character, or float to a two-word descriptor. One thread might assign the new type and then before it writes the new value another thread could read the new type and the old value.
  10.  
  11. Thus it is possible for the bits of a float to be interpreted as an int or char. Or for an int or char to be interpreted as a float.
  12.  
  13. While this can cause incorrect results, it can never cause an out of language exception. (sole possible exception: signalling NANs. However I believe very few programs enable this IEEE feature.)
  14.  
  15. There is *zero* possibility of an int or float being interpreted as a pointer to a heap allocated object, which is I think the danger that people imagine exists.
  16.  
  17. --------------- Dylan code ---------------
  18. define function foo(i :: <integer>) => res;
  19. i;
  20. end;
  21.  
  22. define function bar(s :: <byte-string>) => res;
  23. s;
  24. end;
  25.  
  26. define function baz(c :: <character>) => res;
  27. c;
  28. end;
  29. --------------- generated C (slightly cleaned up) ---------------
  30. extern descriptor_t genrepZliteral; /* proxy for <integer> */
  31. extern descriptor_t genrepZliteral_2; /* proxy for <character> */
  32.  
  33. descriptor_t genrepZgenrepZfoo_FUN(descriptor_t *orig_sp, long A_i /* i */)
  34. {
  35. descriptor_t L_temp;
  36. L_temp.heapptr = genrepZliteral.heapptr;
  37. L_temp.dataword.l = A_i;
  38. return L_temp;
  39. }
  40.  
  41. descriptor_t genrepZgenrepZbar_FUN(descriptor_t *orig_sp, heapptr_t A_s /* s */)
  42. {
  43. descriptor_t L_temp;
  44. L_temp.heapptr = A_s;
  45. L_temp.dataword.l = 0;
  46. return L_temp;
  47. }
  48.  
  49. descriptor_t genrepZgenrepZbaz_FUN(descriptor_t *orig_sp, long A_c /* c */)
  50. {
  51. descriptor_t L_temp;
  52. L_temp.heapptr = genrepZliteral_2.heapptr;
  53. L_temp.dataword.l = A_c;
  54. return L_temp;
  55. }
  56. ---------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement