Browse Source

auto commit

tags/v1.0.0
yitter 3 years ago
parent
commit
27b527993e
5 changed files with 24 additions and 18 deletions
  1. +3
    -4
      C/source/CMakeLists.txt
  2. +3
    -3
      C/source/YitIdHelper.c
  3. +2
    -2
      C/source/YitIdHelper.h
  4. +13
    -6
      C/source/idgen/SnowWorkerM1.c
  5. +3
    -3
      C/source/main.c

+ 3
- 4
C/source/CMakeLists.txt View File

@@ -18,11 +18,10 @@ add_subdirectory(idgen)
# 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)
#target_link_libraries(YitIdGen YitIdHelper)
target_link_libraries(YitIdGen YitIdHelper)
target_link_libraries(YitIdGen idgen)



C/source/idgen/YitIdHelper.c → C/source/YitIdHelper.c View File

@@ -5,15 +5,15 @@
#include <stdlib.h>
#include <stdint.h>
#include "YitIdHelper.h"
#include "IdGenerator.h"
#include "idgen/IdGenerator.h"
extern void SetIdGenerator(IdGeneratorOptions options) {
SetOptions(options);
}
extern void SetWorkerId(uint32_t workerId) {
// IdGeneratorOptions options = BuildIdGenOptions(workerId);
// SetOptions(options);
IdGeneratorOptions options = BuildIdGenOptions(workerId);
// SetOptions(options);
SetIdGenerator(options);
}

C/source/idgen/YitIdHelper.h → C/source/YitIdHelper.h View File

@@ -4,8 +4,8 @@
*/
#pragma once
#include "IdGenOptions.h"
#include "common.h"
#include "idgen/IdGenOptions.h"
#include "idgen/common.h"
TAP_DLLEXPORT

+ 13
- 6
C/source/idgen/SnowWorkerM1.c View File

@@ -113,6 +113,13 @@ static inline uint64_t CalcTurnBackId(SnowFlakeWorker *worker) {
extern SnowFlakeWorker *NewSnowFlakeWorker() {
SnowFlakeWorker *worker = (SnowFlakeWorker *) malloc(sizeof(SnowFlakeWorker));
worker->_IsOverCost = false;
worker->_LastTimeTick = 0;
worker->_TurnBackTimeTick = 0;
worker->_TurnBackIndex = 0;
worker->_OverCostCountInOneTerm = 0;
worker->_GenCountInOneTerm = 0;
worker->_TermIndex = 0;
return worker;
}
@@ -124,15 +131,15 @@ extern uint64_t WorkerM1NextId(SnowFlakeWorker *worker) {
}
extern uint64_t GetCurrentTimeTick(SnowFlakeWorker *worker) {
static struct timeval tv;
struct timeval tv;
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() {
static struct timeval tv;
struct timeval tv;
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;
// ftime(&t1);
@@ -140,9 +147,9 @@ extern uint64_t GetCurrentTime() {
}
extern uint64_t GetCurrentMicroTime() {
static struct timeval tv;
struct timeval tv;
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) {


+ 3
- 3
C/source/main.c View File

@@ -12,11 +12,11 @@
#include <stdbool.h>
#include "idgen/SnowWorkerM1.h"
#include "idgen/IdGenerator.h"
#include "idgen/YitIdHelper.h"
#include "YitIdHelper.h"
const int GenIdCount = 50000;
const bool multiThread = true;
const bool multiThread = false;
const int threadCount = 50;
const int method = 1;
@@ -24,7 +24,7 @@ void RunMultiThread() {
//int64_t start = GetCurrentMicroTime();
for (int i = 0; i < GenIdCount / threadCount; i++) {
int64_t id = NextId();
printf("生成ID: %ld\n", id);
printf("生成ID: %D\n", id);
}
int64_t end = GetCurrentMicroTime();


Loading…
Cancel
Save