Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #include "domaci1.h"
  6.  
  7. #pragma clang diagnostic push
  8. #pragma ide diagnostic ignored "OCDFAInspection"
  9. typedef struct cvor {
  10. int flag;
  11. struct cvor *sledeci;
  12. int to;
  13. } cvor;
  14.  
  15. cvor *tail = NULL;
  16.  
  17.  
  18. void unlock_flag(cvor *c) {
  19. if (!c->to) {
  20. cvor *sl = c->sledeci;
  21. if(sl==NULL) {
  22. if (lrk_compare_and_set((int *) &tail, (int) c, (int) NULL)) {
  23. free(c);
  24. } else {
  25. while (c->sledeci == NULL) {
  26. lrk_sleep(1);
  27. }
  28. printf("ovde");
  29. free(c);
  30. sl->flag=0;
  31. }
  32. }
  33. else {
  34. free(c);
  35. unlock_flag(sl);
  36. }
  37. }
  38. else {
  39. if (!lrk_compare_and_set(&(*c).flag, (c->to), 0)) {
  40. unlock_flag(c);
  41. }
  42. }
  43. }
  44.  
  45. void unlock_n_threads_with_timeout(int id, int *local) {
  46. cvor *stari = (cvor *) local[0];
  47. if (stari->sledeci) {
  48. cvor *sl = stari->sledeci;
  49. free(stari);
  50. unlock_flag(sl);
  51. } else {
  52. if (lrk_compare_and_set((int *) &tail, (int) stari, (int) NULL)) {
  53. free(stari);
  54. } else {
  55. while (stari->sledeci == NULL) {
  56. lrk_sleep(1);
  57. }
  58. cvor *sl = stari->sledeci;
  59. free(stari);
  60. sl->flag = 0;
  61. }
  62. }
  63. }
  64.  
  65. int lock_n_threads_with_timeout(int id, int *local, int timeout) {
  66. double time_started = lrk_get_time() * 1000;
  67. cvor *moj = (cvor *) malloc(sizeof(cvor));
  68. moj->sledeci = NULL;
  69. moj->flag = 1;
  70. moj->to = 1;
  71. cvor *stari = (cvor *) lrk_get_and_set((int *) &tail, (int) moj);
  72. local[0] = (int) moj;
  73. if (stari != NULL) {
  74. stari->sledeci = moj;
  75. while (moj->flag) {
  76. if (lrk_get_time() * 1000 - time_started > timeout) {
  77. moj->to = 0;
  78. if (moj->flag==0) {
  79. unlock_n_threads_with_timeout(id, local);
  80. /* if (moj->sledeci == NULL) {
  81. // if (compare_and_set((int *) &tail, (int) moj, (int) NULL)) {
  82. // free(moj);
  83. // } else {
  84. // while (moj->sledeci == NULL) {
  85. // lrk_sleep(1);
  86. // }
  87. // cvor *sl = moj->sledeci;
  88. // free(moj);
  89. // unlock_flag(sl);
  90. // }
  91. // } else {
  92. // unlock_flag(moj->sledeci);
  93. // free(moj);
  94. // } */
  95. }
  96. return 0;
  97. }
  98. lrk_sleep(1);
  99. }
  100. }
  101.  
  102. // If timeout is too low
  103. // if (get_time() * 1000 - time_started > timeout) {
  104. // moj->to = 0;
  105. // if (moj->flag==0) {
  106. // unlock_n_threads_with_timeout(id, local);
  107. // }
  108. // return 0;
  109. // }
  110. return 1;
  111. }
  112.  
  113. int main(int argc, char *argv[]) {
  114. int i = 200;
  115. if (argc==2) {
  116. i = atoi(argv[1]);
  117. }
  118. printf("Starting test with %d timeout:\n", i);
  119. // for (int i = 0; i < 10; ++i) {
  120. tail = NULL;
  121. start_timeout_mutex_n_threads_test(i);
  122. // }
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement