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
b4d403f5
Commit
b4d403f5
authored
Jul 20, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kj::Thread
parent
fc0c297c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
117 additions
and
54 deletions
+117
-54
Makefile.am
c++/Makefile.am
+4
-1
arena-test.c++
c++/src/kj/arena-test.c++
+1
-26
mutex-test.c++
c++/src/kj/mutex-test.c++
+1
-27
thread.c++
c++/src/kj/thread.c++
+45
-0
thread.h
c++/src/kj/thread.h
+66
-0
No files found.
c++/Makefile.am
View file @
b4d403f5
...
@@ -110,7 +110,8 @@ includekj_HEADERS = \
...
@@ -110,7 +110,8 @@ includekj_HEADERS = \
src/kj/arena.h
\
src/kj/arena.h
\
src/kj/io.h
\
src/kj/io.h
\
src/kj/tuple.h
\
src/kj/tuple.h
\
src/kj/mutex.h
src/kj/mutex.h
\
src/kj/thread.h
includekjparse_HEADERS
=
\
includekjparse_HEADERS
=
\
src/kj/parse/common.h
\
src/kj/parse/common.h
\
...
@@ -148,6 +149,7 @@ libcapnp_la_SOURCES= \
...
@@ -148,6 +149,7 @@ libcapnp_la_SOURCES= \
src/kj/arena.c++
\
src/kj/arena.c++
\
src/kj/io.c++
\
src/kj/io.c++
\
src/kj/mutex.c++
\
src/kj/mutex.c++
\
src/kj/thread.c++
\
src/kj/parse/char.c++
\
src/kj/parse/char.c++
\
src/capnp/blob.c++
\
src/capnp/blob.c++
\
src/capnp/arena.h
\
src/capnp/arena.h
\
...
@@ -198,6 +200,7 @@ capnp_test_SOURCES = \
...
@@ -198,6 +200,7 @@ capnp_test_SOURCES = \
src/kj/units-test.c++
\
src/kj/units-test.c++
\
src/kj/tuple-test.c++
\
src/kj/tuple-test.c++
\
src/kj/mutex-test.c++
\
src/kj/mutex-test.c++
\
src/kj/thread-test.c++
\
src/kj/parse/common-test.c++
\
src/kj/parse/common-test.c++
\
src/kj/parse/char-test.c++
\
src/kj/parse/char-test.c++
\
src/capnp/blob-test.c++
\
src/capnp/blob-test.c++
\
...
...
c++/src/kj/arena-test.c++
View file @
b4d403f5
...
@@ -23,6 +23,7 @@
...
@@ -23,6 +23,7 @@
#include "arena.h"
#include "arena.h"
#include "debug.h"
#include "debug.h"
#include "thread.h"
#include <gtest/gtest.h>
#include <gtest/gtest.h>
#include <stdint.h>
#include <stdint.h>
...
@@ -306,32 +307,6 @@ TEST(Arena, Strings) {
...
@@ -306,32 +307,6 @@ TEST(Arena, Strings) {
EXPECT_EQ
(
quux
.
end
()
+
1
,
corge
.
begin
());
EXPECT_EQ
(
quux
.
end
()
+
1
,
corge
.
begin
());
}
}
// I tried to use std::thread but it threw a pure-virtual exception. It's unclear if it's meant
// to be ready in GCC 4.7.
class
Thread
{
public
:
template
<
typename
Func
>
explicit
Thread
(
Func
&&
func
)
{
KJ_ASSERT
(
pthread_create
(
&
thread
,
nullptr
,
&
runThread
<
Decay
<
Func
>>
,
new
Decay
<
Func
>
(
kj
::
fwd
<
Func
>
(
func
)))
==
0
);
}
~
Thread
()
{
KJ_ASSERT
(
pthread_join
(
thread
,
nullptr
)
==
0
);
}
private
:
pthread_t
thread
;
template
<
typename
Func
>
static
void
*
runThread
(
void
*
ptr
)
{
Func
*
func
=
reinterpret_cast
<
Func
*>
(
ptr
);
KJ_DEFER
(
delete
func
);
(
*
func
)();
return
nullptr
;
}
};
struct
ThreadTestObject
{
struct
ThreadTestObject
{
ThreadTestObject
*
next
;
ThreadTestObject
*
next
;
void
*
owner
;
// points into the owning thread's stack
void
*
owner
;
// points into the owning thread's stack
...
...
c++/src/kj/mutex-test.c++
View file @
b4d403f5
...
@@ -23,6 +23,7 @@
...
@@ -23,6 +23,7 @@
#include "mutex.h"
#include "mutex.h"
#include "debug.h"
#include "debug.h"
#include "thread.h"
#include <pthread.h>
#include <pthread.h>
#include <unistd.h>
#include <unistd.h>
#include <gtest/gtest.h>
#include <gtest/gtest.h>
...
@@ -30,33 +31,6 @@
...
@@ -30,33 +31,6 @@
namespace
kj
{
namespace
kj
{
namespace
{
namespace
{
// I tried to use std::thread but it threw a pure-virtual exception. It's unclear if it's meant
// to be ready in GCC 4.7.
class
Thread
{
public
:
template
<
typename
Func
>
explicit
Thread
(
Func
&&
func
)
{
KJ_ASSERT
(
pthread_create
(
&
thread
,
nullptr
,
&
runThread
<
Decay
<
Func
>>
,
new
Decay
<
Func
>
(
kj
::
fwd
<
Func
>
(
func
)))
==
0
);
}
~
Thread
()
{
KJ_ASSERT
(
pthread_join
(
thread
,
nullptr
)
==
0
);
}
private
:
pthread_t
thread
;
template
<
typename
Func
>
static
void
*
runThread
(
void
*
ptr
)
{
Func
*
func
=
reinterpret_cast
<
Func
*>
(
ptr
);
KJ_DEFER
(
delete
func
);
(
*
func
)();
return
nullptr
;
}
};
inline
void
delay
()
{
usleep
(
10000
);
}
inline
void
delay
()
{
usleep
(
10000
);
}
TEST
(
Mutex
,
MutexGuarded
)
{
TEST
(
Mutex
,
MutexGuarded
)
{
...
...
c++/src/kj/thread.c++
0 → 100644
View file @
b4d403f5
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "thread.h"
#include "debug.h"
#include <pthread.h>
namespace
kj
{
Thread
::
Thread
(
void
*
(
*
run
)(
void
*
),
void
(
*
deleteArg
)(
void
*
),
void
*
arg
)
{
static_assert
(
sizeof
(
threadId
)
>=
sizeof
(
pthread_t
),
"pthread_t is larger than a long long on your platform. Please port."
);
int
pthreadResult
=
pthread_create
(
reinterpret_cast
<
pthread_t
*>
(
&
threadId
),
nullptr
,
run
,
arg
);
if
(
pthreadResult
!=
0
)
{
deleteArg
(
arg
);
KJ_FAIL_SYSCALL
(
"pthread_create"
,
pthreadResult
);
}
}
Thread
::~
Thread
()
{
KJ_ASSERT
(
pthread_join
(
threadId
,
nullptr
)
==
0
);
}
}
// namespace kj
c++/src/kj/thread.h
0 → 100644
View file @
b4d403f5
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef KJ_THREAD_H_
#define KJ_THREAD_H_
#include "common.h"
namespace
kj
{
class
Thread
{
// A thread! Pass a lambda to the constructor. The destructor joins the thread.
public
:
template
<
typename
Func
>
explicit
Thread
(
Func
&&
func
)
:
Thread
(
&
runThread
<
Decay
<
Func
>>
,
&
deleteArg
<
Decay
<
Func
>>
,
new
Decay
<
Func
>
(
kj
::
fwd
<
Func
>
(
func
)))
{}
~
Thread
();
private
:
unsigned
long
long
threadId
;
// actually pthread_t
Thread
(
void
*
(
*
run
)(
void
*
),
void
(
*
deleteArg
)(
void
*
),
void
*
arg
);
template
<
typename
Func
>
static
void
*
runThread
(
void
*
ptr
)
{
// TODO(someday): Catch exceptions and propagate to the joiner.
Func
*
func
=
reinterpret_cast
<
Func
*>
(
ptr
);
KJ_DEFER
(
delete
func
);
(
*
func
)();
return
nullptr
;
}
template
<
typename
Func
>
static
void
deleteArg
(
void
*
ptr
)
{
delete
reinterpret_cast
<
Func
*>
(
ptr
);
}
};
}
// namespace kj
#endif // KJ_THREAD_H_
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