Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implementation of test cases using atomic and non-atomic cmpxchg.
- *
- * Author: Andreas Sandberg
- *
- */
- #include "lab3.h"
- #include "lab3_asm.h"
- static volatile int32_t x = 0;
- static void
- increase(int thread, int iterations, volatile int *data)
- {
- int i;
- for (i = 0; i< iterations; i++){
- while(asm_cmpxchg_int32((int32_t*)&x, 0, 1));
- asm_inc_int32((int32_t*)data);
- x=0;
- }
- /* TASK: Implement a loop that increments *data by 1 using
- * non-atomic compare and exchange instructions. See lab3_asm.h.
- */
- }
- static void
- decrease(int thread, int iterations, volatile int *data)
- {
- int i;
- for(i = 0; i < iterations ; i++){
- while(asm_cmpxchg_int32((int32_t*)&x, 0, 1));
- asm_dec_int32((int32_t*)data);
- x=0;
- }
- /* TASK: Implement a loop that decrements *data by 1 using
- * non-atomic compare and exchange instructions. See lab3_asm.h.
- */
- }
- static void
- increase_atomic(int thread, int iterations, volatile int *data)
- {
- int i;
- for (i = 0; i< iterations; i++){
- while(asm_atomic_cmpxchg_int32((int32_t*)&x, 0, 1));
- asm_atomic_inc_int32((int32_t*)data);
- x=0;
- }
- /* TASK: Implement a loop that increments *data by 1 using
- * atomic compare and exchange instructions. See lab3_asm.h.
- */
- }
- static void
- decrease_atomic(int thread, int iterations, volatile int *data)
- {
- int i;
- for (i = 0; i< iterations; i++){
- while(asm_atomic_cmpxchg_int32((int32_t*)&x, 0, 1));
- asm_atomic_inc_int32((int32_t*)data);
- x=0;
- }
- /* TASK: Implement a loop that decrements *data by 1 using
- * atomic compare and exchange instructions. See lab3_asm.h.
- */
- }
- test_impl_t test_impl_cmpxchg_no_atomic = {
- .name = "cmpxchg_no_atomic",
- .desc = "Modify a shared variable using compare and exchange",
- .num_threads = 2,
- .test = { &increase, &decrease }
- };
- test_impl_t test_impl_cmpxchg_atomic = {
- .name = "cmpxchg_atomic",
- .desc = "Modify a shared variable using atomic compare and exchange",
- .num_threads = 2,
- .test = { &increase_atomic, &decrease_atomic }
- };
- /*
- * Local Variables:
- * mode: c
- * c-basic-offset: 8
- * indent-tabs-mode: nil
- * c-file-style: "linux"
- * End:
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement