зеркало из https://github.com/Azure/c-pal.git
changes from second review
This commit is contained in:
Родитель
2274786bf0
Коммит
1ddb37edc9
|
@ -11,15 +11,12 @@
|
|||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include "testrunnerswitcher.h"
|
||||
#include "interlocked.h"
|
||||
#include "threadapi.h"
|
||||
#include "sync.h"
|
||||
#include "timer.h"
|
||||
|
||||
#include "umock_c/umock_c.h"
|
||||
|
||||
TEST_DEFINE_ENUM_TYPE(THREADAPI_RESULT, THREADAPI_RESULT_VALUES)
|
||||
|
||||
static TEST_MUTEX_HANDLE g_testByTest;
|
||||
|
@ -110,7 +107,7 @@ TEST_FUNCTION(two_threads_increment_alternately)
|
|||
{
|
||||
///arrange
|
||||
volatile_atomic int32_t var;
|
||||
interlocked_exchange(&var, 0);
|
||||
(void)interlocked_exchange(&var, 0);
|
||||
THREAD_HANDLE thread1;
|
||||
THREAD_HANDLE thread2;
|
||||
|
||||
|
@ -134,12 +131,12 @@ TEST_FUNCTION(wake_up_all_threads)
|
|||
{
|
||||
///arrange
|
||||
volatile_atomic int32_t var;
|
||||
interlocked_exchange(&var, 0);
|
||||
interlocked_exchange(&create_count, 0);
|
||||
(void)interlocked_exchange(&var, 0);
|
||||
(void)interlocked_exchange(&create_count, 0);
|
||||
THREAD_HANDLE threads[100];
|
||||
|
||||
///act
|
||||
for(int i = 0; i < 100; ++i)
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
ASSERT_ARE_EQUAL(THREADAPI_RESULT, THREADAPI_OK, ThreadAPI_Create(&threads[i], increment_on_wake_up, (void*)&var));
|
||||
}
|
||||
|
@ -151,7 +148,7 @@ TEST_FUNCTION(wake_up_all_threads)
|
|||
current_create_count = interlocked_add(&create_count, 0);
|
||||
}
|
||||
wake_by_address_all(&var);
|
||||
for(int i = 0; i < 100; ++i)
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
ASSERT_ARE_EQUAL(THREADAPI_RESULT, THREADAPI_OK, ThreadAPI_Join(threads[i], NULL), "ThreadAPI_Join did not work");
|
||||
}
|
||||
|
@ -167,7 +164,7 @@ TEST_FUNCTION(wait_on_address_returns_immediately)
|
|||
{
|
||||
///arrange
|
||||
volatile_atomic int32_t var;
|
||||
interlocked_exchange(&var, 0);
|
||||
(void)interlocked_exchange(&var, 0);
|
||||
int value = 1;
|
||||
|
||||
///act
|
||||
|
@ -185,7 +182,7 @@ TEST_FUNCTION(wait_on_address_returns_after_timeout_elapses)
|
|||
{
|
||||
///arrange
|
||||
volatile_atomic int32_t var;
|
||||
interlocked_exchange(&var, 0);
|
||||
(void)interlocked_exchange(&var, 0);
|
||||
int value = 0;
|
||||
int timeout = 1000;
|
||||
double tolerance_factor = 1.5;
|
||||
|
|
|
@ -10,12 +10,12 @@
|
|||
|
||||
IMPLEMENT_MOCKABLE_FUNCTION(, bool, wait_on_address, volatile_atomic int32_t*, address, int32_t*, compare_address, uint32_t, timeout_ms)
|
||||
{
|
||||
/*Codes_SRS_SYNC_43_001: [ wait_on_address shall atomically compare *address and *compare_address.]*/
|
||||
/*Codes_SRS_SYNC_43_002: [ wait_on_address shall immediately return true if *address is not equal to *compare_address.]*/
|
||||
/*Codes_SRS_SYNC_43_007: [ If *address is equal to *compare_address, wait_on_address shall cause the thread to sleep. ]*/
|
||||
/*Codes_SRS_SYNC_43_009: [ If timeout_ms milliseconds elapse, wait_on_address shall return false. ]*/
|
||||
/*Codes_SRS_SYNC_43_008: [wait_on_address shall wait indefinitely until it is woken up by a call to wake_by_address_[single/all] if timeout_ms is equal to UINT32_MAX]*/
|
||||
/*Codes_SRS_SYNC_43_003: [ wait_on_address shall wait until another thread in the same process signals at address using wake_by_address_[single/all] and return true. ]*/
|
||||
/*Codes_SRS_SYNC_43_001: [ wait_on_address shall atomically compare *address and *compare_address.]*/
|
||||
/*Codes_SRS_SYNC_43_002: [ wait_on_address shall immediately return true if *address is not equal to *compare_address.]*/
|
||||
/*Codes_SRS_SYNC_43_007: [ If *address is equal to *compare_address, wait_on_address shall cause the thread to sleep. ]*/
|
||||
/*Codes_SRS_SYNC_43_009: [ If timeout_ms milliseconds elapse, wait_on_address shall return false. ]*/
|
||||
/*Codes_SRS_SYNC_43_008: [wait_on_address shall wait indefinitely until it is woken up by a call to wake_by_address_[single/all] if timeout_ms is equal to UINT32_MAX]*/
|
||||
/*Codes_SRS_SYNC_43_003: [ wait_on_address shall wait until another thread in the same process signals at address using wake_by_address_[single/all] and return true. ]*/
|
||||
/*Codes_SRS_SYNC_LINUX_43_001: [ wait_on_address shall initialize a timespec struct with .tv_nsec equal to timeout_ms* 10^6. ]*/
|
||||
struct timespec timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1e6 };
|
||||
|
||||
|
@ -27,13 +27,13 @@ IMPLEMENT_MOCKABLE_FUNCTION(, bool, wait_on_address, volatile_atomic int32_t*, a
|
|||
}
|
||||
IMPLEMENT_MOCKABLE_FUNCTION(, void, wake_by_address_all, volatile_atomic int32_t*, address)
|
||||
{
|
||||
/*Codes_SRS_SYNC_43_004: [ wake_by_address_all shall cause all the thread(s) waiting on a call to wait_on_address with argument address to continue execution. ]*/
|
||||
/*Codes_SRS_SYNC_43_004: [ wake_by_address_all shall cause all the thread(s) waiting on a call to wait_on_address with argument address to continue execution. ]*/
|
||||
/*Codes_SRS_SYNC_LINUX_43_005: [ wake_by_address_all shall call syscall from sys/syscall.h with arguments SYS_futex, address, FUTEX_WAKE_PRIVATE, INT_MAX, NULL, NULL, 0. ]*/
|
||||
syscall(SYS_futex, address, FUTEX_WAKE_PRIVATE, INT_MAX, NULL, NULL, 0);
|
||||
}
|
||||
IMPLEMENT_MOCKABLE_FUNCTION(, void, wake_by_address_single, volatile_atomic int32_t*, address)
|
||||
{
|
||||
/*Codes_SRS_SYNC_43_005: [ wake_by_address_single shall cause one thread waiting on a call to wait_on_address with argument address to continue execution. ]*/
|
||||
/*Codes_SRS_SYNC_43_005: [ wake_by_address_single shall cause one thread waiting on a call to wait_on_address with argument address to continue execution. ]*/
|
||||
/*Codes_SRS_SYNC_LINUX_43_006: [ wake_by_address_single shall call syscall from sys/syscall.h with arguments SYS_futex, address, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0. ]*/
|
||||
syscall(SYS_futex, address, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0);
|
||||
}
|
||||
|
|
|
@ -5,18 +5,20 @@
|
|||
#include <cstdlib>
|
||||
#include <cstddef>
|
||||
#include <cstdatomic>
|
||||
#include <climits>
|
||||
#include <ctime>
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdatomic.h>
|
||||
#include <limits.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <sys/syscall.h>
|
||||
#include <linux/futex.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
#include "azure_macro_utils/macro_utils.h"
|
||||
|
@ -47,7 +49,8 @@ static int hook_mock_syscall(long call_code, int* uaddr, int futex_op, int val,
|
|||
/*Tests_SRS_SYNC_LINUX_43_001: [ wait_on_address shall initialize a timespec struct with .tv_nsec equal to timeout_ms* 10^6. ]*/
|
||||
if(check_timeout)
|
||||
{
|
||||
ASSERT_ARE_EQUAL(long, expected_timeout_ms*1e6, timeout->tv_nsec);
|
||||
ASSERT_ARE_EQUAL(long, (expected_timeout_ms / 1000), (long)timeout->tv_sec);
|
||||
ASSERT_ARE_EQUAL(long, (expected_timeout_ms % 1000)*1e6, timeout->tv_nsec);
|
||||
}
|
||||
return expected_return_val;
|
||||
}
|
||||
|
@ -76,7 +79,8 @@ TEST_FUNCTION_INITIALIZE(f)
|
|||
{
|
||||
ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
|
||||
}
|
||||
|
||||
expected_return_val = 0;
|
||||
check_timeout = false;
|
||||
umock_c_reset_all_calls();
|
||||
}
|
||||
|
||||
|
@ -93,7 +97,7 @@ TEST_FUNCTION(wait_on_address_calls_syscall_successfully)
|
|||
///arrange
|
||||
volatile_atomic int32_t var;
|
||||
int32_t val = INT32_MAX;
|
||||
atomic_exchange(&var, val);
|
||||
(void)atomic_exchange(&var, val);
|
||||
check_timeout = true;
|
||||
expected_timeout_ms = 100;
|
||||
expected_return_val = 0;
|
||||
|
@ -114,9 +118,9 @@ TEST_FUNCTION(wait_on_address_calls_sycall_unsuccessfully)
|
|||
///arrange
|
||||
volatile_atomic int32_t var;
|
||||
int32_t val = INT32_MAX;
|
||||
atomic_exchange(&var, val);
|
||||
(void)atomic_exchange(&var, val);
|
||||
check_timeout = true;
|
||||
expected_timeout_ms = 100;
|
||||
expected_timeout_ms = 1500;
|
||||
expected_return_val = -1;
|
||||
STRICT_EXPECTED_CALL(mock_syscall(SYS_futex, (int*)&var, FUTEX_WAIT_PRIVATE, val, IGNORED_ARG, NULL, 0));
|
||||
|
||||
|
@ -132,10 +136,9 @@ TEST_FUNCTION(wait_on_address_calls_sycall_unsuccessfully)
|
|||
TEST_FUNCTION(wake_by_address_all_calls_sycall)
|
||||
{
|
||||
///arrange
|
||||
check_timeout = false;
|
||||
volatile_atomic int32_t var;
|
||||
int32_t val = INT32_MAX;
|
||||
atomic_exchange(&var, val);
|
||||
(void)atomic_exchange(&var, val);
|
||||
STRICT_EXPECTED_CALL(mock_syscall(SYS_futex, (int*)&var, FUTEX_WAKE_PRIVATE, INT_MAX, NULL, NULL, 0));
|
||||
|
||||
///act
|
||||
|
@ -149,10 +152,9 @@ TEST_FUNCTION(wake_by_address_all_calls_sycall)
|
|||
TEST_FUNCTION(wake_by_address_single_calls_sycall)
|
||||
{
|
||||
///arrange
|
||||
check_timeout = false;
|
||||
volatile_atomic int32_t var;
|
||||
int32_t val = INT32_MAX;
|
||||
atomic_exchange(&var, val);
|
||||
(void)atomic_exchange(&var, val);
|
||||
STRICT_EXPECTED_CALL(mock_syscall(SYS_futex, (int*)&var, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0));
|
||||
|
||||
///act
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <cstdint>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "windows.h"
|
||||
#include "sync.h"
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ TEST_FUNCTION(wait_on_address_calls_WaitOnAddress_successfully)
|
|||
///arrange
|
||||
volatile int32_t var;
|
||||
int32_t val = INT32_MAX;
|
||||
InterlockedExchange((volatile LONG*)&var, val);
|
||||
(void)InterlockedExchange((volatile LONG*)&var, val);
|
||||
uint32_t timeout = 1000;
|
||||
STRICT_EXPECTED_CALL(mock_WaitOnAddress((volatile VOID*)&var, (PVOID)&val, (SIZE_T)4, (DWORD)timeout))
|
||||
.SetReturn(true);
|
||||
|
@ -100,7 +100,7 @@ TEST_FUNCTION(wait_on_address_calls_WaitOnAddress_unsuccessfully)
|
|||
///arrange
|
||||
volatile int32_t var;
|
||||
int32_t val = INT32_MAX;
|
||||
InterlockedExchange((volatile LONG*)&var, val);
|
||||
(void)InterlockedExchange((volatile LONG*)&var, val);
|
||||
uint32_t timeout = 1000;
|
||||
STRICT_EXPECTED_CALL(mock_WaitOnAddress((volatile VOID*)&var, (PVOID)&val, (SIZE_T)4, (DWORD)timeout))
|
||||
.SetReturn(false);
|
||||
|
@ -119,7 +119,7 @@ TEST_FUNCTION(wake_by_address_all_calls_WakeByAddressAll)
|
|||
///arrange
|
||||
volatile int32_t var;
|
||||
int32_t val = INT32_MAX;
|
||||
InterlockedExchange((volatile LONG*)&var, val);
|
||||
(void)InterlockedExchange((volatile LONG*)&var, val);
|
||||
STRICT_EXPECTED_CALL(mock_WakeByAddressAll((PVOID)&var));
|
||||
///act
|
||||
wake_by_address_all(&var);
|
||||
|
@ -134,7 +134,7 @@ TEST_FUNCTION(wake_by_address_single_calls_WakeByAddressSingle)
|
|||
///arrange
|
||||
volatile int32_t var;
|
||||
int32_t val = INT32_MAX;
|
||||
InterlockedExchange((volatile LONG*)&var, val);
|
||||
(void)InterlockedExchange((volatile LONG*)&var, val);
|
||||
STRICT_EXPECTED_CALL(mock_WakeByAddressSingle((PVOID)&var));
|
||||
///act
|
||||
wake_by_address_single(&var);
|
||||
|
|
Загрузка…
Ссылка в новой задаче