Skip to content

Commit e488f60

Browse files
committed
Fix bug
1 parent 89a4d92 commit e488f60

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

moxt/libc.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
#define ANKERL_NANOBENCH_IMPLEMENT
88
#include <nanobench.h>
99

10+
#ifdef __linux
11+
#include <sys/types.h>
12+
#include <unistd.h>
13+
#elif _WIN32
14+
#include <windows.h>
15+
#elif __APPLE__
16+
#include <sys/types.h>
17+
#include <unistd.h>
18+
#endif
19+
1020
SEQ_FUNC int64_t seq_voidptr_to_int(void *p) {
1121
return reinterpret_cast<int64_t>(p);
1222
}
@@ -59,4 +69,15 @@ SEQ_FUNC void seq_register_signal_handler(int signum, SignalHandler handler) {
5969
perror("sigaction");
6070
exit(EXIT_FAILURE);
6171
}
72+
}
73+
74+
SEQ_FUNC int64_t seq_thread_id() {
75+
#ifdef __linux
76+
pid_t threadId = gettid();
77+
#elif _WIN32
78+
DWORD threadId = GetCurrentThreadId();
79+
#elif __APPLE__
80+
pid_t threadId = gettid();
81+
#endif
82+
return threadId;
6283
}

moxt/libc.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "common.hpp"
55
#include <snmalloc/snmalloc.h>
6+
#include <thread>
67

78
using namespace snmalloc;
89

@@ -54,5 +55,7 @@ SEQ_FUNC std::atomic<int64_t> *seq_atomic_int64_new(int64_t i);
5455
SEQ_FUNC void seq_atomic_int64_free(std::atomic<int64_t> *p);
5556
SEQ_FUNC int64_t seq_atomic_int64_load(std::atomic<int64_t> *p);
5657
SEQ_FUNC void seq_atomic_int64_store(std::atomic<int64_t> *p, int64_t i);
58+
// »ñÈ¡Ïß³Ìid
59+
SEQ_FUNC int64_t seq_thread_id();
5760

5861
#endif

moxt/libmisc.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
#include "moxt/utils/cast.hpp"
3030
#include <openssl/err.h>
3131
#include <openssl/opensslv.h>
32-
// #include <snmalloc/override/new.cc>
3332
#include "moxt/utils/snowflake.hpp"
34-
#include <atomic>
3533
#include <parallel_hashmap/phmap.h>
3634
#include <snmalloc/snmalloc.h>
3735

moxt/utils/snowflake.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ uint32_t ipToWorkerId(const std::string &ipAddress) {
4141
++it;
4242
}
4343
}
44-
return workerId;
44+
// Ensure workerId is within range [0, 1023]
45+
return workerId % 1024;
4546
}
4647

4748
bool Snowflake::initWorkerId() {
4849
const std::string &ip = getLocalIp();
49-
logi("local ip: {}", ip);
50+
// logi("local ip: {}", ip);
5051
workerId_ = ipToWorkerId(ip);
51-
logi("WorkerId init success: {}", workerId_);
52+
// logi("WorkerId init success: {}", workerId_);
5253
return true;
5354
}
5455

0 commit comments

Comments
 (0)