⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udipoolclientcontext.pas

📁 楠楠写的DBiocp例子都是源码
💻 PAS
字号:
unit uDIPoolClientContext;

interface

uses
  Windows, SysUtils, uCriticalSection, uDIClientContext, uException, uWin32Const;
  {$I IOCP.inc}
  
type

  TDIPoolClientContext = Class
  private
    FPoolClientContextLock:           TCriticalSection;     //锁
    m_pFreeClientContextList:         TDIClientContext;     //指针
    m_nFreeClientContextCount:        Integer;              //空闲数量
    m_iMaxNumberOfFreeClientContext:  Integer;              //池内最大数量,超过释放
  public
    function AllocateFreeClientContextFromPool: TDIClientContext;
    procedure ReleaseClientContextToPool(FDIClientContext: TDIClientContext);
    procedure FreeClientContexts;
    function GetClientContextCount: Integer;
    procedure SetMaxFreeClientContext(m_MaxNumber: Integer);//设置池内最大允许数量
  published
    property FMaxNumberOfFreeClientContext: Integer read m_iMaxNumberOfFreeClientContext
                                                    write SetMaxFreeClientContext;
  public
    constructor Create;
    destructor Destroy; override;
  end;

implementation
  uses uDIMonitor;

constructor TDIPoolClientContext.Create;
begin
  inherited Create;
  m_nFreeClientContextCount := 0;
  m_iMaxNumberOfFreeClientContext := 0;
  m_pFreeClientContextList := nil;
  FPoolClientContextLock := TCriticalSection.Create;
end;

destructor TDIPoolClientContext.Destroy;
begin
  FreeClientContexts;
  FreeAndNil(FPoolClientContextLock);
  inherited Destroy;
end;

function TDIPoolClientContext.GetClientContextCount: Integer;
begin
  try
    FPoolClientContextLock.Lock;
    Result := m_nFreeClientContextCount;
  finally
    FPoolClientContextLock.UnLock;
  end;
end;

procedure TDIPoolClientContext.SetMaxFreeClientContext(m_MaxNumber: Integer);
begin
  try
    FPoolClientContextLock.Lock;
    if m_MaxNumber>=0 then m_iMaxNumberOfFreeClientContext := m_MaxNumber;
  finally
    FPoolClientContextLock.UnLock;
  end;
end;

function TDIPoolClientContext.AllocateFreeClientContextFromPool: TDIClientContext;
var
  m_pDIClientContext: TDIClientContext;
begin
  try
    FPoolClientContextLock.Lock;

    m_pDIClientContext := nil;
    if ( m_pFreeClientContextList = nil ) then begin
      m_pDIClientContext := TDIClientContext.Create;

      //性能分析器 增加上下文 New数量
      {$IFDEF _IOCP_MONITOR}
          _DIMonitor.AddNewContext;
      {$ENDIF}
    end
    else
    begin
      //取单链表最后一个Buffer
      m_pDIClientContext := m_pFreeClientContextList;
      m_pFreeClientContextList := m_pFreeClientContextList.m_pNext;
      Dec(m_nFreeClientContextCount);
    end;

  finally
    FPoolClientContextLock.UnLock;
  end;

  if m_pDIClientContext<> nil then begin
    m_pDIClientContext.FContextLock.Lock;
    m_pDIClientContext.InitClientContext;
    m_pDIClientContext.FContextLock.UnLock;
  end;

  Result := m_pDIClientContext;
end;
    
procedure TDIPoolClientContext.ReleaseClientContextToPool(FDIClientContext: TDIClientContext);
begin
  try
    FPoolClientContextLock.Lock;
 
    if ( m_nFreeClientContextCount < m_iMaxNumberOfFreeClientContext) then begin
      FDIClientContext.m_pNext := m_pFreeClientContextList;
      m_pFreeClientContextList := FDIClientContext;
      Inc(m_nFreeClientContextCount);
    end
    else
    begin
      FreeAndNil(FDIClientContext);
      FDIClientContext := nil;

      //性能分析器 增加上下文 Free数量
      {$IFDEF _IOCP_MONITOR}
          _DIMonitor.AddFreeContext;
      {$ENDIF}
    end;

  finally
    FPoolClientContextLock.UnLock;
  end;
end;

procedure TDIPoolClientContext.FreeClientContexts;
var
  m_pDIFreeClientContext: TDIClientContext;
  m_pDINextClientContext: TDIClientContext;
begin
  try
    FPoolClientContextLock.Lock;

    m_pDIFreeClientContext := m_pFreeClientContextList;
    m_pDINextClientContext := nil;
    while (m_pDIFreeClientContext<> nil) do begin
      m_pDINextClientContext := m_pDIFreeClientContext.m_pNext;
      FreeAndNil(m_pDIFreeClientContext);
      Dec(m_nFreeClientContextCount);
      m_pDIFreeClientContext := m_pDINextClientContext;
    end;

    m_pFreeClientContextList := nil;
    m_nFreeClientContextCount := 0;
  finally
    FPoolClientContextLock.UnLock;
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -