1
0
mirror of synced 2025-11-06 03:20:55 +08:00

5 Commits

Author SHA1 Message Date
yitter
0783aba37a !21 !修复多平台编译问题
Merge pull request !21 from 微希夷/master
2023-09-13 06:27:25 +00:00
Albert
10409330f3 修复多平台编译问题 2023-09-12 11:47:30 +08:00
Albert
04a13eee38 删除传统算法的单元测试 2023-03-21 13:58:30 +08:00
Albert
d97d3aa98f 修复静态编译时宏重复定义问题 2022-11-09 17:11:55 +08:00
Albert
3c15cf9df2 !optimize 时间追平漂移量时,损耗一点时间(微妙级别)降低CPU占用 2022-06-15 15:19:58 +08:00
4 changed files with 24 additions and 34 deletions

View File

@@ -10,7 +10,7 @@
extern int ncpu;
extern int spin;
void spin_lock(atomic_t* lock, uint32_t pid)
void spin_lock(atomic_t *lock, uint32_t pid)
{
int i, n;
@@ -22,8 +22,8 @@ void spin_lock(atomic_t* lock, uint32_t pid)
InterlockedCompareExchange(lock, pid, 0) == 0
#else
__sync_bool_compare_and_swap(lock, 0, pid)
#endif
)
#endif
)
{
return;
}
@@ -36,11 +36,7 @@ void spin_lock(atomic_t* lock, uint32_t pid)
for (i = 0; i < n; i++)
{
#ifdef WIN32
MemoryBarrier();
#else
__asm("pause");
#endif
atomic_cpu_pause();
}
if (*lock == 0 &&
@@ -48,8 +44,8 @@ void spin_lock(atomic_t* lock, uint32_t pid)
InterlockedCompareExchange(lock, pid, 0) == 0
#else
__sync_bool_compare_and_swap(lock, 0, pid)
#endif
)
#endif
)
{
return;
}
@@ -63,11 +59,11 @@ void spin_lock(atomic_t* lock, uint32_t pid)
}
}
void spin_unlock(atomic_t* lock, uint32_t pid)
void spin_unlock(atomic_t *lock, uint32_t pid)
{
#ifdef WIN32
InterlockedCompareExchange(lock, 0, pid);
#else
__sync_bool_compare_and_swap(lock, pid, 0);
#endif
#endif
}

View File

@@ -8,4 +8,14 @@ extern void spin_lock(atomic_t *lock, uint32_t pid);
extern void spin_unlock(atomic_t *lock, uint32_t pid);
#if defined(WIN32)
#define atomic_cpu_pause() MemoryBarrier();
#elif defined(__x86_64__)
#define atomic_cpu_pause() __asm__ __volatile__("pause")
#elif defined(__aarch64__)
#define atomic_cpu_pause() __asm__ __volatile__("yield")
#else
#define atomic_cpu_pause()
#endif
#endif

View File

@@ -1,16 +1,15 @@
--TEST--
Check for snowdrift serial
Check for snowdrift batch get unique
--SKIPIF--
<?php if (!extension_loaded("snowdrift")) print "skip"; ?>
--FILE--
<?php
$arr = [];
$max = 1024;
for ($i = 0; $i < $max; $i++) {
$arr[$i] = SnowDrift::NextId();
$max = 100000;
foreach (SnowDrift::NextNumId($max) as $id) {
$arr[$id] = '';
}
var_dump(($arr[$max-1] - $arr[0]) == ($max-1));
var_dump(count($arr));
?>
--EXPECT--
bool(true)
int(100000)

View File

@@ -1,15 +0,0 @@
--TEST--
Check for snowdrift batch get unique
--SKIPIF--
<?php if (!extension_loaded("snowdrift")) print "skip"; ?>
--FILE--
<?php
$arr = [];
$max = 100000;
foreach (SnowDrift::NextNumId($max) as $id) {
$arr[$id] = '';
}
var_dump(count($arr));
?>
--EXPECT--
int(100000)