2017-08-18 23:52:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
|
|
|
|
* 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"
|
2016-12-30 07:41:03 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define ASSERT_TRUE(p) \
|
|
|
|
do { \
|
|
|
|
if (!(p)) { \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define ASSERT_FALSE(p) ASSERT_TRUE(!(p))
|
|
|
|
#define ASSERT_EQ(lhs, rhs) ASSERT_TRUE((lhs) == (rhs))
|
|
|
|
|
|
|
|
struct data {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
void fn(void *opaque) {
|
|
|
|
struct data *data = (struct data *)opaque;
|
|
|
|
pthread_mutex_lock(&data->mutex);
|
|
|
|
data->data[data->i] = data->i;
|
|
|
|
++data->i;
|
|
|
|
pthread_mutex_unlock(&data->mutex);
|
|
|
|
}
|
|
|
|
|
2017-01-01 03:39:32 +00:00
|
|
|
int testOrder(size_t numThreads, size_t queueSize) {
|
2016-12-30 07:41:03 +00:00
|
|
|
struct data data;
|
2017-01-01 03:39:32 +00:00
|
|
|
POOL_ctx *ctx = POOL_create(numThreads, queueSize);
|
2016-12-30 07:41:03 +00:00
|
|
|
ASSERT_TRUE(ctx);
|
|
|
|
data.i = 0;
|
2017-01-01 00:10:13 +00:00
|
|
|
pthread_mutex_init(&data.mutex, NULL);
|
2016-12-30 07:41:03 +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);
|
2016-12-30 07:41:03 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < data.i; ++i) {
|
|
|
|
ASSERT_EQ(i, data.data[i]);
|
|
|
|
}
|
|
|
|
}
|
2017-01-01 00:10:13 +00:00
|
|
|
pthread_mutex_destroy(&data.mutex);
|
2016-12-30 07:41:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-02 18:27:50 +00:00
|
|
|
void waitFn(void *opaque) {
|
|
|
|
(void)opaque;
|
2017-08-07 18:43:37 +00:00
|
|
|
UTIL_sleepMilli(1);
|
2017-08-02 18:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tests for deadlock */
|
|
|
|
int testWait(size_t numThreads, size_t queueSize) {
|
|
|
|
struct data data;
|
|
|
|
POOL_ctx *ctx = POOL_create(numThreads, queueSize);
|
|
|
|
ASSERT_TRUE(ctx);
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < 16; ++i) {
|
|
|
|
POOL_add(ctx, &waitFn, &data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
POOL_free(ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-30 07:41:03 +00:00
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
size_t numThreads;
|
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) {
|
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;
|
|
|
|
}
|
2017-08-02 18:27:50 +00:00
|
|
|
if (testWait(numThreads, queueSize)) {
|
|
|
|
printf("FAIL: testWait\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-12-30 07:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("PASS: testOrder\n");
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2017-07-31 17:10:16 +00:00
|
|
|
return (POOL_create(0, 1)) ? printf("FAIL: testInvalid\n"), 1
|
|
|
|
: printf("PASS: testInvalid\n"), 0;
|
2016-12-30 07:41:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|