#include "Threading.h"
#ifdef VCZH_MSVC
#include <Windows.h>
namespace
{
using
namespace
threading_internal
;
using
namespace
collections
;
namespace
{
struct
{
;
(
)
:handle(
)
{
}
};
}
::
()
:waitableData(
0
)
{
}
void
::
(
threading_internal
::
*
)
{
=
;
}
bool
::
()
{
return
!=
0
;
}
bool
::
()
{
return WaitForTime(INFINITE);
}
bool
::
(
)
{
if
(
())
{
if(WaitForSingleObject(waitableData->handle, (DWORD)ms)==WAIT_OBJECT_0)
if
(
(
->
, (
)
)==((((
)
0x00000000L
) ) +
0
))
{
return
true
;
}
}
return
false
;
}
bool
::
(
**
,
)
{
<
>
(
);
for
(
=
0
;
i
<
;
i
++)
{
handles
i
]=
[
i
]->
->
;
}
DWORD result=WaitForMultipleObjects((DWORD)count, &handles[0], TRUE, INFINITE);
=
((
)
, &
handles
0
],
1
,
0xFFFFFFFF
);
return result==WAIT_OBJECT_0 || result==WAIT_ABANDONED_0;
return
result
==((((
)
0x00000000L
) ) +
0
) ||
result
==((((
)
0x00000080L
) ) +
0
);
}
bool
::
(
**
,
,
)
{
<
>
(
);
for
(
=
0
;
i
<
;
i
++)
{
handles
i
]=
[
i
]->
->
;
}
DWORD result=WaitForMultipleObjects((DWORD)count, &handles[0], TRUE, (DWORD)ms);
=
((
)
, &
handles
0
],
1
, (
)
);
return result==WAIT_OBJECT_0 || result==WAIT_ABANDONED_0;
return
result
==((((
)
0x00000000L
) ) +
0
) ||
result
==((((
)
0x00000080L
) ) +
0
);
}
::
(
**
,
,
bool
*
)
{
<
>
(
);
for
(
=
0
;
i
<
;
i
++)
{
handles
i
]=
[
i
]->
->
;
}
DWORD result=WaitForMultipleObjects((DWORD)count, &handles[0], FALSE, INFINITE);
=
((
)
, &
handles
0
],
0
,
0xFFFFFFFF
);
if(WAIT_OBJECT_0 <= result && result<WAIT_OBJECT_0+count)
if
(((((
)
0x00000000L
) ) +
0
) <=
result
&&
result
<((((
)
0x00000000L
) ) +
0
)+
)
{
*
=
false
;
return result-WAIT_OBJECT_0;
return
result
-((((
)
0x00000000L
) ) +
0
);
}
else if(WAIT_ABANDONED_0 <= result && result<WAIT_ABANDONED_0+count)
else
if
(((((
)
0x00000080L
) ) +
0
) <=
result
&&
result
<((((
)
0x00000080L
) ) +
0
)+
)
{
*
=
true
;
return result-WAIT_ABANDONED_0;
return
result
-((((
)
0x00000080L
) ) +
0
);
}
else
{
return
-
1
;
}
}
::
(
**
,
,
,
bool
*
)
{
<
>
(
);
for
(
=
0
;
i
<
;
i
++)
{
handles
i
]=
[
i
]->
->
;
}
DWORD result=WaitForMultipleObjects((DWORD)count, &handles[0], FALSE, (DWORD)ms);
=
((
)
, &
handles
0
],
0
, (
)
);
if(WAIT_OBJECT_0 <= result && result<WAIT_OBJECT_0+count)
if
(((((
)
0x00000000L
) ) +
0
) <=
result
&&
result
<((((
)
0x00000000L
) ) +
0
)+
)
{
*
=
false
;
return result-WAIT_OBJECT_0;
return
result
-((((
)
0x00000000L
) ) +
0
);
}
else if(WAIT_ABANDONED_0 <= result && result<WAIT_ABANDONED_0+count)
else
if
(((((
)
0x00000080L
) ) +
0
) <=
result
&&
result
<((((
)
0x00000080L
) ) +
0
)+
)
{
*
=
true
;
return result-WAIT_ABANDONED_0;
return
result
-((((
)
0x00000080L
) ) +
0
);
}
else
{
return
-
1
;
}
}
namespace
{
struct
:
public
{
;
()
{
=-
1
;
}
};
class
:
public
{
private
:
::
;
void
*
;
bool
;
protected
:
void
()
{
bool
=
this
->
;
::
();
try
{
(
this
,
);
=
::
;
::
();
}
catch
(...)
{
::
();
throw
;
}
if
(
deleteAfterStopped
)
{
delete
this
;
}
}
public
:
(
::
,
void
*
,
bool
)
:procedure(
)
,argument(
)
,deleteAfterStopped(
)
{
}
};
class
:
public
{
private
:
<
void
()>
;
bool
;
protected
:
void
()
{
bool
=
this
->
;
::
();
try
{
);
=
::
;
::
();
}
catch
(...)
{
::
();
throw
;
}
if
(
deleteAfterStopped
)
{
delete
this
;
}
}
public
:
(
const
<
void
()>&
,
bool
)
:procedure(
)
,deleteAfterStopped(
)
{
}
};
}
void
(
*
)
{
->
();
}
DWORD WINAPI InternalThreadProcWrapper(LPVOID lpParameter)
__stdcall
InternalThreadProcWrapper
(
)
{
((
*)
);
return
0
;
}
::
()
{
=
new
;
internalData->handle=CreateThread(NULL, 0, InternalThreadProcWrapper, this, CREATE_SUSPENDED, &internalData->id);
->
=
(
0
,
0
,
InternalThreadProcWrapper
,
this
,
0x00000004
, &
->
);
=
::
;
(
);
}
::
()
{
if
(
)
{
();
(
->
);
delete
;
}
}
*
::
(
,
void
*
,
bool
)
{
if
(
)
{
*
=
new
(
,
,
);
if
(
thread
->
())
{
return
thread
;
}
else
{
delete
thread
;
}
}
return
0
;
}
*
::
(
const
<
void
()>&
,
bool
)
{
*
=
new
(
,
);
if
(
thread
->
())
{
return
thread
;
}
else
{
delete
thread
;
}
return
0
;
}
void
::
(
)
{
::
((
)
);
}
::
()
{
;
(&
info
);
return
info
.
;
}
::
()
{
return
(
)::
();
}
bool
::
()
{
if(threadState==Thread::NotStarted && internalData->handle!=NULL)
{
if
(
(
->
)!=-
1
)
{
=
::
;
return
true
;
}
}
return
false
;
}
bool
::
()
{
if(internalData->handle!=NULL)
{
if
(
(
->
) != -
1
)
{
=
::
;
return
true
;
}
}
return
false
;
}
::
::
()
{
return
;
}
void
::
(
)
{
(
->
, ((
)
1
<<
));
}
namespace
{
struct
:
public
{
(
)
:
(
)
{
}
};
}
::
()
:internalData(
0
)
{
}
::
()
{
if
(
)
{
(
->
);
delete
;
}
}
bool
::
(
bool
,
const
&
)
{
if
(
())
return
false
;
BOOL aOwned=owned?TRUE:FALSE;
LPCTSTR aName=name==L""?NULL:name.Buffer();
HANDLE handle=CreateMutex(NULL, aOwned, aName);
if
(
handle
)
{
=
new
(
handle
);
(
);
}
return
();
}
bool
::
(
bool
,
const
&
)
{
if
(
())
return
false
;
BOOL aInteritable=inheritable?TRUE:FALSE;
HANDLE handle=OpenMutex(SYNCHRONIZE, aInteritable, name.Buffer());
=
((
0x00100000L
),
aInteritable
,
.
());
if
(
handle
)
{
=
new
(
handle
);
(
);
}
return
();
}
bool
::
()
{
if
(
())
{
return
(
->
)!=
0
;
}
return
false
;
}
namespace
{
struct
:
public
{
(
)
:
(
)
{
}
};
}
::
()
:internalData(
0
)
{
}
::
()
{
if
(
)
{
(
->
);
delete
;
}
}
bool
::
(
,
,
const
&
)
{
if
(
())
return
false
;
=(
)
;
=(
)
;
LPCTSTR aName=name==L""?NULL:name.Buffer();
HANDLE handle=CreateSemaphore(NULL, aInitial, aMax, aName);
=
(
0
,
aInitial
,
aMax
,
aName
);
if
(
handle
)
{
=
new
(
handle
);
(
);
}
return
();
}
bool
::
(
bool
,
const
&
)
{
if
(
())
return
false
;
BOOL aInteritable=inheritable?TRUE:FALSE;
HANDLE handle=OpenSemaphore(SYNCHRONIZE, aInteritable, name.Buffer());
=
((
0x00100000L
),
aInteritable
,
.
());
if
(
handle
)
{
=
new
(
handle
);
(
);
}
return
();
}
bool
::
()
{
if
(
())
{
return
(
1
)!=-
1
;
}
return
false
;
}
::
(
)
{
if
(
())
{
=-
1
;
if
(
(
->
, (
)
, &
previous
)!=
0
)
{
return
(
)
previous
;
}
}
return
-
1
;
}
namespace
{
struct
:
public
{
(
)
:
(
)
{
}
};
}
::
()
:internalData(
0
)
{
}
::
()
{
if
(
)
{
(
->
);
delete
;
}
}
bool
::
(
bool
,
const
&
)
{
if
(
())
return
false
;
BOOL aSignaled=signaled?TRUE:FALSE;
LPCTSTR aName=name==L""?NULL:name.Buffer();
HANDLE handle=CreateEvent(NULL, FALSE, aSignaled, aName);
=
(
0
,
0
,
aSignaled
,
aName
);
if
(
handle
)
{
=
new
(
handle
);
(
);
}
return
();
}
bool
::
(
bool
,
const
&
)
{
if
(
())
return
false
;
BOOL aSignaled=signaled?TRUE:FALSE;
LPCTSTR aName=name==L""?NULL:name.Buffer();
HANDLE handle=CreateEvent(NULL, TRUE, aSignaled, aName);
=
(
0
,
1
,
aSignaled
,
aName
);
if
(
handle
)
{
=
new
(
handle
);
(
);
}
return
();
}
bool
::
(
bool
,
const
&
)
{
if
(
())
return
false
;
BOOL aInteritable=inheritable?TRUE:FALSE;
HANDLE handle=OpenEvent(SYNCHRONIZE, aInteritable, name.Buffer());
=
((
0x00100000L
),
aInteritable
,
.
());
if
(
handle
)
{
=
new
(
handle
);
(
);
}
return
();
}
bool
::
()
{
if
(
())
{
return
(
->
)!=
0
;
}
return
false
;
}
bool
::
()
{
if
(
())
{
return
(
->
)!=
0
;
}
return
false
;
}
struct
ThreadPoolQueueProcArgument
{
void
(*
)(
void
*);
void
*
;
};
DWORD WINAPI ThreadPoolQueueProc(void* argument)
{
<
ThreadPoolQueueProcArgument
>
=(
ThreadPoolQueueProcArgument
*)
;
::
();
try
{
proc
(
proc
);
::
();
}
catch
(...)
{
::
();
}
return
0
;
}
DWORD WINAPI ThreadPoolQueueFunc(void* argument)
{
<
<
void
()>>
=(
<
void
()>*)
;
::
();
try
{
(*
proc
.
())
);
::
();
}
catch
(...)
{
::
();
}
return
0
;
}
::
()
{
}
::
()
{
}
bool
::
(
void
(*
)(
void
*),
void
*
)
{
ThreadPoolQueueProcArgument
*
=
new
ThreadPoolQueueProcArgument
;
p
->
=
;
p
->
=
;
if(QueueUserWorkItem(&ThreadPoolQueueProc, p, WT_EXECUTEDEFAULT))
{
return
true
;
}
else
{
delete
p
;
return
false
;
}
}
bool
::
(
const
<
void
()>&
)
{
<
void
()>*
=
new
<
void
()>(
);
if(QueueUserWorkItem(&ThreadPoolQueueFunc, p, WT_EXECUTEDEFAULT))
{
return
true
;
}
else
{
delete
p
;
return
false
;
}
}
namespace
{
struct
{
;
};
}
::
::
(
&
)
:criticalSection(&
)
{
->
();
}
::
::
()
{
->
();
}
::
()
{
=
new
;
InitializeCriticalSection
(&
->
);
}
::
()
{
(&
->
);
delete
;
}
bool
::
()
{
return
(&
->
)!=
0
;
}
void
::
()
{
(&
->
);
}
void
::
()
{
(&
->
);
}
namespace
{
struct
{
;
};
}
::
::
(
&
)
:lock(&
)
{
->
();
}
::
::
()
{
->
();
}
::
::
(
&
)
:lock(&
)
{
->
();
}
::
::
()
{
->
();
}
::
()
:internalData(
new
threading_internal
::
)
{
(&
->
);
}
::
()
{
delete
;
}
bool
::
()
{
return
(&
->
)!=
0
;
}
void
::
()
{
(&
->
);
}
void
::
()
{
(&
->
);
}
bool
::
()
{
return
TryAcquireSRWLockExclusive
(&
->
)!=
0
;
}
void
::
()
{
(&
->
);
}
void
::
()
{
(&
->
);
}
namespace
{
struct
{
;
};
}
::
()
:internalData(
new
threading_internal
::
)
{
InitializeConditionVariable
(&
->
);
}
::
()
{
delete
;
}
bool
::
(
&
)
{
return SleepConditionVariableCS(&internalData->variable, &cs.internalData->criticalSection, INFINITE)!=0;
return
(&
->
, &
.
->
,
0xFFFFFFFF
)!=
0
;
}
bool
::
(
&
,
)
{
return
(&
->
, &
.
->
, (
)
)!=
0
;
}
bool
::
(
&
)
{
return SleepConditionVariableSRW(&internalData->variable, &lock.internalData->lock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED)!=0;
return
SleepConditionVariableSRW
(&
->
, &
.
->
,
0xFFFFFFFF
,
0x1
)!=
0
;
}
bool
::
(
&
,
)
{
return SleepConditionVariableSRW(&internalData->variable, &lock.internalData->lock, (DWORD)ms, CONDITION_VARIABLE_LOCKMODE_SHARED)!=0;
return
SleepConditionVariableSRW
(&
->
, &
.
->
, (
)
,
0x1
)!=
0
;
}
bool
::
(
&
)
{
return SleepConditionVariableSRW(&internalData->variable, &lock.internalData->lock, INFINITE, 0)!=0;
return
SleepConditionVariableSRW
(&
->
, &
.
->
,
0xFFFFFFFF
,
0
)!=
0
;
}
bool
::
(
&
,
)
{
return
SleepConditionVariableSRW
(&
->
, &
.
->
, (
)
,
0
)!=
0
;
}
void
::
()
{
(&
->
);
}
void
::
()
{
(&
->
);
}
::
::
(
&
)
:spinLock(&
)
{
->
();
}
::
::
()
{
->
();
}
::
()
:token(
0
)
{
}
::
()
{
}
bool
::
()
{
return
(&
,
1
)==
0
;
}
void
::
()
{
while
(
_InterlockedCompareExchange
(&
,
1
,
0
)!=
0
)
{
while
(
!=
0
)
();
}
}
void
::
()
{
(&
,
0
);
}
#define KEY ((DWORD&)key)
::
(
)
:destructor(
)
{
static_assert
(
sizeof
(
) >=
sizeof
(
),
"ThreadLocalStorage's key storage is not large enouth."
);
(
this
);
CHECK_ERROR(KEY != TLS_OUT_OF_INDEXES, L"vl::ThreadLocalStorage::ThreadLocalStorage()#Failed to alloc new thread local storage index.");
do
{
if
(!(((
&)
) != ((
)
0xFFFFFFFF
)))
throw
(
L"vl::ThreadLocalStorage::ThreadLocalStorage()#Failed to alloc new thread local storage index."
);}
while
(
0
);
}
::
()
{
}
void
*
::
()
{
CHECK_ERROR(!disposed, L"vl::ThreadLocalStorage::Get()#Cannot access a disposed ThreadLocalStorage.");
do
{
if
(!(!
))
throw
(
L"vl::ThreadLocalStorage::Get()#Cannot access a disposed ThreadLocalStorage."
);}
while
(
0
);
}
void
::
(
void
*
)
{
CHECK_ERROR(!disposed, L"vl::ThreadLocalStorage::Set()#Cannot access a disposed ThreadLocalStorage.");
do
{
if
(!(!
))
throw
(
L"vl::ThreadLocalStorage::Set()#Cannot access a disposed ThreadLocalStorage."
);}
while
(
0
);
}
#undef KEY
}
#endif
namespace
{
void
::
()
{
CHECK_ERROR(!disposed, L"vl::ThreadLocalStorage::Clear()#Cannot access a disposed ThreadLocalStorage.");
do
{
if
(!(!
))
throw
(
L"vl::ThreadLocalStorage::Clear()#Cannot access a disposed ThreadLocalStorage."
);}
while
(
0
);
if
(
)
{
if
(
auto
=
())
{
(
data
);
}
}
(
nullptr
);
}
void
::
()
{
CHECK_ERROR(!disposed, L"vl::ThreadLocalStorage::Dispose()#Cannot access a disposed ThreadLocalStorage.");
do
{
if
(!(!
))
throw
(
L"vl::ThreadLocalStorage::Dispose()#Cannot access a disposed ThreadLocalStorage."
);}
while
(
0
);
();
=
true
;
}
struct
{
*
=
nullptr
;
*
=
nullptr
;
};
volatile
bool
=
false
;
*
=
nullptr
;
**
= &
tlsHead
;
void
::
(
*
)
{
CHECK_ERROR(!tlsFixed, L"vl::ThreadLocalStorage::PushStorage(ThreadLocalStorage*)#Cannot create new ThreadLocalStorage instance after calling ThreadLocalStorage::FixStorages().");
do
{
if
(!(!
tlsFixed
))
throw
(
L"vl::ThreadLocalStorage::PushStorage(ThreadLocalStorage*)#Cannot create new ThreadLocalStorage instance after calling ThreadLocalStorage::FixStorages()."
);}
while
(
0
);
auto
=
new
;
link
->
=
;
*
tlsTail
=
link
;
tlsTail
= &
link
->
;
}
void
::
()
{
tlsFixed
=
true
;
}
void
::
()
{
();
auto
=
tlsHead
;
while
(
current
)
{
current
->
->
();
current
=
current
->
;
}
}
void
::
()
{
();
auto
=
tlsHead
;
tlsHead
=
nullptr
;
tlsTail
=
nullptr
;
while
(
current
)
{
current
->
->
();
auto
=
current
;
current
=
current
->
;
delete
temp
;
}
}
}