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
428f1313
Commit
428f1313
authored
Nov 23, 2014
by
Kenton Varda
Committed by
Kenton Varda
Nov 23, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Timeouts: Add convenience helpers to kj::Timer interface for adding a timeout to any Promise.
parent
8175968f
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
80 additions
and
2 deletions
+80
-2
Makefile.am
c++/Makefile.am
+2
-1
CMakeLists.txt
c++/src/kj/CMakeLists.txt
+2
-1
async-io-test.c++
c++/src/kj/async-io-test.c++
+12
-0
time.c++
c++/src/kj/time.c++
+34
-0
time.h
c++/src/kj/time.h
+30
-0
No files found.
c++/Makefile.am
View file @
428f1313
...
@@ -221,7 +221,8 @@ libkj_async_la_LDFLAGS = -release $(VERSION) -no-undefined
...
@@ -221,7 +221,8 @@ libkj_async_la_LDFLAGS = -release $(VERSION) -no-undefined
libkj_async_la_SOURCES
=
\
libkj_async_la_SOURCES
=
\
src/kj/async.c++
\
src/kj/async.c++
\
src/kj/async-unix.c++
\
src/kj/async-unix.c++
\
src/kj/async-io.c++
src/kj/async-io.c++
\
src/kj/time.c++
endif
!LITE_MODE
endif
!LITE_MODE
if
!LITE_MODE
if
!LITE_MODE
...
...
c++/src/kj/CMakeLists.txt
View file @
428f1313
...
@@ -38,7 +38,6 @@ set(kj_headers
...
@@ -38,7 +38,6 @@ set(kj_headers
mutex.h
mutex.h
thread.h
thread.h
threadlocal.h
threadlocal.h
time.h
main.h
main.h
windows-sanity.h
windows-sanity.h
)
)
...
@@ -59,6 +58,7 @@ set(kj-async_sources
...
@@ -59,6 +58,7 @@ set(kj-async_sources
async.c++
async.c++
async-unix.c++
async-unix.c++
async-io.c++
async-io.c++
time.c++
)
)
set
(
kj-async_headers
set
(
kj-async_headers
async-prelude.h
async-prelude.h
...
@@ -66,6 +66,7 @@ set(kj-async_headers
...
@@ -66,6 +66,7 @@ set(kj-async_headers
async-inl.h
async-inl.h
async-unix.h
async-unix.h
async-io.h
async-io.h
time.h
)
)
if
(
NOT CAPNP_LITE
)
if
(
NOT CAPNP_LITE
)
add_library
(
kj-async
${
kj-async_sources
}
)
add_library
(
kj-async
${
kj-async_sources
}
)
...
...
c++/src/kj/async-io-test.c++
View file @
428f1313
...
@@ -204,5 +204,17 @@ TEST(AsyncIo, PipeThreadDisconnects) {
...
@@ -204,5 +204,17 @@ TEST(AsyncIo, PipeThreadDisconnects) {
EXPECT_EQ
(
0
,
pipeThread
.
pipe
->
tryRead
(
buf
,
1
,
1
).
wait
(
ioContext
.
waitScope
));
EXPECT_EQ
(
0
,
pipeThread
.
pipe
->
tryRead
(
buf
,
1
,
1
).
wait
(
ioContext
.
waitScope
));
}
}
TEST
(
AsyncIo
,
Timeouts
)
{
auto
ioContext
=
setupAsyncIo
();
Timer
&
timer
=
ioContext
.
provider
->
getTimer
();
auto
promise1
=
timer
.
timeoutAfter
(
1
*
MILLISECONDS
,
kj
::
Promise
<
int
>
(
kj
::
NEVER_DONE
));
auto
promise2
=
timer
.
timeoutAfter
(
1
*
MILLISECONDS
,
kj
::
Promise
<
int
>
(
123
));
EXPECT_TRUE
(
kj
::
runCatchingExceptions
([
&
]()
{
promise1
.
wait
(
ioContext
.
waitScope
);
})
!=
nullptr
);
EXPECT_EQ
(
123
,
promise2
.
wait
(
ioContext
.
waitScope
));
}
}
// namespace
}
// namespace
}
// namespace kj
}
// namespace kj
c++/src/kj/time.c++
0 → 100644
View file @
428f1313
// Copyright (c) 2014 Google Inc. (contributed by Remy Blank <rblank@google.com>)
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "time.h"
#include "debug.h"
namespace
kj
{
kj
::
Exception
Timer
::
makeTimeoutException
()
{
return
kj
::
Exception
(
kj
::
Exception
::
Nature
::
LOCAL_BUG
,
kj
::
Exception
::
Durability
::
OVERLOADED
,
__FILE__
,
__LINE__
,
kj
::
heapString
(
"operation timed out"
));
}
}
// namespace kj
c++/src/kj/time.h
View file @
428f1313
...
@@ -80,8 +80,38 @@ public:
...
@@ -80,8 +80,38 @@ public:
virtual
Promise
<
void
>
afterDelay
(
Duration
delay
)
=
0
;
virtual
Promise
<
void
>
afterDelay
(
Duration
delay
)
=
0
;
// Equivalent to atTime(now() + delay).
// Equivalent to atTime(now() + delay).
template
<
typename
T
>
Promise
<
T
>
timeoutAt
(
TimePoint
time
,
Promise
<
T
>&&
promise
);
// Return a promise equivalent to `promise` but which throws an exception (and cancels the
// original promise) if it hasn't completed by `time`.
template
<
typename
T
>
Promise
<
T
>
timeoutAfter
(
Duration
delay
,
Promise
<
T
>&&
promise
);
// Return a promise equivalent to `promise` but which throws an exception (and cancels the
// original promise) if it hasn't completed after `delay` from now.
private
:
static
kj
::
Exception
makeTimeoutException
();
};
};
// =======================================================================================
// inline implementation details
template
<
typename
T
>
Promise
<
T
>
Timer
::
timeoutAt
(
TimePoint
time
,
Promise
<
T
>&&
promise
)
{
return
promise
.
exclusiveJoin
(
atTime
(
time
).
then
([]()
->
kj
::
Promise
<
T
>
{
return
makeTimeoutException
();
}));
}
template
<
typename
T
>
Promise
<
T
>
Timer
::
timeoutAfter
(
Duration
delay
,
Promise
<
T
>&&
promise
)
{
return
promise
.
exclusiveJoin
(
afterDelay
(
delay
).
then
([]()
->
kj
::
Promise
<
T
>
{
return
makeTimeoutException
();
}));
}
}
// namespace kj
}
// namespace kj
#endif // KJ_TIME_H_
#endif // KJ_TIME_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