This repository has been archived by the owner on May 4, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e981721
commit 8187985
Showing
6 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,113 @@ | |||
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. | |||
* | |||
* 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 "uv.h" | |||
#include "task.h" | |||
|
|||
static uv_cond_t signal_cond; | |||
static uv_mutex_t signal_mutex; | |||
static uv_mutex_t wait_mutex; | |||
static unsigned done_cb_called; | |||
static unsigned timer_cb_called; | |||
static uv_work_t reqs[16]; | |||
|
|||
|
|||
static void work_cb(uv_work_t* req) { | |||
uv_mutex_lock(&signal_mutex); | |||
uv_cond_signal(&signal_cond); | |||
uv_mutex_unlock(&signal_mutex); | |||
|
|||
uv_mutex_lock(&wait_mutex); | |||
uv_mutex_unlock(&wait_mutex); | |||
} | |||
|
|||
|
|||
static void done_cb(uv_work_t* req) { | |||
done_cb_called++; | |||
free(req); | |||
} | |||
|
|||
|
|||
static void fail_cb(uv_work_t* req) { | |||
ASSERT(0 && "fail_cb called"); | |||
} | |||
|
|||
|
|||
static void timer_cb(uv_timer_t* handle, int status) { | |||
unsigned i; | |||
|
|||
for (i = 0; i < ARRAY_SIZE(reqs); i++) | |||
ASSERT(0 == uv_work_cancel(reqs + i)); | |||
|
|||
uv_close((uv_handle_t*) handle, NULL); | |||
uv_mutex_unlock(&signal_mutex); | |||
uv_mutex_unlock(&wait_mutex); | |||
timer_cb_called++; | |||
} | |||
|
|||
|
|||
/* Submit as many work requests as it takes to saturate the thread pool, | |||
* then submit more and try to cancel them. | |||
*/ | |||
TEST_IMPL(threadpool_cancel_work) { | |||
uv_timer_t timer_handle; | |||
unsigned num_threads; | |||
unsigned i; | |||
uv_work_t* req; | |||
|
|||
ASSERT(0 == uv_cond_init(&signal_cond)); | |||
ASSERT(0 == uv_mutex_init(&signal_mutex)); | |||
ASSERT(0 == uv_mutex_init(&wait_mutex)); | |||
|
|||
uv_mutex_lock(&signal_mutex); | |||
uv_mutex_lock(&wait_mutex); | |||
|
|||
for (num_threads = 0; /* empty */; num_threads++) { | |||
req = malloc(sizeof(*req)); | |||
ASSERT(req != NULL); | |||
ASSERT(0 == uv_work_queue(uv_default_loop(), req, work_cb, done_cb)); | |||
|
|||
/* Expect to get signalled within 350 ms, otherwise assume that | |||
* the thread pool is saturated. As with any timing dependent test, | |||
* this is obviously not ideal. | |||
*/ | |||
if (uv_cond_timedwait(&signal_cond, &signal_mutex, 350 * 1e6)) { | |||
ASSERT(0 == uv_work_cancel(req)); | |||
free(req); | |||
break; | |||
} | |||
} | |||
|
|||
for (i = 0; i < ARRAY_SIZE(reqs); i++) | |||
ASSERT(0 == uv_work_queue(uv_default_loop(), reqs + i, fail_cb, NULL)); | |||
|
|||
ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle)); | |||
ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 10, 0)); | |||
ASSERT(0 == uv_run(uv_default_loop())); | |||
|
|||
ASSERT(done_cb_called == num_threads); | |||
|
|||
uv_cond_destroy(&signal_cond); | |||
uv_mutex_destroy(&signal_mutex); | |||
uv_mutex_destroy(&wait_mutex); | |||
|
|||
return 0; | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters