job.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. enum class JobState {
  20. initialized, //Job was created
  21. ready, //Job is ready to be sent
  22. sent, //sendBuf has been copied to sendQueue
  23. receiving, //first part of response has been received
  24. finished, //Job has been successfully received
  25. failed, //Job failed too many times, throw exception
  26. };
  27. //generic uint32 storage
  28. class WordBuffer {
  29. public:
  30. WordBuffer(size_t length);
  31. ~WordBuffer();
  32. uint8_t* getByteAddr() const {return bytes;}
  33. uint32_t* getWordAddr() const {return words;}
  34. uint8_t getByte(size_t i) const {return bytes[i];}
  35. void setByte(size_t i, uint32_t v) const {bytes[i] = v;}
  36. uint32_t getWord(size_t i) const {return words[i];}
  37. void setWord(size_t i, uint32_t v) const {words[i] = v;}
  38. size_t getWordCount() const {return wordCount;}
  39. size_t getByteCount() const {return wordCount*4;}
  40. private:
  41. size_t wordCount;
  42. union {
  43. uint8_t *bytes;
  44. uint32_t *words;
  45. };
  46. };
  47. //wrapper for data structure that is sent over the network
  48. class JobData : public WordBuffer {
  49. public:
  50. JobData(uint payloadLength);
  51. uint32_t getPreamble() const {return getWord(0);}
  52. void setPreamble(uint32_t v) const {setWord(0, v);}
  53. uint32_t getJobId() const {return getWord(1);}
  54. void setJobId(uint32_t v) const {setWord(1, v);}
  55. uint32_t getModuleId() const {return getWord(2);}
  56. void setModuleId(uint32_t v) const {setWord(2, v);}
  57. uint32_t getPayload(size_t i) const {return getWord(i+3);}
  58. void setPayload(size_t i, uint32_t v) const {setWord(i+3, v);}
  59. size_t getPayloadSize() const {return getWordCount()-4;}
  60. uint32_t getCRC() const {return getWord(getWordCount()-1);}
  61. void setCRC(uint32_t v) const {setWord(getWordCount()-1, v);}
  62. };
  63. //entity to track a single Job
  64. class Job : public JobData {
  65. public:
  66. Job(Module mod);
  67. uint32_t tag = 0;
  68. uint32_t getResponsePayload(size_t i) const {return recvBuf.getWord(i);}
  69. void setResponsePayload(size_t i, uint32_t v) const {recvBuf.setWord(i, v);}
  70. size_t getResponsePayloadSize() const {return recvBuf.getWordCount()-1;}
  71. uint32_t* getResponseAddr() const {return recvBuf.getWordAddr();}
  72. size_t getResponseBufferWordCount() const {return recvBuf.getWordCount();}
  73. void calcCRC();
  74. bool checkCRC();
  75. JobState getState() const {return state;}
  76. void setState(JobState s) {state = s;}
  77. void setReady();
  78. void setSent();
  79. void setReceived(const bool success);
  80. void setDoneCallback(DoneCallback cb);
  81. Clock::time_point getSent() const {return sent;}
  82. Clock::time_point getReceived() const {return received;}
  83. void* getAssignedFPGA() const {return assignedFPGA;}
  84. void setAssignedFPGA(void *fpga) {assignedFPGA = fpga;}
  85. size_t getSendCounter() const {return sendCounter;}
  86. std::mutex jobLock;
  87. private:
  88. //only payload and CRC of response
  89. WordBuffer recvBuf;
  90. DoneCallback doneCb = NULL;
  91. JobState state = JobState::initialized;
  92. Clock::time_point sent;
  93. Clock::time_point received;
  94. void *assignedFPGA = NULL;
  95. size_t sendCounter = 0;
  96. };
  97. //thread safe Job container
  98. class JobContainer {
  99. public:
  100. JobContainer(std::shared_ptr<Job> &p) : job(p), lock(p->jobLock) {
  101. };
  102. Job * operator->()const { return job.get(); }
  103. Job & operator*() const { return *job; }
  104. std::shared_ptr<Job>& sharedPtr() {return job;}
  105. private:
  106. std::shared_ptr<Job> job;
  107. std::unique_lock<std::mutex> lock;
  108. };
  109. #endif