job.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef myJOB_H
  2. #define myJOB_H
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <sys/types.h>
  7. #include <functional>
  8. #include <cstring>
  9. #include <mutex>
  10. #include <condition_variable>
  11. #include <chrono>
  12. #include "modules.hpp"
  13. #include "rng.hpp"
  14. typedef std::chrono::high_resolution_clock Clock;
  15. typedef std::chrono::milliseconds milliseconds;
  16. typedef std::chrono::microseconds microseconds;
  17. typedef std::function<void()> DoneCallback;
  18. #define PREAMBLE (0xE1E4C312)
  19. #define MAX_JOB_LEN (256*256)
  20. enum class JobState {
  21. initialized, //Job was created
  22. ready, //Job is ready to be sent
  23. sent, //sendBuf has been copied to sendQueue
  24. receiving, //first part of response has been received
  25. finished, //Job has been successfully received
  26. failed, //Job failed too many times, throw exception
  27. };
  28. //generic uint32 storage
  29. class WordBuffer {
  30. public:
  31. WordBuffer(size_t length);
  32. ~WordBuffer();
  33. uint8_t* getByteAddr() const {return bytes;}
  34. uint32_t* getWordAddr() const {return words;}
  35. uint8_t getByte(size_t i) const {return bytes[i];}
  36. uint8_t getWord(size_t i) const {return words[i];}
  37. size_t getWordCount() const {return wordCount;}
  38. size_t getByteCount() const {return wordCount*4;}
  39. protected:
  40. size_t wordCount;
  41. union {
  42. uint8_t *bytes;
  43. uint32_t *words;
  44. };
  45. };
  46. //data structure that is sent over the network
  47. class JobData : public WordBuffer {
  48. public:
  49. JobData(uint payloadLength);
  50. uint32_t getPreamble() const {return words[0];}
  51. void setPreamble(uint32_t v) const {words[0] = v;}
  52. uint32_t getJobId() const {return words[1];}
  53. void setJobId(uint32_t v) const {words[1] = v;}
  54. uint32_t getModuleId() const {return words[2];}
  55. void setModuleId(uint32_t v) const {words[2] = v;}
  56. uint32_t getPayload(size_t i) const {return words[i+3];}
  57. void setPayload(size_t i, uint32_t v) const {words[i+3] = v;}
  58. uint32_t getCRC() const {return words[wordCount-1];}
  59. void setCRC(uint32_t v) const {words[wordCount-1] = v;}
  60. };
  61. //entity to track a single Job
  62. class Job : public JobData {
  63. public:
  64. Job(Module mod);
  65. uint32_t tag = 0;
  66. //locks state
  67. std::mutex stateMutex;
  68. //locks sendBuf
  69. std::mutex sendMutex;
  70. //locks recvBuf, recvWordIndex
  71. std::mutex recvMutex;
  72. uint32_t getResponsePayload(size_t i) const {return recvBuf.getWord(i);}
  73. void setResponsePayload(size_t i, uint32_t v) const {recvBuf.getWordAddr()[i] = v;}
  74. uint32_t* getResponseAddr() const {return recvBuf.getWordAddr();}
  75. size_t getResponseBufferWordCount() const {return recvBuf.getWordCount();}
  76. void calcCRC();
  77. bool checkCRC();
  78. JobState getState() const {return state;}
  79. void setState(JobState s) {state = s;}
  80. void setReady();
  81. void setSent();
  82. void setReceived(const bool success);
  83. void setDoneCallback(DoneCallback cb);
  84. Clock::time_point getSent() const {return sent;}
  85. Clock::time_point getReceived() const {return received;}
  86. void* getAssignedFPGA() const {return assignedFPGA;}
  87. void setAssignedFPGA(void *fpga) {assignedFPGA = fpga;}
  88. size_t getSendCounter() const {return sendCounter;}
  89. private:
  90. //only payload and CRC of response
  91. WordBuffer recvBuf;
  92. DoneCallback doneCb = NULL;
  93. JobState state = JobState::initialized;
  94. Clock::time_point sent;
  95. Clock::time_point received;
  96. void *assignedFPGA = NULL;
  97. size_t sendCounter = 0;
  98. };
  99. #endif