Skip to content

Commit

Permalink
Renames for PR and build fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHenson committed Nov 3, 2023
1 parent 5192871 commit 657f800
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
#include <aws/common/byte_buf.h>
#include <aws/common/common.h>

struct aws_ipc_util_instance_lock;
struct aws_cross_process_lock;
AWS_EXTERN_C_BEGIN

/**
* Attempts to acquire a system-wide (not per process or per user) lock scoped by instance_nonce.
* For any given unique nonce, a lock will be returned by the first caller. Subsequent calls will
* return NULL until the either the process owning the lock exits or the program owning the lock
* calls aws_ipc_util_instance_lock_release() explicitly.
* calls aws_cross_process_lock_release() explicitly.
*
* If the process exits before the lock is released, the kernel will unlock it for the next consumer.
*/
AWS_COMMON_API
struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
struct aws_cross_process_lock *aws_cross_process_lock_try_acquire(
struct aws_allocator *allocator,
struct aws_byte_cursor instance_nonce);

/**
* Releases the lock so the next caller (may be another process) can get an instance of the lock.
*/
AWS_COMMON_API
void aws_ipc_util_instance_lock_release(struct aws_ipc_util_instance_lock *instance_lock);
void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock);

AWS_EXTERN_C_END

Expand Down
21 changes: 10 additions & 11 deletions source/posix/ipc_util.c → source/posix/cross_process_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/ipc_util.h>
#include <aws/common/cross_process_lock.h>

#include <aws/common/byte_buf.h>
#include <errno.h>
#include <sys/file.h>
#include <unistd.h>

#include <aws/common/error.h>
#include <aws/common/file.h>
#include <aws/common/logging.h>
#include <aws/common/error.h>

struct aws_ipc_util_instance_lock {
struct aws_cross_process_lock {
struct aws_allocator *allocator;
int locked_fd;
};

struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
struct aws_cross_process_lock *aws_cross_process_lock_try_acquire(
struct aws_allocator *allocator,
struct aws_byte_cursor instance_nonce) {

Expand Down Expand Up @@ -56,7 +56,7 @@ struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
aws_byte_buf_append_dynamic(&nonce_buf, &path_suffix);
aws_byte_buf_append_null_terminator(&nonce_buf);

struct aws_ipc_util_instance_lock *instance_lock = NULL;
struct aws_cross_process_lock *instance_lock = NULL;

int fd = open((const char *)nonce_buf.buffer, O_CREAT | O_RDWR, 0666);
if (fd < 0) {
Expand All @@ -73,13 +73,12 @@ struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
AWS_LS_COMMON_GENERAL,
"static: Lock file %s already acquired by another instance",
(const char *)nonce_buf.buffer);
close(fd);
close(fd);
aws_raise_error(AWS_ERROR_MUTEX_CALLER_NOT_OWNER);
goto cleanup;
}

instance_lock =
aws_mem_calloc(allocator, 1, sizeof(struct aws_ipc_util_instance_lock));
instance_lock = aws_mem_calloc(allocator, 1, sizeof(struct aws_cross_process_lock));
instance_lock->locked_fd = fd;
instance_lock->allocator = allocator;

Expand All @@ -89,13 +88,13 @@ struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
(const char *)nonce_buf.buffer,
fd);

clean_up:
aws_byte_buf_clean_up(&nonce_buf)
cleanup:
aws_byte_buf_clean_up(&nonce_buf);

return instance_lock;
}

void aws_ipc_util_instance_lock_release(struct aws_ipc_util_instance_lock *instance_lock) {
void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock) {
if (instance_lock) {
flock(instance_lock->locked_fd, LOCK_UN);
close(instance_lock->locked_fd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
#include <windows.h>

#include <aws/common/byte_buf.h>
#include <aws/common/ipc_util.h>
#include <aws/common/logging.h>
#include <aws/common/cross_process_lock.h>
#include <aws/common/error.h>
#include <aws/common/logging.h>
#include <inttypes.h>

struct aws_ipc_util_instance_lock {
struct aws_cross_process_lock {
struct aws_allocator *allocator;
HANDLE mutex;
};

struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
struct aws_cross_process_lock *aws_cross_process_lock_try_acquire(
struct aws_allocator *allocator,
struct aws_byte_cursor instance_nonce) {

Expand All @@ -33,7 +33,7 @@ struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
return NULL;
}

struct aws_ipc_util_instance_lock *instance_lock = NULL;
struct aws_cross_process_lock *instance_lock = NULL;

/* Local prefix, per the docs, specifies session scope rather than global to the user or system. */
struct aws_byte_cursor path_prefix = aws_byte_cursor_from_c_str("Local/aws_crt_cross_process_lock/");
Expand Down Expand Up @@ -62,13 +62,12 @@ struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
AWS_LS_COMMON_GENERAL,
"static: Lock %s is already acquired by another instance",
(const char *)nonce_buf.buffer);
CloseHandle(mutex);
CloseHandle(mutex);
aws_raise_error(AWS_ERROR_MUTEX_CALLER_NOT_OWNER);
goto cleanup;
}

instance_lock =
aws_mem_calloc(allocator, 1, sizeof(struct aws_ipc_util_instance_lock));
instance_lock = aws_mem_calloc(allocator, 1, sizeof(struct aws_cross_process_lock));
instance_lock->mutex = mutex;
instance_lock->allocator = allocator;

Expand All @@ -85,7 +84,7 @@ struct aws_ipc_util_instance_lock *aws_ipc_util_instance_lock_try_acquire(
return instance_lock;
}

void aws_ipc_util_instance_lock_release(struct aws_ipc_util_instance_lock *instance_lock) {
void aws_ipc_util_instance_lock_release(struct aws_cross_process_lock *instance_lock) {
if (instance_lock) {
CloseHandle(instance_lock->mutex);
AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "static: Lock released for handle %p", (void *)instance_lock->mutex);
Expand Down
8 changes: 4 additions & 4 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,11 @@ add_test_case(test_uri_encode_path_rfc3986)
add_test_case(test_uri_encode_query)
add_test_case(test_uri_decode)

add_test_case(test_instance_lock_works_in_proc)
add_test_case(test_instance_lock_works_cross_proc)
add_test_case(test_cross_process_lock_works_in_proc)
add_test_case(test_cross_process_lock_works_cross_proc)
#this one is here for use by test_instance_lock_works_cross_proc
add_test_case(instance_lock_mp_test_runner)
add_test_case(test_instance_lock_invalid_nonce_fails)
add_test_case(cross_process_lock_mp_test_runner)
add_test_case(test_cross_process_lock_invalid_nonce_fails)


generate_test_driver(${PROJECT_NAME}-tests)
Expand Down
40 changes: 19 additions & 21 deletions tests/io_util_tests.c → tests/cross_process_lock_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/cross_process_lock.h>
#include <aws/common/environment.h>
#include <aws/common/ipc_util.h>
#include <aws/common/process.h>
#include <aws/testing/aws_test_harness.h>

static int s_test_instance_lock_works_in_proc(struct aws_allocator *allocator, void *ctx) {
static int s_test_cross_process_lock_works_in_proc(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_common_library_init(allocator);

struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("lock_nonce");
struct aws_ipc_util_instance_lock *instance_lock = aws_ipc_util_instance_lock_try_acquire(allocator, lock_nonce);
struct aws_cross_process_lock *instance_lock = aws_cross_process_lock_try_acquire(allocator, lock_nonce);
ASSERT_NOT_NULL(instance_lock);

struct aws_ipc_util_instance_lock *should_be_null = aws_ipc_util_instance_lock_try_acquire(allocator, lock_nonce);
struct aws_cross_process_lock *should_be_null = aws_cross_process_lock_try_acquire(allocator, lock_nonce);
ASSERT_NULL(should_be_null);

aws_ipc_util_instance_lock_release(instance_lock);
struct aws_ipc_util_instance_lock *should_not_be_null =
aws_ipc_util_instance_lock_try_acquire(allocator, lock_nonce);
aws_cross_process_lock_release(instance_lock);
struct aws_cross_process_lock *should_not_be_null = aws_cross_process_lock_try_acquire(allocator, lock_nonce);
ASSERT_NOT_NULL(should_not_be_null);
aws_ipc_util_instance_lock_release(should_not_be_null);
aws_cross_process_lock_release(should_not_be_null);

aws_common_library_clean_up();

return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_instance_lock_works_in_proc, s_test_instance_lock_works_in_proc)
AWS_TEST_CASE(test_cross_process_lock_works_in_proc, s_test_cross_process_lock_works_in_proc)

static int s_instance_lock_mp_test_runner(struct aws_allocator *allocator, void *ctx) {
static int s_cross_process_lock_mp_test_runner(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_common_library_init(allocator);

Expand All @@ -41,20 +40,19 @@ static int s_instance_lock_mp_test_runner(struct aws_allocator *allocator, void
if (aws_get_environment_value(allocator, test_run_gate, &output_val) == AWS_OP_SUCCESS && output_val) {
aws_string_destroy(output_val);
struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("lock_mp_nonce");
struct aws_ipc_util_instance_lock *instance_lock =
aws_ipc_util_instance_lock_try_acquire(allocator, lock_nonce);
struct aws_cross_process_lock *instance_lock = aws_cross_process_lock_try_acquire(allocator, lock_nonce);
ASSERT_NOT_NULL(instance_lock);

aws_ipc_util_instance_lock_release(instance_lock);
aws_cross_process_lock_release(instance_lock);
}
aws_string_destroy(test_run_gate);
aws_common_library_clean_up();

return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(instance_lock_mp_test_runner, s_instance_lock_mp_test_runner)
AWS_TEST_CASE(cross_process_lock_mp_test_runner, s_cross_process_lock_mp_test_runner)

static int s_test_instance_lock_works_cross_proc(struct aws_allocator *allocator, void *ctx) {
static int s_test_cross_process_lock_works_cross_proc(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_common_library_init(allocator);

Expand Down Expand Up @@ -84,21 +82,21 @@ static int s_test_instance_lock_works_cross_proc(struct aws_allocator *allocator
aws_run_command_result_cleanup(&result);
AWS_ZERO_STRUCT(result);
struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("lock_mp_nonce");
struct aws_ipc_util_instance_lock *instance_lock = aws_ipc_util_instance_lock_try_acquire(allocator, lock_nonce);
struct aws_cross_process_lock *instance_lock = aws_cross_process_lock_try_acquire(allocator, lock_nonce);
ASSERT_NOT_NULL(instance_lock);

ASSERT_SUCCESS(aws_run_command(allocator, &command_options, &result));
ASSERT_FALSE(result.ret_code == 0);
aws_run_command_result_cleanup(&result);

aws_ipc_util_instance_lock_release(instance_lock);
aws_cross_process_lock_release(instance_lock);
aws_common_library_clean_up();

return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_instance_lock_works_cross_proc, s_test_instance_lock_works_cross_proc)
AWS_TEST_CASE(test_cross_process_lock_works_cross_proc, s_test_cross_process_lock_works_cross_proc)

static int s_test_instance_lock_invalid_nonce(struct aws_allocator *allocator, void *ctx) {
static int s_test_cross_process_lock_invalid_nonce(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_common_library_init(allocator);
#ifdef _WIN32
Expand All @@ -107,12 +105,12 @@ static int s_test_instance_lock_invalid_nonce(struct aws_allocator *allocator, v
struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("invalid/lock_nonce");
#endif /* _WIN32 */

struct aws_ipc_util_instance_lock *instance_lock = aws_ipc_util_instance_lock_try_acquire(allocator, lock_nonce);
struct aws_cross_process_lock *instance_lock = aws_cross_process_lock_try_acquire(allocator, lock_nonce);
ASSERT_NULL(instance_lock);
ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error());

aws_common_library_clean_up();

return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_instance_lock_invalid_nonce_fails, s_test_instance_lock_invalid_nonce)
AWS_TEST_CASE(test_cross_process_lock_invalid_nonce_fails, s_test_cross_process_lock_invalid_nonce)

0 comments on commit 657f800

Please sign in to comment.