Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
67b9ea88
Commit
67b9ea88
authored
Jul 27, 2017
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor kj::Thread to share more code cross-platform.
parent
cc74158d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
24 deletions
+24
-24
thread.c++
c++/src/kj/thread.c++
+22
-24
thread.h
c++/src/kj/thread.h
+2
-0
No files found.
c++/src/kj/thread.c++
View file @
67b9ea88
...
...
@@ -34,7 +34,7 @@ namespace kj {
#if _WIN32
Thread
::
Thread
(
Function
<
void
()
>
func
)
:
state
(
new
ThreadState
{
kj
::
mv
(
func
),
nullptr
,
2
}
)
{
Thread
::
Thread
(
Function
<
void
()
>
func
)
:
state
(
new
ThreadState
(
kj
::
mv
(
func
))
)
{
threadHandle
=
CreateThread
(
nullptr
,
0
,
&
runThread
,
state
,
0
,
nullptr
);
if
(
threadHandle
==
nullptr
)
{
state
->
unref
();
...
...
@@ -61,20 +61,9 @@ void Thread::detach() {
detached
=
true
;
}
DWORD
Thread
::
runThread
(
void
*
ptr
)
{
ThreadState
*
state
=
reinterpret_cast
<
ThreadState
*>
(
ptr
);
KJ_IF_MAYBE
(
exception
,
kj
::
runCatchingExceptions
([
&
]()
{
state
->
func
();
}))
{
state
->
exception
=
kj
::
mv
(
*
exception
);
}
state
->
unref
();
return
0
;
}
#else // _WIN32
Thread
::
Thread
(
Function
<
void
()
>
func
)
:
state
(
new
ThreadState
{
kj
::
mv
(
func
),
nullptr
,
2
}
)
{
Thread
::
Thread
(
Function
<
void
()
>
func
)
:
state
(
new
ThreadState
(
kj
::
mv
(
func
))
)
{
static_assert
(
sizeof
(
threadId
)
>=
sizeof
(
pthread_t
),
"pthread_t is larger than a long long on your platform. Please port."
);
...
...
@@ -119,19 +108,13 @@ void Thread::detach() {
state
->
unref
();
}
void
*
Thread
::
runThread
(
void
*
ptr
)
{
ThreadState
*
state
=
reinterpret_cast
<
ThreadState
*>
(
ptr
);
KJ_IF_MAYBE
(
exception
,
kj
::
runCatchingExceptions
([
&
]()
{
state
->
func
();
}))
{
state
->
exception
=
kj
::
mv
(
*
exception
);
}
state
->
unref
();
return
nullptr
;
}
#endif // _WIN32, else
Thread
::
ThreadState
::
ThreadState
(
Function
<
void
()
>
func
)
:
func
(
kj
::
mv
(
func
)),
exception
(
nullptr
),
refcount
(
2
)
{}
void
Thread
::
ThreadState
::
unref
()
{
#if _MSC_VER
if
(
_InterlockedDecrement
(
&
refcount
)
==
0
)
{
...
...
@@ -148,4 +131,19 @@ void Thread::ThreadState::unref() {
}
}
#if _WIN32
DWORD
Thread
::
runThread
(
void
*
ptr
)
{
#else
void
*
Thread
::
runThread
(
void
*
ptr
)
{
#endif
ThreadState
*
state
=
reinterpret_cast
<
ThreadState
*>
(
ptr
);
KJ_IF_MAYBE
(
exception
,
kj
::
runCatchingExceptions
([
&
]()
{
state
->
func
();
}))
{
state
->
exception
=
kj
::
mv
(
*
exception
);
}
state
->
unref
();
return
0
;
}
}
// namespace kj
c++/src/kj/thread.h
View file @
67b9ea88
...
...
@@ -53,6 +53,8 @@ public:
private
:
struct
ThreadState
{
ThreadState
(
Function
<
void
()
>
func
);
Function
<
void
()
>
func
;
kj
::
Maybe
<
kj
::
Exception
>
exception
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment