📄 share.pas
字号:
sqlCounter:=0;
timeCounter:=0;
end;
finally
leaveCriticalSection(sect);
end;
end;
procedure TDBWriteBuffer.sqlPush(sqlStr:string);
var
tmpStr:string;
begin
enterCriticalSection(sect);
try
tmpStr:=trim(sqlStr);
if tmpStr='' then exit;
if copy(tmpStr,length(tmpStr),1)<>';' then
tmpStr:=tmpStr+';';
sqlStrs.Add(tmpStr);
inc(sqlCounter);
if (sqlCounter>=size) and (trim(sqlStrs.Text)<>'') then
begin
DBWriter.commit(sqlPop);
end;
finally
leaveCriticalSection(sect);
end;
end;
procedure TDBWriteBuffer.timeGo;
begin
entercriticalSection(sect);
try
if sqlCounter<=0 then exit;
inc(timeCounter);
if (timeCounter>=waitTime) then
begin
DBWriter.commit(sqlPop);
end;
finally
leaveCriticalSection(sect);
end;
end;
function TDBWriteBuffer.toStrinig: string;
begin
result:=sqlStrs.Text;
end;
{ TJPObjectQueue }
constructor TJPObjectQueue.create(size: integer;safeSize:integer);
var
i:integer;
begin
self.FSize:=size;
setLength(queue,size);
for i:=0 to size-1 do
queue[i]:=nil;
self.FSafeSize:=safeSize;
FCount:=0;
rear:=0;
top:=0;
lock:=TLock.create;
end;
function TJPObjectQueue.isEmpty: boolean;
begin
result:=false;
if count<=0 then result:=true;
end;
function TJPObjectQueue.isFull: boolean;
begin
result:=false;
if count>=size then result:=true;
end;
function TJPObjectQueue.isSafeFull: boolean;
begin
result:=false;
if count>=size-safeSize then result:=true;
end;
function TJPObjectQueue.pPop: TObject;
begin
result:=nil;
if not lock.getState then
begin
lock.lock;
try
if isEmpty then exit;
result:=queue[top];
finally
lock.unlock;
end;
end;
end;
function TJPObjectQueue.pPush(obj: TObject): boolean;
begin
result:=false;
if not lock.getState then
begin
lock.lock;
try
if IsFull then exit;
queue[rear]:=obj;
inc(rear);
inc(FCount);
if rear>=size then Rear:=0;
Result:=true;
finally
lock.unlock;
end;
end;
end;
procedure TJPObjectQueue.remove;
begin
if not lock.getState then
begin
lock.lock;
try
if isEmpty then exit;
{删除对象}
if queue[top]<>nil then
begin
queue[top].Free;
queue[top]:=nil;
end;
inc(top);
dec(FCount);
if top>=Size then top:=0;
finally
lock.unlock;
end;
end;
end;
function TJPObjectQueue.pSafePush(obj: TObject): boolean;
begin
result:=false;
if not lock.getState then
begin
lock.lock;
try
if isSafeFull then exit;
queue[rear]:=obj;
inc(rear);
inc(FCount);
if rear>=size then Rear:=0;
Result:=true;
finally
lock.unlock;
end;
end;
end;
procedure TJPObjectQueue.pRemoveNoFree;
begin
if not lock.getState then
begin
lock.lock;
try
if isEmpty then exit;
if queue[top]<>nil then
begin
queue[top]:=nil;
end;
inc(top);
dec(FCount);
if top>=Size then top:=0;
finally
lock.unlock;
end;
end;
end;
{ TLock }
constructor TLock.create;
begin
aLock:=false;
end;
function TLock.getState: boolean;
begin
result:=aLock;
end;
procedure TLock.lock;
begin
aLock:=true;
end;
procedure TLock.unlock;
begin
aLock:=false;
end;
{ TJPCounter }
constructor TJPCounter.create(maxTime: integer);
begin
self.maxTime:=maxTime;
waitTime:=0;
end;
procedure TJPCounter.resetCounter;
begin
waitTime:=0;
end;
function TJPCounter.timeGo:boolean;
begin
result:=false;
inc(waitTime);
if waitTime>=maxTime then
begin
result:=true;
resetCounter;
onCounter;
end;
end;
{ TJPWaitObjectQueue }
constructor TJPWaitObjectQueue.create(size, safeSize,maxTime: integer);
var
i:integer;
begin
FSize:=size;
FSafeSize:=safeSize;
FMaxTime:=maxTime;
setLength(queue,size);
for i:=0 to size-1 do
queue[i]:=nil;
lock:=TLock.create;
end;
function TJPWaitObjectQueue.cusGetByID(id: cardinal;
var index: integer): TWaitUnit;
var
ind:cardinal;
begin
result:=nil;
if not lock.getState then
begin
lock.lock;
try
result:=getObjByID(id,ind);
index:=ind;
finally
lock.unlock;
end;
end;
end;
function TJPWaitObjectQueue.getFreePos: integer;
var
i:integer;
begin
result:=-1;
for i:=low(queue) to high(queue) do
if queue[i]=nil then
begin
result:=i;
break;
end;
end;
function TJPWaitObjectQueue.getObjByID(id:cardinal;var index:cardinal): TWaitUnit;
var
i:integer;
begin
result:=nil;
for i:=low(queue) to high(queue) do
begin
if queue[i].getID=id then
begin
result:=queue[i];
index:=i;
break;
end;
end;
end;
function TJPWaitObjectQueue.isEmpty: boolean;
begin
result:=false;
if count<=0 then result:=true;
end;
function TJPWaitObjectQueue.isFull: boolean;
begin
result:=false;
if count>=size then result:=true;
end;
function TJPWaitObjectQueue.isSafeFull: boolean;
begin
result:=false;
if count>=size-safeSize then result:=true;
end;
function TJPWaitObjectQueue.cusPush(obj: TWaitUnit): boolean;
var
pos:integer;
begin
result:=false;
if not lock.getState then
begin
lock.lock;
try
if isFull then exit;
pos:=getFreePos;
if pos<0 then exit;
queue[pos]:=obj;
queue[pos].resetTime;
inc(FCount);
result:=true;
finally
lock.unlock;
end;
end;
end;
procedure TJPWaitObjectQueue.removeByID(id: cardinal);
var
ind:cardinal;
obj:TObject;
begin
if not lock.getState then
begin
lock.lock;
try
obj:=getObjByID(id,ind);
if obj<>nil then
begin
queue[ind].freeData:=true;
queue[ind].Free;
queue[ind]:=nil
end;
dec(FCount);
finally
lock.unlock;
end;
end;
end;
procedure TJPWaitObjectQueue.removeByIndex(index: cardinal);
begin
if not lock.getState then
begin
lock.lock;
try
{删除对象}
if queue[index]<>nil then
begin
queue[index].freeData:=true;
queue[index].Free;
queue[index]:=nil;
end;
dec(FCount);
finally
lock.unlock;
end;
end;
end;
procedure TJPWaitObjectQueue.removeNoFreeByID(id: cardinal);
var
obj:TObject;
ind:cardinal;
begin
if not lock.getState then
begin
lock.lock;
try
obj:=getObjByID(id,ind);
if obj<>nil then
begin
queue[ind].freeData:=false;
queue[ind].Free;
queue[ind]:=nil
end;
dec(FCount);
finally
lock.unlock;
end;
end;
end;
function TJPWaitObjectQueue.cusSafePush(obj: TWaitUnit): boolean;
var
pos:integer;
begin
result:=false;
if not lock.getState then
begin
lock.lock;
try
if isSafeFull then exit;
pos:=getFreePos;
if pos<0 then exit;
queue[pos]:=obj;
queue[pos].resetTime;
inc(FCount);
result:=true;
finally
lock.unlock;
end;
end;
end;
procedure TJPWaitObjectQueue.timeGo;
var
i:integer;
tmpCount:integer;
begin
tmpCount:=0;
for i:=low(queue) to high(queue) do
begin
if tmpCount>count then break;
if queue[i]<>nil then
begin
queue[i].timeGo;
end;
inc(tmpCount);
end;
end;
function TJPWaitObjectQueue.getByIndex(ind: integer): TWaitUnit;
begin
result:=queue[ind];
end;
{ TWaitUnit }
constructor TWaitUnit.create(data: TObject);
begin
self.data:=data;
resetTime;
end;
destructor TWaitUnit.destroy;
begin
if isFreeData then
data.Free;
inherited;
end;
function TWaitUnit.getData: TObject;
begin
result:=data;
end;
function TWaitUnit.getTime: cardinal;
begin
result:=waitTime;
end;
procedure TWaitUnit.resetTime;
begin
waitTime:=0;
end;
procedure TWaitUnit.timeGo;
begin
inc(waitTime);
end;
{ TJPAbstactLink }
constructor TJPAbstractLink.create(max: integer);
begin
FMax:=max;
link:=nil;
rear:=nil;
cnt:=0;
initializeCriticalSection(sect);
end;
function TJPAbstractLink.getIterator:IIterator;
begin
result:=TJPIterator.create(self);
end;
function TJPAbstractLink.vNext(pNode:PLinkNode):PLinkNode;
begin
enterCriticalSection(sect);
try
result:=pNode^.next;
finally
leaveCriticalSection(sect);
end;
end;
function TJPAbstractLink.vPrior(pNode:PLinkNode):PLinkNode;
begin
enterCriticalSection(sect);
try
result:=pNode^.prior;
finally
leaveCriticalSection(sect);
end;
end;
function TJPAbstractLink.vPush(pData: pointer): boolean;
var
p:PLinkNode;
begin
enterCriticalSection(sect);
try
result:=false;
if cnt>=FMax then exit;
new(p);
p^.data:=pData;
p^.prior:=rear;
p^.next:=nil;
if rear<>nil then
begin
rear^.next:=p;
rear:=p;
end else begin
link:=p;
rear:=p;
end;
inc(cnt);
result:=true;
finally
leaveCriticalSection(sect);
end;
end;
function TJPAbstractLink.vRemove(pNode: PLinkNode): boolean;
begin
enterCriticalSection(sect);
result:=false;
try
if cnt<=0 then exit;
if (pNode<>nil) then
begin
if pNode=link then//删除的是第一个元素
begin
link:=pNode^.next;
if link<>nil then
link^.prior:=nil
else rear:=nil;
end else
if pNode=rear then
begin
rear:=pNode^.prior;
(pNode^.prior)^.next:=pNode^.next;
end else
begin
(pNode^.prior)^.next:=pNode^.next;
(pNode^.next)^.prior:=pNode^.prior;
end;
if pNode.data<>nil then
dispose(pNode^.data);
dispose(pNode);
dec(cnt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -