Browse Source

always lock

pull/2/head
Albert 4 years ago
parent
commit
d3005f372d
8 changed files with 19 additions and 37 deletions
  1. +0
    -1
      PHP/php_snowdrift.h
  2. +0
    -2
      PHP/snowdrift.c
  3. +2
    -2
      PHP/snowdrift.php
  4. +10
    -24
      PHP/src/snowflake/snowflake.c
  5. +0
    -1
      PHP/src/snowflake/snowflake.h
  6. +3
    -3
      PHP/src/snowflake/spinlock.c
  7. +3
    -2
      PHP/src/snowflake/spinlock.h
  8. +1
    -2
      PHP/src/test.c

+ 0
- 1
PHP/php_snowdrift.h View File

@@ -58,7 +58,6 @@ uint8_t WorkerIdNum;
uint8_t WorkerIdBitLength;
uint8_t SeqBitLength;
uint16_t TopOverCostCount;
uint8_t Lock;

ZEND_END_MODULE_GLOBALS(snowdrift)



+ 0
- 2
PHP/snowdrift.c View File

@@ -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;


+ 2
- 2
PHP/snowdrift.php View File

@@ -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();



+ 10
- 24
PHP/src/snowflake/snowflake.c View File

@@ -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;
}

+ 0
- 1
PHP/src/snowflake/snowflake.h View File

@@ -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;


+ 3
- 3
PHP/src/snowflake/spinlock.c View File

@@ -1,11 +1,11 @@
#include <stdlib.h>
#include "spinlock.h"
#include <sched.h>
#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);
}

+ 3
- 2
PHP/src/snowflake/spinlock.h View File

@@ -1,10 +1,11 @@
#ifndef __SPINLOCK_H
#define __SPINLOCK_H
#include <stdint.h>

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

+ 1
- 2
PHP/src/test.c View File

@@ -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);
}



Loading…
Cancel
Save