diff --git a/PHP/php_snowdrift.h b/PHP/php_snowdrift.h index cfab159..1027451 100644 --- a/PHP/php_snowdrift.h +++ b/PHP/php_snowdrift.h @@ -58,7 +58,6 @@ uint8_t WorkerIdNum; uint8_t WorkerIdBitLength; uint8_t SeqBitLength; uint16_t TopOverCostCount; -uint8_t Lock; ZEND_END_MODULE_GLOBALS(snowdrift) diff --git a/PHP/snowdrift.c b/PHP/snowdrift.c index 891162d..82a8a93 100644 --- a/PHP/snowdrift.c +++ b/PHP/snowdrift.c @@ -47,7 +47,6 @@ STD_PHP_INI_ENTRY("snowdrift.WorkerIdNum", "1", PHP_INI_SYSTEM, OnUpdateLongGEZe STD_PHP_INI_ENTRY("snowdrift.WorkerIdBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdBitLength, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.SeqBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, SeqBitLength, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.TopOverCostCount", "2000", PHP_INI_SYSTEM, OnUpdateLongGEZero, TopOverCostCount, zend_snowdrift_globals, snowdrift_globals) -STD_PHP_INI_ENTRY("snowdrift.Lock", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, Lock, zend_snowdrift_globals, snowdrift_globals) PHP_INI_END() /* }}} */ @@ -75,7 +74,6 @@ static int snowdrift_init() tmp->WorkerIdBitLength = SD_G(WorkerIdBitLength); tmp->SeqBitLength = SD_G(SeqBitLength); tmp->TopOverCostCount = SD_G(TopOverCostCount); - tmp->Lock = SD_G(Lock); Config(tmp); } return SUCCESS; diff --git a/PHP/snowdrift.php b/PHP/snowdrift.php index a804814..31830c7 100644 --- a/PHP/snowdrift.php +++ b/PHP/snowdrift.php @@ -14,7 +14,7 @@ final class SnowFlake private CData $flake; private CData $pflake; - public function __construct(array $config = ['Method' => 1, 'BaseTime' => 0, 'WorkerId' => 1, 'WorkerIdBitLength' => 6, 'SeqBitLength' => 10, 'TopOverCostCount' => 2000, 'Lock' => 0]) + public function __construct(array $config = ['Method' => 1, 'BaseTime' => 0, 'WorkerId' => 1, 'WorkerIdBitLength' => 6, 'SeqBitLength' => 10, 'TopOverCostCount' => 2000]) { $this->ffi = FFI::cdef(file_get_contents($this->getHeaders()), $this->getLibrary()); $this->flake = $this->ffi->new("struct snowflake"); @@ -58,7 +58,7 @@ final class SnowFlake $total = 50000; -$snowflake = new SnowFlake(['Method' => 1, 'BaseTime' => 1577808000000, 'WorkerId' => 1, 'WorkerIdBitLength' => 1, 'SeqBitLength' => 10, 'TopOverCostCount' => 2000, 'Lock' => 0]); +$snowflake = new SnowFlake(['Method' => 1, 'BaseTime' => 1577808000000, 'WorkerId' => 1, 'WorkerIdBitLength' => 1, 'SeqBitLength' => 10, 'TopOverCostCount' => 2000]); $ffi = $snowflake->getFFI(); $pflake = $snowflake->getPflake(); diff --git a/PHP/src/snowflake/snowflake.c b/PHP/src/snowflake/snowflake.c index 91a90f4..5a298fb 100644 --- a/PHP/src/snowflake/snowflake.c +++ b/PHP/src/snowflake/snowflake.c @@ -17,8 +17,8 @@ static uint64_t CalcId(snowflake *flake); static uint64_t CalcTurnBackId(snowflake *flake); int ncpu; -int spin = 2048; -int pid = 0; +uint16_t spin = 2048; +uint32_t pid = 0; void Config(snowflake *flake) { @@ -188,34 +188,20 @@ uint64_t inline GetId(snowflake *flake) uint64_t NextId(snowflake *flake) { - if (flake->Lock != 0) - { - spin_lock(&flake->_Lock, pid); - uint64_t id = GetId(flake); - spin_unlock(&flake->_Lock, pid); - return id; - } - return GetId(flake); + spin_lock(&flake->_Lock, pid); + uint64_t id = GetId(flake); + spin_unlock(&flake->_Lock, pid); + return id; } uint64_t *NextNumId(snowflake *flake, uint32_t num) { uint64_t *arr = (uint64_t *)malloc(sizeof(uint64_t) * num); - if (flake->Lock != 0) + spin_lock(&flake->_Lock, pid); + for (uint32_t i = 0; i < num; i++) { - spin_lock(&flake->_Lock, pid); - for (uint32_t i = 0; i < num; i++) - { - arr[i] = GetId(flake); - } - spin_unlock(&flake->_Lock, pid); - } - else - { - for (uint32_t i = 0; i < num; i++) - { - arr[i] = GetId(flake); - } + arr[i] = GetId(flake); } + spin_unlock(&flake->_Lock, pid); return arr; } diff --git a/PHP/src/snowflake/snowflake.h b/PHP/src/snowflake/snowflake.h index f20de1b..7e2ac13 100644 --- a/PHP/src/snowflake/snowflake.h +++ b/PHP/src/snowflake/snowflake.h @@ -10,7 +10,6 @@ typedef struct snowflake uint32_t MaxSeqNumber; uint32_t MinSeqNumber; uint32_t TopOverCostCount; - uint8_t Lock; uint8_t _TimestampShift; uint32_t _CurrentSeqNumber; diff --git a/PHP/src/snowflake/spinlock.c b/PHP/src/snowflake/spinlock.c index 009f737..7a2de49 100644 --- a/PHP/src/snowflake/spinlock.c +++ b/PHP/src/snowflake/spinlock.c @@ -1,11 +1,11 @@ #include -#include "spinlock.h" #include +#include "spinlock.h" extern int ncpu; extern int spin; -void spin_lock(atomic_t *lock, int pid) +void spin_lock(atomic_t *lock, uint32_t pid) { int i, n; @@ -41,7 +41,7 @@ void spin_lock(atomic_t *lock, int pid) } } -void spin_unlock(atomic_t *lock, int pid) +void spin_unlock(atomic_t *lock, uint32_t pid) { __sync_bool_compare_and_swap(lock, pid, 0); } diff --git a/PHP/src/snowflake/spinlock.h b/PHP/src/snowflake/spinlock.h index 2d9a7cf..c1535f1 100644 --- a/PHP/src/snowflake/spinlock.h +++ b/PHP/src/snowflake/spinlock.h @@ -1,10 +1,11 @@ #ifndef __SPINLOCK_H #define __SPINLOCK_H +#include typedef volatile unsigned int atomic_t; -extern void spin_lock(atomic_t *lock, int which); +extern void spin_lock(atomic_t *lock, uint32_t pid); -extern void spin_unlock(atomic_t *lock, int which); +extern void spin_unlock(atomic_t *lock, uint32_t pid); #endif diff --git a/PHP/src/test.c b/PHP/src/test.c index a7fb16b..533bf94 100644 --- a/PHP/src/test.c +++ b/PHP/src/test.c @@ -49,7 +49,6 @@ void run() int main() { flake->Method = 1; - flake->Lock = 1; Config(flake); pthread_t tid[THREAD]; struct timeval t_start, t_end; @@ -97,7 +96,7 @@ int main() NextId(flake); } gettimeofday(&t_end, NULL); - printf("单线程 %s %s,总共:%d,%lf ms\n", flake->Method == 1 ? "漂移" : "传统", flake->Lock == 0 ? "无锁" : "有锁", TOTAL, (double)(t_end.tv_usec - t_start.tv_usec) / 1000.0); + printf("单线程 %s,总共:%d,%lf ms\n", flake->Method == 1 ? "漂移" : "传统", TOTAL, (double)(t_end.tv_usec - t_start.tv_usec) / 1000.0); sleep(1); }