Projekty z kursu Systemy operacyjne 2 na PWr
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <iostream>
  2. #include <mutex>
  3. #include "Entry.h"
  4. bool Entry::initialized = false;
  5. int Entry::xMax;
  6. int Entry::yMax;
  7. bool full;
  8. std::mutex state;
  9. std::mutex ready1;
  10. std::mutex ready2;
  11. std::mutex service;
  12. Entry::Entry(int speedRate)
  13. {
  14. //ctor
  15. this->xPosition = -1;
  16. this->yPosition = yMax / 2;
  17. this->speedRate = speedRate;
  18. this->symbol = "*";
  19. if(initialized == false)
  20. {
  21. std::cout << "WARNING: Scene size not initialized!" << std::endl;
  22. }
  23. }
  24. Entry::~Entry()
  25. {
  26. //dtor
  27. }
  28. void Entry::initScene(int xRes, int yRes)
  29. {
  30. xMax = xRes;
  31. yMax = yRes;
  32. initialized = true;
  33. }
  34. int Entry::getxPosition()
  35. {
  36. return xPosition;
  37. }
  38. int Entry::getyPosition()
  39. {
  40. return yPosition;
  41. }
  42. std::string Entry::getSymbol()
  43. {
  44. return symbol;
  45. }
  46. void Entry::run()
  47. {
  48. if(initialized)
  49. {
  50. while(xPosition < xMax / 2)
  51. {
  52. xPosition++;
  53. usleep(100000 - 10000 * speedRate);
  54. }
  55. bool goAround;
  56. state.lock();
  57. if(full == true)
  58. {
  59. goAround = true;
  60. }
  61. else
  62. {
  63. goAround = false;
  64. full = true;
  65. ready2.lock();
  66. }
  67. state.unlock();
  68. while(goAround)
  69. {
  70. while(yPosition < 3 * yMax / 4)
  71. {
  72. yPosition++;
  73. usleep(100000 - 10000 * speedRate);
  74. }
  75. while(xPosition < 3 * xMax / 4)
  76. {
  77. xPosition++;
  78. usleep(100000 - 10000 * speedRate);
  79. }
  80. while(yPosition > yMax / 4)
  81. {
  82. yPosition--;
  83. usleep(100000 - 10000 * speedRate);
  84. }
  85. while(xPosition > xMax / 2)
  86. {
  87. xPosition--;
  88. usleep(100000 - 10000 * speedRate);
  89. }
  90. while(yPosition < yMax / 2)
  91. {
  92. yPosition++;
  93. usleep(100000 - 10000 * speedRate);
  94. }
  95. state.lock();
  96. if(full == false)
  97. {
  98. goAround = false;
  99. full = true;
  100. ready2.lock();
  101. }
  102. state.unlock();
  103. }
  104. xPosition++;
  105. symbol = "2";
  106. ready1.lock();
  107. state.lock();
  108. full = false;
  109. state.unlock();
  110. ready2.unlock();
  111. xPosition++;
  112. symbol = "1";
  113. service.lock();
  114. ready1.unlock();
  115. xPosition++;
  116. symbol = "#";
  117. sleep(5);
  118. service.unlock();
  119. symbol = "*";
  120. while(xPosition < xMax)
  121. {
  122. xPosition++;
  123. usleep(100000 - 10000 * speedRate);
  124. }
  125. }
  126. else
  127. {
  128. std::cout << "ERROR: Scene size not initialized!" << std::endl;
  129. }
  130. }
  131. std::thread Entry::runThread()
  132. {
  133. return std::thread(&Entry::run, this);
  134. }