From ffd831cd165c1c0cb6d069e98392aaa095f8259c Mon Sep 17 00:00:00 2001 From: yitter Date: Sun, 28 Mar 2021 14:13:40 +0800 Subject: [PATCH] auto commit --- C#.NET/source/Yitter.IdGenTest/Program.cs | 39 ++++++++++++++++++++----------- C/source/CMakeLists.txt | 2 +- C/source/YitIdHelper.c | 13 ++++++++--- C/source/YitIdHelper.h | 12 +++++++--- C/source/idgen/common.h | 13 +++++++++++ 5 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 C/source/idgen/common.h diff --git a/C#.NET/source/Yitter.IdGenTest/Program.cs b/C#.NET/source/Yitter.IdGenTest/Program.cs index 0946be0..ad9dc75 100644 --- a/C#.NET/source/Yitter.IdGenTest/Program.cs +++ b/C#.NET/source/Yitter.IdGenTest/Program.cs @@ -61,28 +61,41 @@ namespace Yitter.OrgSystem.TestA } } - [DllImport("yitidgenc.dll", CallingConvention = CallingConvention.StdCall)] + [DllImport("yitidgenc.dll", CallingConvention = CallingConvention.Cdecl)] public static extern long NextId(); - [DllImport("yitidgenc.dll", CallingConvention = CallingConvention.StdCall)] - public static extern void SetWorkerId(int workerId); + [DllImport("yitidgenc.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void SetWorkerId(uint workerId); + + [DllImport("yitidgenc.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int TestId(); private static void CallDll() { - int i = 0; - long id = 0; - DateTime start = DateTime.Now; + try + { + + int i = 0; + long id = 0; + DateTime start = DateTime.Now; + + var ids = TestId(); - SetWorkerId(1); + //SetWorkerId(1); - while (i < 50000) + while (i < 50000) + { + id = NextId(); + i++; + } + DateTime end = DateTime.Now; + Console.WriteLine("id:" + id); + Console.WriteLine($"+++++++++++C# call rust dll, gen 5W, total: {(end - start).TotalMilliseconds} ms"); + } + catch (Exception ex) { - id = NextId(); - i++; + Console.WriteLine(ex.Message); } - DateTime end = DateTime.Now; - Console.WriteLine("id:" + id); - Console.WriteLine($"+++++++++++C# call rust dll, gen 5W, total: {(end - start).TotalMilliseconds} ms"); } private static void RunSingle() diff --git a/C/source/CMakeLists.txt b/C/source/CMakeLists.txt index 8898c7b..1c324fb 100644 --- a/C/source/CMakeLists.txt +++ b/C/source/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_C_STANDARD 11) aux_source_directory(. DIR_SRCS) add_subdirectory(idgen) -##编译动态库 +#编译动态库 #set(LIB_SRC YitIdHelper.h YitIdHelper.c) #add_library(YitIdGenLib SHARED ${LIB_SRC}) #target_link_libraries(YitIdGenLib idgen) diff --git a/C/source/YitIdHelper.c b/C/source/YitIdHelper.c index d84a72f..3a84292 100644 --- a/C/source/YitIdHelper.c +++ b/C/source/YitIdHelper.c @@ -11,12 +11,19 @@ extern void SetIdGenerator(IdGeneratorOptions options) { SetOptions(options); } -extern void SetWorker(uint32_t workerId) { +extern void SetWorkerId(uint32_t workerId) { IdGeneratorOptions options = BuildIdGenOptions(workerId); - SetIdGenerator(options); + SetOptions(options); + //SetIdGenerator(options); } extern uint64_t NextId() { - return GetIdGenInstance()->NextId(); + IdGenerator *generator = GetIdGenInstance(); + uint64_t id = generator->NextId(); + free(generator); + return id; } +extern uint64_t TestId() { + return 123456; +} diff --git a/C/source/YitIdHelper.h b/C/source/YitIdHelper.h index 9ac2a05..1a53560 100644 --- a/C/source/YitIdHelper.h +++ b/C/source/YitIdHelper.h @@ -5,11 +5,17 @@ #pragma once #include "idgen/IdGenOptions.h" +#include "idgen/common.h" -__declspec(dllexport) void __stdcall SetIdGenerator(IdGeneratorOptions options); +TAP_DLLEXPORT +extern void SetIdGenerator(IdGeneratorOptions options); -__declspec(dllexport) void __stdcall SetWorker(uint32_t workerId); +TAP_DLLEXPORT +extern void SetWorkerId(uint32_t workerId); -__declspec(dllexport) uint64_t __stdcall NextId(); +TAP_DLLEXPORT +extern uint64_t NextId(); +TAP_DLLEXPORT +extern uint64_t TestId() ; diff --git a/C/source/idgen/common.h b/C/source/idgen/common.h new file mode 100644 index 0000000..b4f15cf --- /dev/null +++ b/C/source/idgen/common.h @@ -0,0 +1,13 @@ +// +// Created by zhouzj on 2021/3/28. +// +#pragma once + +#ifdef _WIN32 +#define TAP_CDECL __cdecl +#define TAP_DLLEXPORT __declspec(dllexport) +#else +#define TAP_CDECL +#define TAP_DLLEXPORT __declspec(dllexport) +#endif +