Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions win32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(pal_win32_h_files
${pal_common_h_files}
inc/c_pal/execution_engine_win32.h
inc/c_pal/job_object_helper.h
inc/c_pal/robust_semaphore.h
)

set(pal_win32_c_files
Expand All @@ -64,6 +65,7 @@ set(pal_win32_c_files
src/${gballoc_ll_c}
src/${gballoc_hl_c}
src/job_object_helper.c
src/robust_semaphore.c
)

FILE(GLOB pal_win32_md_files "devdoc/*.md")
Expand Down
224 changes: 224 additions & 0 deletions win32/devdoc/robust_semaphore_requirements.md

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions win32/inc/c_pal/robust_semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef ROBUST_SEMAPHORE_H
#define ROBUST_SEMAPHORE_H

#ifdef __cplusplus
#include <cstdint>
#else
#include <stdint.h>
#endif

#include "macro_utils/macro_utils.h"

#include "umock_c/umock_c_prod.h"

/* a robust semaphore with N units is made of N mutexes, waited on together with the cancel event (MAXIMUM_WAIT_OBJECTS is 64) */
#define ROBUST_SEMAPHORE_MAX_COUNT 63

/* wait for a unit until one is free / hold a unit with no time limit */
#define ROBUST_SEMAPHORE_NO_TIMEOUT UINT32_MAX

#define ROBUST_SEMAPHORE_UNIT_INDEX_NONE UINT32_MAX

#define ROBUST_SEMAPHORE_ACQUIRE_RESULT_VALUES \
ROBUST_SEMAPHORE_ACQUIRE_OK, \
ROBUST_SEMAPHORE_ACQUIRE_TIMEOUT, \
ROBUST_SEMAPHORE_ACQUIRE_CANCELLED, \
ROBUST_SEMAPHORE_ACQUIRE_ERROR

MU_DEFINE_ENUM(ROBUST_SEMAPHORE_ACQUIRE_RESULT, ROBUST_SEMAPHORE_ACQUIRE_RESULT_VALUES);

typedef struct ROBUST_SEMAPHORE_TAG* ROBUST_SEMAPHORE_HANDLE;

typedef void (*ROBUST_SEMAPHORE_ON_ACQUIRE_COMPLETE)(void* context, ROBUST_SEMAPHORE_ACQUIRE_RESULT result, uint32_t unit_index);

#ifdef __cplusplus
extern "C"
{
#endif
MOCKABLE_FUNCTION(, ROBUST_SEMAPHORE_HANDLE, robust_semaphore_create, const char*, name, uint32_t, max_count, const char*, security_descriptor);
MOCKABLE_FUNCTION(, void, robust_semaphore_destroy, ROBUST_SEMAPHORE_HANDLE, robust_semaphore);
MOCKABLE_FUNCTION(, int, robust_semaphore_acquire_async, ROBUST_SEMAPHORE_HANDLE, robust_semaphore, uint32_t, timeout_ms, uint32_t, max_hold_ms, ROBUST_SEMAPHORE_ON_ACQUIRE_COMPLETE, on_acquire_complete, void*, on_acquire_complete_context);
MOCKABLE_FUNCTION(, void, robust_semaphore_cancel, ROBUST_SEMAPHORE_HANDLE, robust_semaphore);
MOCKABLE_FUNCTION(, void, robust_semaphore_release, ROBUST_SEMAPHORE_HANDLE, robust_semaphore);
#ifdef __cplusplus
}
#endif

#endif // ROBUST_SEMAPHORE_H
712 changes: 712 additions & 0 deletions win32/src/robust_semaphore.c

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions win32/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ if(${run_unittests})

build_test_folder(gballoc_hl_passthrough_ut)
build_test_folder(job_object_helper_ut)
build_test_folder(robust_semaphore_ut)
endif()

if(${run_int_tests})
add_subdirectory(job_object_helper_tester)
add_subdirectory(process_watchdog_int_child)
add_subdirectory(robust_semaphore_tester)
build_test_folder(malloc_multi_flex_int)
build_test_folder(string_utils_int)
build_test_folder(job_object_helper_int)
build_test_folder(process_watchdog_int)
build_test_folder(robust_semaphore_int)

if((${GBALLOC_LL_TYPE} STREQUAL "MIMALLOC"))
build_test_folder(gballoc_ll_mimalloc_int)
Expand Down
24 changes: 24 additions & 0 deletions win32/tests/robust_semaphore_int/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#Copyright (c) Microsoft. All rights reserved.
#Licensed under the MIT license. See LICENSE file in the project root for full license information.

set(theseTestsName robust_semaphore_int)

set(${theseTestsName}_test_files
${theseTestsName}.c
)

set(${theseTestsName}_c_files
)

set(${theseTestsName}_h_files
)

build_test_artifacts(${theseTestsName} "tests/c_pal/win32" ADDITIONAL_LIBS c_pal)

# the tests start robust_semaphore_tester to hold a unit from another process
if(TARGET ${theseTestsName}_exe_${CMAKE_PROJECT_NAME})
add_dependencies(${theseTestsName}_exe_${CMAKE_PROJECT_NAME} robust_semaphore_tester)
endif()
if(TARGET ${theseTestsName}_dll_${CMAKE_PROJECT_NAME})
add_dependencies(${theseTestsName}_dll_${CMAKE_PROJECT_NAME} robust_semaphore_tester)
endif()
Loading
Loading