2017-08-18 23:52:05 +00:00
|
|
|
/*
|
2020-03-26 22:19:05 +00:00
|
|
|
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
|
2017-08-18 23:52:05 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2017-08-18 23:52:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-12-30 07:41:03 +00:00
|
|
|
#include "pool.h"
|
2017-01-01 00:10:13 +00:00
|
|
|
#include "threading.h"
|
2017-08-07 18:43:37 +00:00
|
|
|
#include "util.h"
|
2019-04-11 16:50:39 +00:00
|
|
|
#include "timefn.h"
|
2016-12-30 07:41:03 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-06-21 00:18:57 +00:00
|
|
|
#define ASSERT_TRUE(p) \
|
|
|
|
do { \
|
|
|
|
if (!(p)) { \
|
|
|
|
return 1; \
|
|
|
|
} \
|
2016-12-30 07:41:03 +00:00
|
|
|
} while (0)
|
|
|
|
#define ASSERT_FALSE(p) ASSERT_TRUE(!(p))
|
|
|
|
#define ASSERT_EQ(lhs, rhs) ASSERT_TRUE((lhs) == (rhs))
|
|
|
|
|
|
|
|
struct data {
|
2019-04-11 19:03:42 +00:00
|
|
|
ZSTD_pthread_mutex_t mutex;
|
2017-01-01 03:39:32 +00:00
|
|
|
unsigned data[16];
|
2016-12-30 07:41:03 +00:00
|
|
|
size_t i;
|
|
|
|
};
|
|
|
|
|
2019-04-11 19:03:42 +00:00
|
|
|
static void fn(void *opaque)
|
|
|
|
{
|
2016-12-30 07:41:03 +00:00
|
|
|
struct data *data = (struct data *)opaque;
|
2018-06-21 00:18:57 +00:00
|
|
|
ZSTD_pthread_mutex_lock(&data->mutex);
|
2019-04-11 19:25:27 +00:00
|
|
|
data->data[data->i] = (unsigned)(data->i);
|
2016-12-30 07:41:03 +00:00
|
|
|
++data->i;
|
2018-06-21 00:18:57 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&data->mutex);
|
2016-12-30 07:41:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 19:03:42 +00:00
|
|
|
static int testOrder(size_t numThreads, size_t queueSize)
|
|
|
|
{
|
2016-12-30 07:41:03 +00:00
|
|
|
struct data data;
|
2019-04-11 19:03:42 +00:00
|
|
|
POOL_ctx* const ctx = POOL_create(numThreads, queueSize);
|
2016-12-30 07:41:03 +00:00
|
|
|
ASSERT_TRUE(ctx);
|
|
|
|
data.i = 0;
|
2019-10-18 19:33:45 +00:00
|
|
|
ASSERT_FALSE(ZSTD_pthread_mutex_init(&data.mutex, NULL));
|
2018-06-21 00:18:57 +00:00
|
|
|
{ size_t i;
|
2017-01-01 03:39:32 +00:00
|
|
|
for (i = 0; i < 16; ++i) {
|
2016-12-30 07:41:03 +00:00
|
|
|
POOL_add(ctx, &fn, &data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
POOL_free(ctx);
|
2017-01-01 03:39:32 +00:00
|
|
|
ASSERT_EQ(16, data.i);
|
2018-06-21 00:18:57 +00:00
|
|
|
{ size_t i;
|
2016-12-30 07:41:03 +00:00
|
|
|
for (i = 0; i < data.i; ++i) {
|
|
|
|
ASSERT_EQ(i, data.data[i]);
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 00:18:57 +00:00
|
|
|
ZSTD_pthread_mutex_destroy(&data.mutex);
|
2016-12-30 07:41:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-21 00:18:57 +00:00
|
|
|
|
|
|
|
/* --- test deadlocks --- */
|
|
|
|
|
2018-10-24 17:36:06 +00:00
|
|
|
static void waitFn(void *opaque) {
|
2017-08-02 18:27:50 +00:00
|
|
|
(void)opaque;
|
2017-08-07 18:43:37 +00:00
|
|
|
UTIL_sleepMilli(1);
|
2017-08-02 18:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tests for deadlock */
|
2018-10-24 17:36:06 +00:00
|
|
|
static int testWait(size_t numThreads, size_t queueSize) {
|
2017-08-02 18:27:50 +00:00
|
|
|
struct data data;
|
2019-04-11 19:03:42 +00:00
|
|
|
POOL_ctx* const ctx = POOL_create(numThreads, queueSize);
|
2017-08-02 18:27:50 +00:00
|
|
|
ASSERT_TRUE(ctx);
|
2018-06-21 00:18:57 +00:00
|
|
|
{ size_t i;
|
2017-08-02 18:27:50 +00:00
|
|
|
for (i = 0; i < 16; ++i) {
|
|
|
|
POOL_add(ctx, &waitFn, &data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
POOL_free(ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-21 00:18:57 +00:00
|
|
|
|
|
|
|
/* --- test POOL_resize() --- */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ZSTD_pthread_mutex_t mut;
|
2019-05-02 20:23:41 +00:00
|
|
|
int countdown;
|
2018-06-21 00:18:57 +00:00
|
|
|
int val;
|
|
|
|
int max;
|
|
|
|
ZSTD_pthread_cond_t cond;
|
2018-06-21 19:24:36 +00:00
|
|
|
} poolTest_t;
|
2018-06-21 00:18:57 +00:00
|
|
|
|
2018-10-24 17:36:06 +00:00
|
|
|
static void waitLongFn(void *opaque) {
|
2019-04-11 19:03:42 +00:00
|
|
|
poolTest_t* const test = (poolTest_t*) opaque;
|
2018-06-21 00:18:57 +00:00
|
|
|
ZSTD_pthread_mutex_lock(&test->mut);
|
2019-05-02 20:23:41 +00:00
|
|
|
test->val++;
|
|
|
|
if (test->val > test->max)
|
|
|
|
test->max = test->val;
|
|
|
|
ZSTD_pthread_mutex_unlock(&test->mut);
|
|
|
|
|
2019-05-02 21:35:25 +00:00
|
|
|
UTIL_sleepMilli(10);
|
2019-05-02 20:23:41 +00:00
|
|
|
|
|
|
|
ZSTD_pthread_mutex_lock(&test->mut);
|
|
|
|
test->val--;
|
|
|
|
test->countdown--;
|
|
|
|
if (test->countdown == 0)
|
|
|
|
ZSTD_pthread_cond_signal(&test->cond);
|
2018-06-21 00:18:57 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&test->mut);
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:24:36 +00:00
|
|
|
static int testThreadReduction_internal(POOL_ctx* ctx, poolTest_t test)
|
2018-06-21 00:18:57 +00:00
|
|
|
{
|
|
|
|
int const nbWaits = 16;
|
|
|
|
|
2019-05-02 20:23:41 +00:00
|
|
|
test.countdown = nbWaits;
|
2018-06-21 00:18:57 +00:00
|
|
|
test.val = 0;
|
2019-05-02 20:23:41 +00:00
|
|
|
test.max = 0;
|
2018-06-21 00:18:57 +00:00
|
|
|
|
|
|
|
{ int i;
|
|
|
|
for (i=0; i<nbWaits; i++)
|
|
|
|
POOL_add(ctx, &waitLongFn, &test);
|
|
|
|
}
|
|
|
|
ZSTD_pthread_mutex_lock(&test.mut);
|
2019-05-02 20:23:41 +00:00
|
|
|
while (test.countdown > 0)
|
|
|
|
ZSTD_pthread_cond_wait(&test.cond, &test.mut);
|
|
|
|
ASSERT_EQ(test.val, 0);
|
|
|
|
ASSERT_EQ(test.max, 4);
|
2018-06-21 00:18:57 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&test.mut);
|
|
|
|
|
2018-06-22 19:14:59 +00:00
|
|
|
ASSERT_EQ( POOL_resize(ctx, 2/*nbThreads*/) , 0 );
|
2019-05-02 20:23:41 +00:00
|
|
|
test.countdown = nbWaits;
|
2018-06-21 00:18:57 +00:00
|
|
|
test.val = 0;
|
2019-05-02 20:23:41 +00:00
|
|
|
test.max = 0;
|
2018-06-21 00:18:57 +00:00
|
|
|
{ int i;
|
|
|
|
for (i=0; i<nbWaits; i++)
|
|
|
|
POOL_add(ctx, &waitLongFn, &test);
|
|
|
|
}
|
|
|
|
ZSTD_pthread_mutex_lock(&test.mut);
|
2019-05-02 20:23:41 +00:00
|
|
|
while (test.countdown > 0)
|
|
|
|
ZSTD_pthread_cond_wait(&test.cond, &test.mut);
|
|
|
|
ASSERT_EQ(test.val, 0);
|
|
|
|
ASSERT_EQ(test.max, 2);
|
2018-06-21 00:18:57 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&test.mut);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int testThreadReduction(void) {
|
|
|
|
int result;
|
2018-06-21 19:24:36 +00:00
|
|
|
poolTest_t test;
|
2018-06-22 19:14:59 +00:00
|
|
|
POOL_ctx* const ctx = POOL_create(4 /*nbThreads*/, 2 /*queueSize*/);
|
2018-06-21 00:18:57 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(ctx);
|
|
|
|
|
|
|
|
memset(&test, 0, sizeof(test));
|
|
|
|
ASSERT_FALSE( ZSTD_pthread_mutex_init(&test.mut, NULL) );
|
|
|
|
ASSERT_FALSE( ZSTD_pthread_cond_init(&test.cond, NULL) );
|
|
|
|
|
|
|
|
result = testThreadReduction_internal(ctx, test);
|
|
|
|
|
|
|
|
ZSTD_pthread_mutex_destroy(&test.mut);
|
|
|
|
ZSTD_pthread_cond_destroy(&test.cond);
|
|
|
|
POOL_free(ctx);
|
2018-06-21 19:24:36 +00:00
|
|
|
|
2018-06-21 00:18:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-21 19:24:36 +00:00
|
|
|
/* --- test abrupt ending --- */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ZSTD_pthread_mutex_t mut;
|
|
|
|
int val;
|
|
|
|
} abruptEndCanary_t;
|
|
|
|
|
2018-10-24 17:36:06 +00:00
|
|
|
static void waitIncFn(void *opaque) {
|
2018-06-21 19:24:36 +00:00
|
|
|
abruptEndCanary_t* test = (abruptEndCanary_t*) opaque;
|
2018-06-21 21:58:59 +00:00
|
|
|
UTIL_sleepMilli(10);
|
2018-06-21 19:24:36 +00:00
|
|
|
ZSTD_pthread_mutex_lock(&test->mut);
|
|
|
|
test->val = test->val + 1;
|
|
|
|
ZSTD_pthread_mutex_unlock(&test->mut);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int testAbruptEnding_internal(abruptEndCanary_t test)
|
|
|
|
{
|
|
|
|
int const nbWaits = 16;
|
|
|
|
|
2018-06-22 19:14:59 +00:00
|
|
|
POOL_ctx* const ctx = POOL_create(3 /*numThreads*/, nbWaits /*queueSize*/);
|
2018-06-21 19:24:36 +00:00
|
|
|
ASSERT_TRUE(ctx);
|
|
|
|
test.val = 0;
|
|
|
|
|
|
|
|
{ int i;
|
|
|
|
for (i=0; i<nbWaits; i++)
|
2018-06-21 21:58:59 +00:00
|
|
|
POOL_add(ctx, &waitIncFn, &test); /* all jobs pushed into queue */
|
2018-06-21 19:24:36 +00:00
|
|
|
}
|
2018-06-22 19:14:59 +00:00
|
|
|
ASSERT_EQ( POOL_resize(ctx, 1 /*numThreads*/) , 0 ); /* downsize numThreads, to try to break end condition */
|
2018-06-21 19:24:36 +00:00
|
|
|
|
|
|
|
POOL_free(ctx); /* must finish all jobs in queue before giving back control */
|
|
|
|
ASSERT_EQ(test.val, nbWaits);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int testAbruptEnding(void) {
|
|
|
|
int result;
|
|
|
|
abruptEndCanary_t test;
|
|
|
|
|
|
|
|
memset(&test, 0, sizeof(test));
|
|
|
|
ASSERT_FALSE( ZSTD_pthread_mutex_init(&test.mut, NULL) );
|
|
|
|
|
|
|
|
result = testAbruptEnding_internal(test);
|
|
|
|
|
|
|
|
ZSTD_pthread_mutex_destroy(&test.mut);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-21 00:18:57 +00:00
|
|
|
/* --- test launcher --- */
|
|
|
|
|
2016-12-30 07:41:03 +00:00
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
size_t numThreads;
|
2018-06-21 00:18:57 +00:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
|
|
|
if (POOL_create(0, 1)) { /* should not be possible */
|
|
|
|
printf("FAIL: should not create POOL with 0 threads\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-01 03:39:32 +00:00
|
|
|
for (numThreads = 1; numThreads <= 4; ++numThreads) {
|
|
|
|
size_t queueSize;
|
2017-07-31 17:10:16 +00:00
|
|
|
for (queueSize = 0; queueSize <= 2; ++queueSize) {
|
2018-06-21 00:18:57 +00:00
|
|
|
printf("queueSize==%u, numThreads=%u \n",
|
|
|
|
(unsigned)queueSize, (unsigned)numThreads);
|
2017-01-01 03:39:32 +00:00
|
|
|
if (testOrder(numThreads, queueSize)) {
|
2016-12-30 07:41:03 +00:00
|
|
|
printf("FAIL: testOrder\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-06-21 00:18:57 +00:00
|
|
|
printf("SUCCESS: testOrder\n");
|
2017-08-02 18:27:50 +00:00
|
|
|
if (testWait(numThreads, queueSize)) {
|
|
|
|
printf("FAIL: testWait\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-06-21 00:18:57 +00:00
|
|
|
printf("SUCCESS: testWait\n");
|
2016-12-30 07:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-21 00:18:57 +00:00
|
|
|
|
2018-06-21 19:24:36 +00:00
|
|
|
if (testThreadReduction()) {
|
|
|
|
printf("FAIL: thread reduction not effective \n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
2019-05-02 20:23:41 +00:00
|
|
|
printf("SUCCESS: thread reduction effective \n");
|
2018-06-21 19:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (testAbruptEnding()) {
|
|
|
|
printf("FAIL: jobs in queue not completed on early end \n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
printf("SUCCESS: all jobs in queue completed on early end \n");
|
|
|
|
}
|
|
|
|
|
2018-06-21 00:18:57 +00:00
|
|
|
printf("PASS: all POOL tests\n");
|
|
|
|
|
2016-12-30 07:41:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|