Advertisement
Guest User

Vraag6

a guest
Dec 15th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. /**
  2.  
  3. * Implementation of test cases using atomic and non-atomic cmpxchg.
  4.  
  5. *
  6.  
  7. * Author: Andreas Sandberg
  8.  
  9. *
  10.  
  11. */
  12.  
  13.  
  14.  
  15. #include "lab3.h"
  16.  
  17.  
  18.  
  19. #include "lab3_asm.h"
  20.  
  21.  
  22.  
  23. static volatile int32_t x = 0;
  24.  
  25.  
  26.  
  27. static void
  28.  
  29. increase(int thread, int iterations, volatile int *data)
  30.  
  31. {
  32.  
  33. int i;
  34.  
  35. for (i = 0; i< iterations; i++){
  36.  
  37. while(asm_cmpxchg_int32((int32_t*)&x, 0, 1));
  38.  
  39. asm_inc_int32((int32_t*)data);
  40.  
  41. x=0;
  42.  
  43. }
  44.  
  45. /* TASK: Implement a loop that increments *data by 1 using
  46.  
  47. * non-atomic compare and exchange instructions. See lab3_asm.h.
  48.  
  49. */
  50.  
  51. }
  52.  
  53.  
  54.  
  55. static void
  56.  
  57. decrease(int thread, int iterations, volatile int *data)
  58.  
  59. {
  60.  
  61. int i;
  62.  
  63. for(i = 0; i < iterations ; i++){
  64.  
  65. while(asm_cmpxchg_int32((int32_t*)&x, 0, 1));
  66.  
  67. asm_dec_int32((int32_t*)data);
  68.  
  69. x=0;
  70.  
  71. }
  72.  
  73.  
  74.  
  75. /* TASK: Implement a loop that decrements *data by 1 using
  76.  
  77. * non-atomic compare and exchange instructions. See lab3_asm.h.
  78.  
  79. */
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. static void
  88.  
  89. increase_atomic(int thread, int iterations, volatile int *data)
  90.  
  91. {
  92.  
  93. int i;
  94.  
  95. for (i = 0; i< iterations; i++){
  96.  
  97. while(asm_atomic_cmpxchg_int32((int32_t*)&x, 0, 1));
  98.  
  99. asm_atomic_inc_int32((int32_t*)data);
  100.  
  101. x=0;
  102.  
  103. }
  104.  
  105.  
  106.  
  107. /* TASK: Implement a loop that increments *data by 1 using
  108.  
  109. * atomic compare and exchange instructions. See lab3_asm.h.
  110.  
  111. */
  112.  
  113. }
  114.  
  115.  
  116.  
  117. static void
  118.  
  119. decrease_atomic(int thread, int iterations, volatile int *data)
  120.  
  121. {
  122.  
  123. int i;
  124.  
  125. for (i = 0; i< iterations; i++){
  126.  
  127. while(asm_atomic_cmpxchg_int32((int32_t*)&x, 0, 1));
  128.  
  129. asm_atomic_inc_int32((int32_t*)data);
  130.  
  131. x=0;
  132.  
  133. }
  134.  
  135. /* TASK: Implement a loop that decrements *data by 1 using
  136.  
  137. * atomic compare and exchange instructions. See lab3_asm.h.
  138.  
  139. */
  140.  
  141. }
  142.  
  143.  
  144.  
  145. test_impl_t test_impl_cmpxchg_no_atomic = {
  146.  
  147. .name = "cmpxchg_no_atomic",
  148.  
  149. .desc = "Modify a shared variable using compare and exchange",
  150.  
  151.  
  152.  
  153. .num_threads = 2,
  154.  
  155. .test = { &increase, &decrease }
  156.  
  157. };
  158.  
  159.  
  160.  
  161. test_impl_t test_impl_cmpxchg_atomic = {
  162.  
  163. .name = "cmpxchg_atomic",
  164.  
  165. .desc = "Modify a shared variable using atomic compare and exchange",
  166.  
  167.  
  168.  
  169. .num_threads = 2,
  170.  
  171. .test = { &increase_atomic, &decrease_atomic }
  172.  
  173. };
  174.  
  175.  
  176.  
  177. /*
  178.  
  179. * Local Variables:
  180.  
  181. * mode: c
  182.  
  183. * c-basic-offset: 8
  184.  
  185. * indent-tabs-mode: nil
  186.  
  187. * c-file-style: "linux"
  188.  
  189. * End:
  190.  
  191. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement