zoukankan      html  css  js  c++  java
  • 数据模块池

    unit untDMPool;

    interface

    uses
    Classes, SyncObjs, SysUtils,
    DateUtils, untData;

    type

    PServerObject = ^TServerObject;

    TServerObject = record
    ServerObject: TdmData;
    InUse: Boolean;
    end;

    TDMPool = class
    private
    FCriticalSection: TCriticalSection;
    FServerObjects: TList;
    FPoolSize: integer;

    public
    constructor Create; overload;
    destructor Destroy; override;
    function Lock: TdmData;
    procedure Unlock(Value: TdmData);
    procedure Init;
    property PoolSize: integer read FPoolSize write FPoolSize;
    end;

    var
    G_DMPool: TDMPool;

    implementation
    Uses
    untCommonFun;

    constructor TDMPool.Create;
    begin
    FPoolSize := 20;
    FServerObjects := TList.Create;
    FCriticalSection := TCriticalSection.Create;
    end;

    destructor TDMPool.Destroy;
    begin
    while FServerObjects.Count > 0 do
    begin
    PServerObject(FServerObjects[0])^.ServerObject.Free;
    Dispose(PServerObject(FServerObjects[0]));
    FServerObjects.Delete(0);
    end;
    FreeAndNil(FServerObjects);
    FreeAndNil(FCriticalSection);
    inherited Destroy;
    end;

    procedure TDMPool.Init;
    var
    i: integer;
    p: PServerObject;
    begin
    if not Assigned(FServerObjects) then exit;
    try
    for i := 1 to FPoolSize do
    begin
    New(p);
    if Assigned(p) then
    begin
    p^.ServerObject := TdmData.Create(nil);
    p^.InUse := False;
    FServerObjects.Add(p);
    end;
    end;
    except
    On E:Exception do
    begin
    SysLog.WriteLog(e.Message);
    Exit;
    end;
    end;
    end;

    function TDMPool.Lock: TdmData;
    var
    i: integer;
    bFound: Boolean;
    begin
    Result := nil;
    try
    FCriticalSection.Enter;
    try
    bFound := False;
    for i := 0 to FServerObjects.Count - 1 do
    begin
    if not PServerObject(FServerObjects[i])^.InUse then
    begin
    PServerObject(FServerObjects[i])^.InUse := True;
    Result := PServerObject(FServerObjects[i])^.ServerObject;
    bFound := True;
    Break;
    end;
    end;
    if (FServerObjects.Count = PoolSize) and (not bFound) then
    begin
    Result := TdmData.Create(nil);
    Result.tag := 5;
    end;
    finally
    FCriticalSection.Leave;
    end;
    except
    on e:Exception do
    begin
    SysLog.WriteLog(e.Message);
    Exit;
    end;
    end;
    end;

    procedure TDMPool.Unlock(Value: TdmData);
    var
    i: integer;
    begin
    if not Assigned(Value) then
    exit;
    try
    FCriticalSection.Enter;
    try
    if Value.tag = 5 then
    begin
    FreeAndNil(Value);
    end
    else
    begin
    for i := 0 to FServerObjects.Count - 1 do
    begin
    if Value = PServerObject(FServerObjects[i])^.ServerObject then
    begin
    PServerObject(FServerObjects[i])^.InUse := False;
    Break;
    end;
    end;
    end;
    finally
    FCriticalSection.Leave;
    end;
    except
    On E:Exception do
    begin
    SysLog.WriteLog(e.Message);
    Exit;
    end;
    end;
    end;

    end.

  • 相关阅读:
    python 包管理工具 pip 的配置
    Python 变量作用域 LEGB (下)—— Enclosing function locals
    Python 变量作用域 LEGB (上)—— Local,Global,Builtin
    2020 Java 面试题 小结 (答案慢慢补上,有错误请指出)
    mysql 根据日期(date)做年,月,日分组统计查询
    jvm指令
    正则表达式 分割地址 获取省市区详细地址
    .Net 异常记录
    WCF设计服务协议(一)
    plsql ORA-01789:查询块具有不正确的结果列数
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2785043.html
Copyright © 2011-2022 走看看