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

SnowWorkerM1.c 5.5 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码翻译:amuluowin
  4. * 代码修订:yitter
  5. * 开源地址:https://github.com/yitter/idgenerator
  6. */
  7. #include <malloc.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include <sys/time.h>
  11. #include <unistd.h>
  12. #include "SnowWorkerM1.h"
  13. pthread_mutex_t ThreadMutex = PTHREAD_MUTEX_INITIALIZER;
  14. static void EndOverCostAction(int64_t useTimeTick, SnowFlakeWorker *worker);
  15. static int64_t NextOverCostId(SnowFlakeWorker *worker);
  16. static int64_t NextNormalId(SnowFlakeWorker *worker);
  17. static int64_t CalcId(SnowFlakeWorker *worker);
  18. static int64_t CalcTurnBackId(SnowFlakeWorker *worker);
  19. static inline void EndOverCostAction(int64_t useTimeTick, SnowFlakeWorker *worker) {
  20. if (worker->_TermIndex > 10000) {
  21. worker->_TermIndex = 0;
  22. }
  23. }
  24. static inline int64_t NextOverCostId(SnowFlakeWorker *worker) {
  25. uint64_t currentTimeTick = GetCurrentTimeTick(worker);
  26. if (currentTimeTick > worker->_LastTimeTick) {
  27. EndOverCostAction(currentTimeTick, worker);
  28. worker->_LastTimeTick = currentTimeTick;
  29. worker->_CurrentSeqNumber = worker->MinSeqNumber;
  30. worker->_IsOverCost = false;
  31. worker->_OverCostCountInOneTerm = 0;
  32. worker->_GenCountInOneTerm = 0;
  33. return CalcId(worker);
  34. }
  35. if (worker->_OverCostCountInOneTerm > worker->TopOverCostCount) {
  36. EndOverCostAction(currentTimeTick, worker);
  37. worker->_LastTimeTick = GetNextTimeTick(worker);
  38. worker->_CurrentSeqNumber = worker->MinSeqNumber;
  39. worker->_IsOverCost = false;
  40. worker->_OverCostCountInOneTerm = 0;
  41. worker->_GenCountInOneTerm = 0;
  42. return CalcId(worker);
  43. }
  44. if (worker->_CurrentSeqNumber > worker->MaxSeqNumber) {
  45. worker->_LastTimeTick++;
  46. worker->_CurrentSeqNumber = worker->MinSeqNumber;
  47. worker->_IsOverCost = true;
  48. worker->_OverCostCountInOneTerm++;
  49. worker->_GenCountInOneTerm++;
  50. return CalcId(worker);
  51. }
  52. worker->_GenCountInOneTerm++;
  53. return CalcId(worker);
  54. }
  55. static inline int64_t NextNormalId(SnowFlakeWorker *worker) {
  56. uint64_t currentTimeTick = GetCurrentTimeTick(worker);
  57. if (currentTimeTick < worker->_LastTimeTick) {
  58. if (worker->_TurnBackTimeTick < 1) {
  59. worker->_TurnBackTimeTick = worker->_LastTimeTick - 1;
  60. }
  61. worker->_TurnBackIndex++;
  62. if (worker->_TurnBackIndex > 4) {
  63. worker->_TurnBackIndex = 1;
  64. }
  65. // usleep(1000); // 暂停1ms
  66. return CalcTurnBackId(worker);
  67. }
  68. if (worker->_TurnBackTimeTick > 0) {
  69. worker->_TurnBackTimeTick = 0;
  70. }
  71. if (currentTimeTick > worker->_LastTimeTick) {
  72. worker->_LastTimeTick = currentTimeTick;
  73. worker->_CurrentSeqNumber = worker->MinSeqNumber;
  74. return CalcId(worker);
  75. }
  76. if (worker->_CurrentSeqNumber > worker->MaxSeqNumber) {
  77. worker->_TermIndex++;
  78. worker->_LastTimeTick++;
  79. worker->_CurrentSeqNumber = worker->MinSeqNumber;
  80. worker->_IsOverCost = true;
  81. worker->_OverCostCountInOneTerm = 1;
  82. worker->_GenCountInOneTerm = 1;
  83. return CalcId(worker);
  84. }
  85. return CalcId(worker);
  86. }
  87. static inline int64_t CalcId(SnowFlakeWorker *worker) {
  88. uint64_t result = (worker->_LastTimeTick << worker->_TimestampShift) | (worker->WorkerId << worker->SeqBitLength) |
  89. (worker->_CurrentSeqNumber);
  90. worker->_CurrentSeqNumber++;
  91. return result;
  92. }
  93. static inline int64_t CalcTurnBackId(SnowFlakeWorker *worker) {
  94. uint64_t result = (worker->_LastTimeTick << worker->_TimestampShift) | (worker->WorkerId << worker->SeqBitLength) |
  95. (worker->_TurnBackTimeTick);
  96. worker->_TurnBackTimeTick--;
  97. return result;
  98. }
  99. extern SnowFlakeWorker *NewSnowFlakeWorker() {
  100. SnowFlakeWorker *worker = (SnowFlakeWorker *) malloc(sizeof(SnowFlakeWorker));
  101. worker->_IsOverCost = false;
  102. worker->_LastTimeTick = 0;
  103. worker->_TurnBackTimeTick = 0;
  104. worker->_TurnBackIndex = 0;
  105. worker->_OverCostCountInOneTerm = 0;
  106. worker->_GenCountInOneTerm = 0;
  107. worker->_TermIndex = 0;
  108. return worker;
  109. }
  110. extern int64_t WorkerM1NextId(SnowFlakeWorker *worker) {
  111. pthread_mutex_lock(&ThreadMutex);
  112. int64_t id = worker->_IsOverCost ? NextOverCostId(worker) : NextNormalId(worker);
  113. pthread_mutex_unlock(&ThreadMutex);
  114. return id;
  115. }
  116. extern int64_t GetCurrentTimeTick(SnowFlakeWorker *worker) {
  117. struct timeval tv;
  118. gettimeofday(&tv, NULL);
  119. return ((int64_t) tv.tv_sec * 1000 + tv.tv_usec / 1000 - worker->BaseTime);
  120. }
  121. extern int64_t GetCurrentTime() {
  122. struct timeval tv;
  123. gettimeofday(&tv, NULL);
  124. return ((int64_t) (tv.tv_sec)) * 1000 + tv.tv_usec / 1000;
  125. //static struct timeb t1;
  126. // ftime(&t1);
  127. // return (uint64_t) ((t1.time * 1000 + t1.millitm));
  128. }
  129. extern int64_t GetCurrentMicroTime() {
  130. struct timeval tv;
  131. gettimeofday(&tv, NULL);
  132. return ((int64_t) tv.tv_sec * 1000000 + tv.tv_usec);
  133. }
  134. extern int64_t GetNextTimeTick(SnowFlakeWorker *worker) {
  135. uint64_t tempTimeTicker = GetCurrentTimeTick(worker);
  136. while (tempTimeTicker <= worker->_LastTimeTick) {
  137. usleep(1000); // 暂停1ms
  138. tempTimeTicker = GetCurrentTimeTick(worker);
  139. }
  140. return tempTimeTicker;
  141. }