| @@ -18,11 +18,10 @@ add_subdirectory(idgen) | |||||
| # PREFIX "") | # PREFIX "") | ||||
| ##编译执行文件 | ##编译执行文件 | ||||
| #set(LIB_SRC idgen/YitIdHelper.h idgen/YitIdHelper.c) | |||||
| #add_library(YitIdHelper ${LIB_SRC}) | |||||
| set(LIB_SRC YitIdHelper.h YitIdHelper.c) | |||||
| add_library(YitIdHelper ${LIB_SRC}) | |||||
| add_executable(YitIdGen main.c) | add_executable(YitIdGen main.c) | ||||
| #target_link_libraries(YitIdGen YitIdHelper) | |||||
| target_link_libraries(YitIdGen YitIdHelper) | |||||
| target_link_libraries(YitIdGen idgen) | target_link_libraries(YitIdGen idgen) | ||||
| @@ -5,15 +5,15 @@ | |||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <stdint.h> | #include <stdint.h> | ||||
| #include "YitIdHelper.h" | #include "YitIdHelper.h" | ||||
| #include "IdGenerator.h" | |||||
| #include "idgen/IdGenerator.h" | |||||
| extern void SetIdGenerator(IdGeneratorOptions options) { | extern void SetIdGenerator(IdGeneratorOptions options) { | ||||
| SetOptions(options); | SetOptions(options); | ||||
| } | } | ||||
| extern void SetWorkerId(uint32_t workerId) { | extern void SetWorkerId(uint32_t workerId) { | ||||
| // IdGeneratorOptions options = BuildIdGenOptions(workerId); | |||||
| // SetOptions(options); | |||||
| IdGeneratorOptions options = BuildIdGenOptions(workerId); | |||||
| // SetOptions(options); | |||||
| SetIdGenerator(options); | SetIdGenerator(options); | ||||
| } | } | ||||
| @@ -4,8 +4,8 @@ | |||||
| */ | */ | ||||
| #pragma once | #pragma once | ||||
| #include "IdGenOptions.h" | |||||
| #include "common.h" | |||||
| #include "idgen/IdGenOptions.h" | |||||
| #include "idgen/common.h" | |||||
| TAP_DLLEXPORT | TAP_DLLEXPORT | ||||
| @@ -113,6 +113,13 @@ static inline uint64_t CalcTurnBackId(SnowFlakeWorker *worker) { | |||||
| extern SnowFlakeWorker *NewSnowFlakeWorker() { | extern SnowFlakeWorker *NewSnowFlakeWorker() { | ||||
| SnowFlakeWorker *worker = (SnowFlakeWorker *) malloc(sizeof(SnowFlakeWorker)); | SnowFlakeWorker *worker = (SnowFlakeWorker *) malloc(sizeof(SnowFlakeWorker)); | ||||
| worker->_IsOverCost = false; | worker->_IsOverCost = false; | ||||
| worker->_LastTimeTick = 0; | |||||
| worker->_TurnBackTimeTick = 0; | |||||
| worker->_TurnBackIndex = 0; | |||||
| worker->_OverCostCountInOneTerm = 0; | |||||
| worker->_GenCountInOneTerm = 0; | |||||
| worker->_TermIndex = 0; | |||||
| return worker; | return worker; | ||||
| } | } | ||||
| @@ -124,15 +131,15 @@ extern uint64_t WorkerM1NextId(SnowFlakeWorker *worker) { | |||||
| } | } | ||||
| extern uint64_t GetCurrentTimeTick(SnowFlakeWorker *worker) { | extern uint64_t GetCurrentTimeTick(SnowFlakeWorker *worker) { | ||||
| static struct timeval tv; | |||||
| struct timeval tv; | |||||
| gettimeofday(&tv, NULL); | gettimeofday(&tv, NULL); | ||||
| return (uint64_t)(tv.tv_sec * 1000 + tv.tv_usec / 1000 - worker->BaseTime); | |||||
| return ((uint64_t) tv.tv_sec * 1000 + tv.tv_usec / 1000 - worker->BaseTime); | |||||
| } | } | ||||
| extern uint64_t GetCurrentTime() { | extern uint64_t GetCurrentTime() { | ||||
| static struct timeval tv; | |||||
| struct timeval tv; | |||||
| gettimeofday(&tv, NULL); | gettimeofday(&tv, NULL); | ||||
| return (uint64_t)(tv.tv_sec * 1000 + tv.tv_usec / 1000); | |||||
| return ((uint64_t) (tv.tv_sec)) * 1000 + tv.tv_usec / 1000; | |||||
| //static struct timeb t1; | //static struct timeb t1; | ||||
| // ftime(&t1); | // ftime(&t1); | ||||
| @@ -140,9 +147,9 @@ extern uint64_t GetCurrentTime() { | |||||
| } | } | ||||
| extern uint64_t GetCurrentMicroTime() { | extern uint64_t GetCurrentMicroTime() { | ||||
| static struct timeval tv; | |||||
| struct timeval tv; | |||||
| gettimeofday(&tv, NULL); | gettimeofday(&tv, NULL); | ||||
| return (uint64_t)(tv.tv_sec * 1000000 + tv.tv_usec); | |||||
| return ((uint64_t) tv.tv_sec * 1000000 + tv.tv_usec); | |||||
| } | } | ||||
| extern uint64_t GetNextTimeTick(SnowFlakeWorker *worker) { | extern uint64_t GetNextTimeTick(SnowFlakeWorker *worker) { | ||||
| @@ -12,11 +12,11 @@ | |||||
| #include <stdbool.h> | #include <stdbool.h> | ||||
| #include "idgen/SnowWorkerM1.h" | #include "idgen/SnowWorkerM1.h" | ||||
| #include "idgen/IdGenerator.h" | #include "idgen/IdGenerator.h" | ||||
| #include "idgen/YitIdHelper.h" | |||||
| #include "YitIdHelper.h" | |||||
| const int GenIdCount = 50000; | const int GenIdCount = 50000; | ||||
| const bool multiThread = true; | |||||
| const bool multiThread = false; | |||||
| const int threadCount = 50; | const int threadCount = 50; | ||||
| const int method = 1; | const int method = 1; | ||||
| @@ -24,7 +24,7 @@ void RunMultiThread() { | |||||
| //int64_t start = GetCurrentMicroTime(); | //int64_t start = GetCurrentMicroTime(); | ||||
| for (int i = 0; i < GenIdCount / threadCount; i++) { | for (int i = 0; i < GenIdCount / threadCount; i++) { | ||||
| int64_t id = NextId(); | int64_t id = NextId(); | ||||
| printf("生成ID: %ld\n", id); | |||||
| printf("生成ID: %D\n", id); | |||||
| } | } | ||||
| int64_t end = GetCurrentMicroTime(); | int64_t end = GetCurrentMicroTime(); | ||||