Skip to content

Commit

Permalink
Use Slim Reader/Writer lock to replace CRITICAL_SECTION (better perfo…
Browse files Browse the repository at this point in the history
…rmance).

Use One-Time Initialization for glyph caching to avoid duplicate glyph generation.
Use thread local storage in stream I/O of font manager to avoid transient GDI font selection.
  • Loading branch information
Crend King authored and Crend King committed Jan 10, 2012
1 parent 5567543 commit 2614a08
Show file tree
Hide file tree
Showing 31 changed files with 298 additions and 743 deletions.
2 changes: 1 addition & 1 deletion gdipp_client/api_override.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "gdipp_client/gdi_painter.h"
#include "gdipp_client/global.h"
#include "gdipp_client/helper.h"
#include "gdipp_lib/lock.h"
#include "gdipp_lib/scoped_rw_lock.h"
#include "gdipp_rpc/gdipp_rpc.h"

namespace gdipp
Expand Down
6 changes: 3 additions & 3 deletions gdipp_client/com_override.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "stdafx.h"
#include "com_override.h"
#include "gdipp_client/global.h"
#include "gdipp_lib/lock.h"
#include "gdipp_lib/scoped_rw_lock.h"

namespace gdipp
{
Expand Down Expand Up @@ -87,7 +87,7 @@ IFACEMETHODIMP CreateDxgiSurfaceRenderTarget_hook(

if (DrawGlyphRun_orig == NULL)
{
lock l(lock::CLIENT_COM_HOOK);
const scoped_rw_lock lock_w(scoped_rw_lock::CLIENT_COM_HOOK, false);
if (DrawGlyphRun_orig == NULL)
{
const void **vfptr = *reinterpret_cast<const void ***>(*renderTarget);
Expand Down Expand Up @@ -119,7 +119,7 @@ HRESULT WINAPI D2D1CreateFactory_hook(D2D1_FACTORY_TYPE factoryType, REFIID riid

if (CreateDxgiSurfaceRenderTarget_orig == NULL)
{
lock l(lock::CLIENT_COM_HOOK);
const scoped_rw_lock lock_w(scoped_rw_lock::CLIENT_COM_HOOK, false);
if (CreateDxgiSurfaceRenderTarget_orig == NULL)
{
ID2D1Factory *pIFactory = *reinterpret_cast<ID2D1Factory **>(ppIFactory);
Expand Down
4 changes: 2 additions & 2 deletions gdipp_client/gamma.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "stdafx.h"
#include "gamma.h"
#include "gdipp_lib/lock.h"
#include "gdipp_lib/scoped_rw_lock.h"

namespace gdipp
{
Expand All @@ -17,7 +17,7 @@ const BYTE *gamma::get_ramp(double gamma)
if (iter == _gamma_ramps.end())
{
// double-check lock
lock l(lock::CLIENT_GAMMA);
const scoped_rw_lock lock_w(scoped_rw_lock::CLIENT_GAMMA, false);
iter = _gamma_ramps.find(gamma);
if (iter == _gamma_ramps.end())
init_ramp(gamma);
Expand Down
5 changes: 2 additions & 3 deletions gdipp_client/gdipp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "gdipp_config/constant_client.h"
#include "gdipp_config/exclude_config.h"
#include "gdipp_lib/helper.h"
#include "gdipp_lib/lock.h"
#include "gdipp_lib/scoped_rw_lock.h"

namespace gdipp
{
Expand Down Expand Up @@ -69,7 +69,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
return FALSE;
gdipp::os_support_directwrite = (ver_info.dwMajorVersion >= 6);

gdipp::lock::initialize_locks();
gdipp::scoped_rw_lock::initialize();

if (!gdipp::init_rpc_client())
return FALSE;
Expand All @@ -83,7 +83,6 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
}
case DLL_PROCESS_DETACH:
gdipp::hook_instance.stop();
gdipp::lock::destory_locks();
break;
}

Expand Down
4 changes: 2 additions & 2 deletions gdipp_config/render_config_cache.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "stdafx.h"
#include "render_config_cache.h"
#include "gdipp_lib/helper.h"
#include "gdipp_lib/lock.h"
#include "gdipp_lib/scoped_rw_lock.h"

namespace gdipp
{
Expand Down Expand Up @@ -42,7 +42,7 @@ const render_config_static *render_config_cache::get_font_render_config(bool bol
std::map<uint32_t, const render_config_static *>::const_iterator config_iter = _cache.find(trait);
if (config_iter == _cache.end())
{
lock l(lock::CONFIG_RENDER_CACHE);
const scoped_rw_lock lock_w(scoped_rw_lock::CONFIG_RENDER_CACHE, false);
config_iter = _cache.find(trait);
if (config_iter == _cache.end())
{
Expand Down
4 changes: 2 additions & 2 deletions gdipp_config/render_config_delta_cache.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "stdafx.h"
#include "render_config_delta_cache.h"
#include "gdipp_lib/helper.h"
#include "gdipp_lib/lock.h"
#include "gdipp_lib/scoped_rw_lock.h"

namespace gdipp
{
Expand Down Expand Up @@ -41,7 +41,7 @@ render_config_delta render_config_delta_cache::get_font_render_config_delta(bool
std::map<uint32_t, const render_config_delta *>::const_iterator config_iter = _cache.find(trait);
if (config_iter == _cache.end())
{
lock l(lock::CONFIG_RENDER_CONFIG_DELTA_CACHE);
const scoped_rw_lock lock_w(scoped_rw_lock::CONFIG_RENDER_CONFIG_DELTA_CACHE, false);
config_iter = _cache.find(trait);
if (config_iter == _cache.end())
{
Expand Down
8 changes: 4 additions & 4 deletions gdipp_demo/gdipp_demo.rc
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,9,2,0
PRODUCTVERSION 0,9,2,0
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -112,12 +112,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "gdipp Project"
VALUE "FileDescription", "gdipp Demo"
VALUE "FileVersion", "0, 9, 2, 0"
VALUE "FileVersion", "1, 0, 0, 0"
VALUE "InternalName", "gdipp_demo"
VALUE "LegalCopyright", "Copyright (C) gdipp Project 2010"
VALUE "OriginalFilename", "gdipp_demo.exe"
VALUE "ProductName", "gdipp Demo"
VALUE "ProductVersion", "0, 9, 2, 0"
VALUE "ProductVersion", "1, 0, 0, 0"
END
END
BLOCK "VarFileInfo"
Expand Down
12 changes: 6 additions & 6 deletions gdipp_lib/gdipp_lib.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@
>
</File>
<File
RelativePath=".\lock.cpp"
RelativePath=".\minidump.cpp"
>
</File>
<File
RelativePath=".\minidump.cpp"
RelativePath=".\scoped_rw_lock.cpp"
>
</File>
<File
Expand Down Expand Up @@ -412,10 +412,6 @@
RelativePath=".\helper.h"
>
</File>
<File
RelativePath=".\lock.h"
>
</File>
<File
RelativePath=".\lru.h"
>
Expand All @@ -432,6 +428,10 @@
RelativePath=".\rpc.h"
>
</File>
<File
RelativePath=".\scoped_rw_lock.h"
>
</File>
<File
RelativePath=".\stdafx.h"
>
Expand Down
34 changes: 0 additions & 34 deletions gdipp_lib/lock.cpp

This file was deleted.

41 changes: 0 additions & 41 deletions gdipp_lib/lock.h

This file was deleted.

4 changes: 3 additions & 1 deletion gdipp_lib/lru.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "gdipp_lib/scoped_rw_lock.h"

namespace gdipp
{

Expand All @@ -24,7 +26,7 @@ class lru_list

bool access(const T data, T &erased)
{
lock l(lock::SERVER_LRU);
const scoped_rw_lock lock_w(scoped_rw_lock::LIB_LRU, false);

bool overflow = false;

Expand Down
35 changes: 35 additions & 0 deletions gdipp_lib/scoped_rw_lock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "stdafx.h"
#include "scoped_rw_lock.h"

namespace gdipp
{

std::vector<SRWLOCK> scoped_rw_lock::_srws;

void scoped_rw_lock::initialize()
{
_srws.resize(LAST_MONITOR_LOCATION);
for (int i = 0; i < LAST_MONITOR_LOCATION; ++i)
InitializeSRWLock(&_srws[i]);
}

scoped_rw_lock::scoped_rw_lock(MONITOR_LOCATION cs_location, bool is_shared)
{
_curr_srw = &_srws[cs_location];
_is_shared = is_shared;

if (is_shared)
AcquireSRWLockShared(_curr_srw);
else
AcquireSRWLockExclusive(_curr_srw);
}

scoped_rw_lock::~scoped_rw_lock()
{
if (_is_shared)
ReleaseSRWLockShared(_curr_srw);
else
ReleaseSRWLockExclusive(_curr_srw);
}

}
41 changes: 41 additions & 0 deletions gdipp_lib/scoped_rw_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "gdipp_lib/api.h"

namespace gdipp
{

class GDIPP_API scoped_rw_lock
{
public:
enum MONITOR_LOCATION
{
CLIENT_COM_HOOK,
CLIENT_GAMMA,
CONFIG_RENDER_CACHE,
CONFIG_RENDER_CONFIG_DELTA_CACHE,
GLOBAL_DEBUG,
LIB_LRU,
SERVER_DC_POOL,
SERVER_FONT_MGR,
SERVER_FREETYPE,
SERVER_GLYPH_CACHE,
SERVER_GLYPH_RUN_CACHE,

LAST_MONITOR_LOCATION
};

public:
static void initialize();

explicit scoped_rw_lock(MONITOR_LOCATION srw_location, bool is_shared);
~scoped_rw_lock();

private:
static std::vector<SRWLOCK> _srws;

SRWLOCK *_curr_srw;
bool _is_shared;
};

}
6 changes: 3 additions & 3 deletions gdipp_server/dc_pool.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "stdafx.h"
#include "dc_pool.h"
#include "gdipp_lib/lock.h"
#include "gdipp_lib/scoped_rw_lock.h"

namespace gdipp
{
Expand All @@ -25,7 +25,7 @@ HDC dc_pool::claim()
// if no resource exists, create one by calling create() of the template class
// otherwise, remove one from the free resource set and add to busy set

lock l(lock::SERVER_DC_POOL);
const scoped_rw_lock lock_w(scoped_rw_lock::SERVER_DC_POOL, false);

HDC hdc;

Expand All @@ -47,7 +47,7 @@ bool dc_pool::free(HDC hdc)
{
// return claimed resource back to the pool

lock l(lock::SERVER_DC_POOL);
const scoped_rw_lock lock_w(scoped_rw_lock::SERVER_DC_POOL, false);

std::set<HDC>::const_iterator busy_iter = _busy.find(hdc);
if (busy_iter == _busy.end())
Expand Down
Loading

0 comments on commit 2614a08

Please sign in to comment.