You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.4KB

  1. #include <sstream>
  2. #include "Squinky.hpp"
  3. #ifdef _CPU_HOG
  4. #include "WidgetComposite.h"
  5. #include "ThreadClient.h"
  6. #include "ThreadServer.h"
  7. #include "ThreadSharedState.h"
  8. /** The following block of constants control what
  9. * this plugin does. Change them and re-build
  10. */
  11. static const int numLoadThreads = 1;
  12. static const int drawMillisecondSleep = 0;
  13. static std::atomic<bool> drawIsSleeping;
  14. /**
  15. * Implementation class for BootyModule
  16. */
  17. struct CPU_HogModule : Module
  18. {
  19. CPU_HogModule();
  20. ~CPU_HogModule();
  21. /**
  22. * Overrides of Module functions
  23. */
  24. void step() override;
  25. int stepsWhileDrawing = 0;
  26. private:
  27. typedef float T;
  28. std::vector< std::shared_ptr<ThreadClient> > threads;
  29. };
  30. class PServer : public ThreadServer
  31. {
  32. public:
  33. PServer(std::shared_ptr<ThreadSharedState> state)
  34. : ThreadServer(state)
  35. {
  36. }
  37. virtual void threadFunction() override;
  38. ~PServer()
  39. {
  40. }
  41. private:
  42. bool didRun = false;
  43. double dummy = 0;
  44. };
  45. void PServer::threadFunction()
  46. {
  47. sharedState->serverRunning = true;
  48. for (bool done = false; !done; ) {
  49. if (sharedState->serverStopRequested.load()) {
  50. done = true;
  51. } else {
  52. // now kill a lot of time
  53. for (int i = 0; i < 10000; ++i) {
  54. dummy += std::log(rand()) * std::sin(rand());
  55. }
  56. }
  57. }
  58. thread->detach();
  59. sharedState->serverRunning = false;
  60. }
  61. CPU_HogModule::CPU_HogModule() : Module(0, 0, 0, 0)
  62. {
  63. for (int i = 0; i < numLoadThreads; ++i) {
  64. std::shared_ptr<ThreadSharedState> state = std::make_shared<ThreadSharedState>();
  65. std::unique_ptr<ThreadServer> server(new PServer(state));
  66. threads.push_back(
  67. std::make_shared<ThreadClient>(
  68. state,
  69. std::move(server)));
  70. }
  71. // TODO: can we assume onSampleRateChange() gets called first, so this is unnecessary?
  72. onSampleRateChange();
  73. }
  74. CPU_HogModule::~CPU_HogModule()
  75. {
  76. threads.resize(0);
  77. }
  78. void CPU_HogModule::step()
  79. {
  80. if (drawIsSleeping) {
  81. stepsWhileDrawing++;
  82. }
  83. }
  84. ////////////////////
  85. // module widget
  86. ////////////////////
  87. struct CPU_HogWidget : ModuleWidget
  88. {
  89. CPU_HogWidget(CPU_HogModule *);
  90. void draw(NVGcontext *vg) override
  91. {
  92. const CPU_HogModule* pMod = static_cast<const CPU_HogModule*>(module);
  93. std::stringstream s;
  94. s << pMod->stepsWhileDrawing;
  95. steps->text = s.str();
  96. ModuleWidget::draw(vg);
  97. if (drawMillisecondSleep) {
  98. drawIsSleeping = true;
  99. std::this_thread::sleep_for(std::chrono::milliseconds(drawMillisecondSleep));
  100. drawIsSleeping = false;
  101. }
  102. }
  103. Label* steps;
  104. };
  105. /**
  106. * Widget constructor will describe my implementation structure and
  107. * provide meta-data.
  108. * This is not shared by all modules in the DLL, just one
  109. */
  110. CPU_HogWidget::CPU_HogWidget(CPU_HogModule *module) : ModuleWidget(module)
  111. {
  112. box.size = Vec(6 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  113. {
  114. SVGPanel *panel = new SVGPanel();
  115. panel->box.size = box.size;
  116. panel->setBackground(SVG::load(assetPlugin(plugin, "res/cpu_hog_panel.svg")));
  117. addChild(panel);
  118. }
  119. Label* label = new Label();
  120. label->box.pos = Vec(10, 140);
  121. label->text = "SleepSteps";
  122. label->color = COLOR_BLACK;
  123. addChild(label);
  124. steps = new Label();
  125. steps->box.pos = Vec(10, 180);
  126. steps->text = "";
  127. steps->color = COLOR_BLACK;
  128. addChild(steps);
  129. // screws
  130. addChild(Widget::create<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  131. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  132. addChild(Widget::create<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  133. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  134. }
  135. // Specify the Module and ModuleWidget subclass, human-readable
  136. // manufacturer name for categorization, module slug (should never
  137. // change), human-readable module name, and any number of tags
  138. // (found in `include/tags.hpp`) separated by commas.
  139. RACK_PLUGIN_MODEL_INIT(squinkylabs_plug1, CPU_hog) {
  140. Model *modelCPU_HogModule = Model::create<CPU_HogModule, CPU_HogWidget>("Squinky Labs",
  141. "squinkylabs-cpuhog",
  142. "CPU Hog", EFFECT_TAG);
  143. return modelCPU_HogModule;
  144. }
  145. #endif