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.

ExecutiveProcess.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SNF4CGP/ExecutiveProcess.cpp
  2. // Copyright (C) 2009 ARM Research Labs, LLC.
  3. // See www.armresearch.com for more information.
  4. #include "ExecutiveProcess.hpp"
  5. #include "../CodeDweller/faults.hpp"
  6. #include <iostream>
  7. #include <string>
  8. #include <sstream>
  9. using namespace std;
  10. void ExecutiveProcess::initializeOutput() {
  11. Output.init(Jobs);
  12. }
  13. void ExecutiveProcess::initializeJobPool() {
  14. Jobs.init(VersionInfo, ConfigInfo, Output);
  15. }
  16. void ExecutiveProcess::initializeWorkerPool() {
  17. Workers.init();
  18. }
  19. void ExecutiveProcess::shutdownWorkerPool() {
  20. Workers.stop();
  21. }
  22. void ExecutiveProcess::shutdownJobPool() {
  23. Jobs.stop();
  24. }
  25. void ExecutiveProcess::shutdownOutput() {
  26. Output.stop();
  27. }
  28. void ExecutiveProcess::dispatchCommand(Command& C) { // Job + Worker + Command; go!
  29. Job& J = Jobs.grab();
  30. J.setCommand(C);
  31. Worker& W = Workers.grab();
  32. W.doJob(J);
  33. }
  34. ExecutiveProcess::ExecutiveProcess(const string& Version, const string& Config) : // Simple construction.
  35. VersionInfo(Version),
  36. ConfigInfo(Config),
  37. QuitJobNumber(0) {}
  38. ExecutiveProcess::~ExecutiveProcess() { // Deal with uncerimoneous shutdowns.
  39. }
  40. void ExecutiveProcess::doStartup() { // Startup happens in this sequence.
  41. initializeJobPool();
  42. initializeOutput();
  43. initializeWorkerPool();
  44. }
  45. void ExecutiveProcess::dispatchWakeupCommand() {
  46. Command C;
  47. C.Type = Command::WAKE; // Create the WAKE command to announce
  48. C.Data = VersionInfo; // that we are alive (and who we are)
  49. dispatchCommand(C); // and dispatch a job with it.
  50. }
  51. void ExecutiveProcess::notifyQUIT() {
  52. ostringstream O;
  53. O << "* SNF4CGP[" << QuitJobNumber << "] Received QUIT, shutting down..." << endl;
  54. Output.sendString(O.str());
  55. }
  56. void ExecutiveProcess::notifyNOTGOOD() {
  57. ostringstream O;
  58. O << "* SNF4CGP Input stream has gone bad, shutting down..." << endl;
  59. Output.sendString(O.str());
  60. }
  61. void ExecutiveProcess::doProcessing() { // Command processing happens here.
  62. dispatchWakeupCommand(); // First job is to announce ourselves.
  63. for(;;) { // Then process all jobs but QUIT.
  64. Command C = Input.getCommand();
  65. switch(C.Type) {
  66. case Command::QUIT: {
  67. QuitJobNumber = C.Number;
  68. notifyQUIT();
  69. return;
  70. }
  71. case Command::NOTGOOD: {
  72. notifyNOTGOOD();
  73. return;
  74. }
  75. default: {
  76. dispatchCommand(C);
  77. continue;
  78. }
  79. }
  80. }
  81. }
  82. void ExecutiveProcess::doShutdown() { // Shutdown happens in this sequence.
  83. shutdownWorkerPool();
  84. shutdownOutput();
  85. shutdownJobPool();
  86. cout << "* SNF4CGP Shutdown completed." << endl;
  87. if(0 < QuitJobNumber) { // If processing ended with QUIT
  88. cout << QuitJobNumber << " OK" << endl; // then respond appropriately.
  89. }
  90. cout.flush();
  91. }