From 60854e5a2e274e3875a34a908cd5e74a23042c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=B8=8C=E5=A4=B7?= <63851587@qq.com> Date: Wed, 7 Apr 2021 15:57:27 +0800 Subject: [PATCH] =?UTF-8?q?!4=20fix=20num=E5=8F=98=E9=87=8F=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP/config.w32 | 9 +++++---- PHP/snowdrift.c | 28 +++++++++++++--------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/PHP/config.w32 b/PHP/config.w32 index 8e8a6f7..8d6432e 100644 --- a/PHP/config.w32 +++ b/PHP/config.w32 @@ -8,10 +8,11 @@ ARG_ENABLE("snowdrift", "enable snowdrift support", "no"); if (PHP_SNOWDRIFT != "no") { - THIS_DIR=`dirname $0` - snowdrift_source_file="snowdrift.c\ - $THIS_DIR/src/snowflake/snowflake.c - " + snowdrift_source_file="snowdrift.c\ + src/snowflake/snowflake.c\ + src/snowflake/shm.c\ + src/snowflake/spinlock.c + " EXTENSION("snowdrift", $snowdrift_source_file, PHP_EXTNAME_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); } diff --git a/PHP/snowdrift.c b/PHP/snowdrift.c index 1262378..656b8a5 100644 --- a/PHP/snowdrift.c +++ b/PHP/snowdrift.c @@ -35,7 +35,7 @@ static struct shm shmctx; static snowflake *sf; zend_class_entry snowdrift_ce; -uint8_t num = 0; +static uint8_t wid_num = 0; /* {{{ PHP_INI */ @@ -43,7 +43,7 @@ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("snowdrift.Method", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, Method, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.BaseTime", "1582136402000", PHP_INI_SYSTEM, OnUpdateLongGEZero, BaseTime, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.WorkerId", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerId, zend_snowdrift_globals, snowdrift_globals) -STD_PHP_INI_ENTRY("snowdrift.WorkerIdNum", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdNum, zend_snowdrift_globals, snowdrift_globals) +STD_PHP_INI_ENTRY("snowdrift.WorkerIdNum", "63", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdNum, zend_snowdrift_globals, snowdrift_globals) 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.MaxSeqNumber", "0", PHP_INI_SYSTEM, OnUpdateLongGEZero, MaxSeqNumber, zend_snowdrift_globals, snowdrift_globals) @@ -55,25 +55,23 @@ PHP_INI_END() static int snowdrift_init() { - num = (-1L << SD_G(WorkerIdBitLength)) ^ -1L; - if (SD_G(WorkerIdNum) < num) + wid_num = (-1L << SD_G(WorkerIdBitLength)) ^ -1L; + if (SD_G(WorkerIdNum) < wid_num) { - num = SD_G(WorkerIdNum); + wid_num = SD_G(WorkerIdNum); } - shmctx.size = num * sizeof(snowflake); + shmctx.size = wid_num * sizeof(snowflake); if (shm_alloc(&shmctx) == -1) { - zend_throw_exception_ex(NULL, 0, "shared memory malloc failed"); return FAILURE; } - if (SD_G(MaxSeqNumber) <= SD_G(MinSeqNumber)) + if (SD_G(MaxSeqNumber) < SD_G(MinSeqNumber)) { - zend_throw_exception_ex(NULL, 0, "MaxSeqNumber must GE then MinSeqNumber"); return FAILURE; } - bzero(shmctx.addr, num * sizeof(snowflake)); + bzero(shmctx.addr, wid_num * sizeof(snowflake)); sf = (snowflake *)shmctx.addr; - for (int i = 0; i < num; i++) + for (int i = 0; i < wid_num; i++) { snowflake *tmp = (sf + i); tmp->Method = SD_G(Method); @@ -97,9 +95,9 @@ PHP_METHOD(snowdrift, NextId) RETURN_FALSE; } wid--; - if (wid < 0 || wid > num - 1) + if (wid < 0 || wid > wid_num) { - zend_throw_exception_ex(NULL, 0, "wid error! wid between 0 and %d", num - 1); + zend_throw_exception_ex(NULL, 0, "wid error! wid between 1 and %d", wid_num); RETURN_NULL(); } snowflake *flake = (sf + wid); @@ -117,9 +115,9 @@ PHP_METHOD(snowdrift, NextNumId) RETURN_FALSE; } wid--; - if (wid < 0 || wid > num - 1) + if (wid < 0 || wid > wid_num) { - zend_throw_exception_ex(NULL, 0, "wid error! wid between 0 and %d", num - 1); + zend_throw_exception_ex(NULL, 0, "wid error! wid between 1 and %d", wid_num); RETURN_NULL(); } snowflake *flake = (sf + wid);