ultimatepp/uppsrc/CtrlCore/CtrlMt.cpp
Mirek Fidler 34ff691308 sizeof(wchar) is changed to 4 (32 bits) to support non BMP unicode characters
This might bring some incompatibilities in the code that expects wchar to be 16 bit, which
  escpecially involves dealing with Win32 (and to lesser extend MacOS) APIs, so if your application
  is doing that, please check all instances of WCHAR (UniChar on MacOS) or even wchar
  especially type casts.

  To support host APIs, char16 is introduced (but there is no 16-bit String varian).

  Use ToSystemCharsetW, FromSystemCharsetW to convert texts to Win32 API.

- Support of drawing non-BMP characters in GUI
- Vastly improved character font replacement code (when drawing characters missing with requested font, replacement font is used)
- Last instances of Win32 ANSI calls (those ending with A) are removed
- UTF handling routines are refactored and their's naming is unified
- RTF is now being able to handle non-BMP characters (RTF is used as clipboard format for RichText)

Other minor changes:

- fixed TryRealloc issue
- improved MemoryCheck
- Removed MemoryAlloc48/MemoryFree48
- In theide Background parsing should less often cause delays in the main thread
2021-12-02 12:03:19 +01:00

119 lines
2.1 KiB
C++

#include "CtrlCore.h"
#define LLOG(x) // DLOG(x)
namespace Upp {
static StaticMutex sGLock;
// #define DELAY_WATCH 1000 _DBG_
static thread_local int sGLockLevel = 0;
void EnterGuiMutex()
{
LLOG(">EnterGuiMutex " << sGLockLevel << ' ' << IsMainThread());
if(sGLockLevel++ == 0) {
#if DELAY_WATCH
for(int i = 0; i < DELAY_WATCH; i++) {
if(sGLock.TryEnter()) return;
Sleep(1);
}
Panic("Long timer procedure detected!");
#else
sGLock.Enter();
#endif
}
LLOG("EnterGuiMutex " << sGLockLevel << ' ' << IsMainThread());
}
void EnterGuiMutex(int n)
{
LLOG(">EnterGuiMutex: " << n << ' ' << sGLockLevel << ' ' << IsMainThread());
if(n > 0) {
if(sGLockLevel == 0)
sGLock.Enter();
sGLockLevel += n;
}
LLOG("EnterGuiMutex " << sGLockLevel << ' ' << IsMainThread());
}
bool TryEnterGuiMutex()
{
if(sGLockLevel == 0 && sGLock.TryEnter()) {
sGLockLevel++;
return true;
}
return false;
}
void LeaveGuiMutex()
{
LLOG(">LeaveGuiMutex " << sGLockLevel << ' ' << IsMainThread());
ASSERT(sGLockLevel > 0);
if(--sGLockLevel == 0)
sGLock.Leave();
LLOG("LeaveGuiMutex " << sGLockLevel << ' ' << IsMainThread());
}
int LeaveGuiMutexAll()
{
LLOG(">LeaveGuiMutexAll " << sGLockLevel << ' ' << IsMainThread());
int q = sGLockLevel;
if(q) {
sGLock.Leave();
sGLockLevel = 0;
}
LLOG("LeaveGuiMutexAll " << q << ' ' << IsMainThread());
return q;
}
bool ThreadHasGuiLock()
{
return Thread::IsMain() || sGLockLevel;
}
int GetGuiLockLevel()
{
return sGLockLevel;
}
void WakeUpGuiThread();
struct Ctrl::CallBox {
Semaphore sem;
Event<> cb;
};
void Ctrl::PerformCall(Ctrl::CallBox *cbox)
{
cbox->cb();
cbox->sem.Release();
}
void Ctrl::Call(Function<void ()> cb)
{
if(IsMainThread())
cb();
else {
CallBox cbox;
cbox.cb << cb;
UPP::PostCallback(callback1(PerformCall, &cbox));
WakeUpGuiThread();
int level = LeaveGuiMutexAll(); // Unlock GUI to give main thread chance to handle Call
cbox.sem.Wait();
EnterGuiMutex(level);
}
}
void Ctrl::ShutdownThreads()
{
Thread::BeginShutdownThreads();
while(Thread::GetCount()) {
ProcessEvents();
Sleep(0);
}
Thread::EndShutdownThreads();
}
}