Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fix timer race code #412

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

add fix timer race code #412

wants to merge 11 commits into from

Conversation

M-iceberg
Copy link
Contributor

No description provided.

@M-iceberg
Copy link
Contributor Author

M-iceberg commented Nov 14, 2024

/azp run #Resolved

Copy link

azure-pipelines bot commented Nov 14, 2024

Azure Pipelines successfully started running 1 pipeline(s).

#Resolved

@M-iceberg
Copy link
Contributor Author

M-iceberg commented Nov 19, 2024

/azp run #Resolved

Copy link

azure-pipelines bot commented Nov 19, 2024

Azure Pipelines successfully started running 1 pipeline(s).

#Resolved

@@ -21,10 +21,13 @@ extern "C" {
#endif

typedef struct THREADPOOL_TAG THREADPOOL;
typedef struct TIMER_INSTANCE_TAG* TIMER_INSTANCE_HANDLE;
typedef struct TIMER_INSTANCE_TAG TIMER_INSTANCE;
typedef struct TIMER_INSTANCE_TAG* TIMER_INSTANCE_HANDLE; //todo: if this one is needed
Copy link
Contributor

@darobs darobs Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably remove TIMER_INSTANCE_HANDLE #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not removed, I can still see it.


MOCKABLE_FUNCTION(, int, threadpool_timer_restart, TIMER_INSTANCE_HANDLE, timer, uint32_t, start_delay_ms, uint32_t, timer_period_ms);
MOCKABLE_FUNCTION(, int, threadpool_timer_restart, THANDLE(TIMER_INSTANCE)*, timer, uint32_t, start_delay_ms, uint32_t, timer_period_ms);
Copy link
Contributor

@darobs darobs Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)*

timer is just THANDLE(TIMER_INSTANCE), not THANDLE(TIMER_INSTANCE)* Same for line 92. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -1140,9 +1141,10 @@ TEST_FUNCTION(threadpool_timer_start_with_NULL_work_function_context_succeeds)
// arrange
THANDLE(THREADPOOL) threadpool = test_create_and_open_threadpool();

TIMER_INSTANCE_HANDLE timer_instance;
THANDLE(TIMER_INSTANCE) timer_instance = NULL;
Copy link
Contributor

@darobs darobs Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timer_instance = NULL

Do we have to assign this to be NULL for threadpool_timer_start to work? I can see this might be an annoyance when the timer is part of a struct, like it generally is. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will getting segmentation fault if not set this THANDLE to NULL

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The common pattern we have in the code that uses this is

typedef struct SOME_STRUCT_TAG
{
//  ...
THANDLE(TIMER_INSTANCE) timer;
// ...
} SOME_STRUCT;

// then in the "_create" function:
SOME_STRUCT_HANDLE  result = (SOME_STRUCT_HANDLE)malloc(sizeof(SOME_STRUCT);
threadpool_timer_start(threadpool, start_delay, period, test_work_function, context, &result->timer);

The timer is being passed in uninitialized, and so requiring it to be initialized in the test implies that the above pattern will need to change. BTW, this comment goes along with using THANDLE_INITIALIZE_MOVE instead of THANDLE_MOVE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modified to initialize_move

threadpool_internal_set_timer(tp_timer, start_delay_ms, timer_period_ms);

/* Codes_SRS_THREADPOOL_WIN32_42_009: [ threadpool_timer_start shall return the allocated handle in timer_handle. ]*/
THANDLE_MOVE(TIMER_INSTANCE)(timer_handle, &timer_temp);
Copy link
Contributor

@darobs darobs Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THANDLE_MOVE(TIMER_INSTANCE)(timer_handle, &timer_temp);

Would THANDLE_INITIALIZE_MOVE make more sense here? #Resolved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for Linux?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -18,8 +18,7 @@
threadpool_schedule_work, \
threadpool_timer_start, \
threadpool_timer_restart, \
threadpool_timer_cancel, \
threadpool_timer_destroy \
threadpool_timer_cancel
) \
Copy link
Contributor

@darobs darobs Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build failed, missing \ at the end of this line #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link

azure-pipelines bot commented Nov 19, 2024

No pipelines are associated with this pull request.

#Resolved

1 similar comment
Copy link

azure-pipelines bot commented Nov 19, 2024

No pipelines are associated with this pull request.

#Resolved

@M-iceberg
Copy link
Contributor Author

M-iceberg commented Nov 19, 2024

/azp run #Resolved

Copy link

azure-pipelines bot commented Nov 19, 2024

Azure Pipelines successfully started running 1 pipeline(s).

#Resolved

@M-iceberg
Copy link
Contributor Author

M-iceberg commented Nov 19, 2024

/azp run #Resolved

Copy link

azure-pipelines bot commented Nov 19, 2024

Azure Pipelines successfully started running 1 pipeline(s).

#Resolved

@@ -21,10 +21,13 @@ extern "C" {
#endif

typedef struct THREADPOOL_TAG THREADPOOL;
typedef struct TIMER_INSTANCE_TAG TIMER_INSTANCE;
typedef struct TIMER_INSTANCE_TAG* TIMER_INSTANCE_HANDLE;
Copy link
Member

@dcristoloveanu dcristoloveanu Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIMER_INSTANCE_HANDLE

Who uses the TIMER_INSTANCE_HANDLE? I am imagining that after this change there should be only one, right? #Resolved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or are there more PRs planned? If so, we should add a comment explaining that, otherwise it is sort of confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the timer_instance_handle, not using anymore

for (uint32_t i = 0; i < N_TIMERS; i++)
{
ASSERT_ARE_EQUAL(int, 0, threadpool_timer_start(threadpool, 100, 500, work_function, (void*)&call_count, &timers[i]));
THANDLE_INITIALIZE(TIMER_INSTANCE)((void*) & timers[i], NULL);
ASSERT_ARE_EQUAL(int, 0, threadpool_timer_start(threadpool, 100, 500, work_function, (void*)&call_count, (void*) & timers[i]));
Copy link
Member

@dcristoloveanu dcristoloveanu Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(void*)

Needed? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed

(void)InterlockedDecrement(&chaos_test_data->timers_starting);
WakeByAddressSingle((PVOID)&chaos_test_data->timers_starting);
}
break;
Copy link
Member

@dcristoloveanu dcristoloveanu Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break;

break should go within statement scope (before line 962). #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

(void)InterlockedDecrement(&chaos_test_data->timers_starting);
WakeByAddressSingle((PVOID)&chaos_test_data->timers_starting);
}
case TEST_ACTION_SCHEDULE_WORK_ITEM:
Copy link
Member

@dcristoloveanu dcristoloveanu Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing break; #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

if (threadpool_schedule_work_item(chaos_test_data->threadpool, chaos_test_data->work_item_context) == 0)
{
(void)InterlockedIncrement64(&chaos_test_data->expected_call_count);
}
Copy link
Member

@dcristoloveanu dcristoloveanu Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Consider adding the else branch empty (just with a comment saying that we don't care if it failed. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@dcristoloveanu
Copy link
Member

dcristoloveanu commented Nov 20, 2024

    break;

Break should go one line higher #Resolved


Refers to: win32/tests/threadpool_win32_int/threadpool_win32_int.c:1064 in 491e0ff. [](commit_id = 491e0ff, deletion_comment = False)

@dcristoloveanu
Copy link
Member

    break;

Here and below.


In reply to: 2487086153


Refers to: win32/tests/threadpool_win32_int/threadpool_win32_int.c:1064 in 491e0ff. [](commit_id = 491e0ff, deletion_comment = False)

@@ -1008,12 +1099,9 @@ static DWORD WINAPI chaos_thread_with_timers_no_lock_func(LPVOID lpThreadParamet
}
Copy link
Member

@dcristoloveanu dcristoloveanu Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Missing break; #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

// assert that all scheduled items were executed
ASSERT_ARE_EQUAL(int64_t, (int64_t)InterlockedAdd64(&chaos_test_data.expected_call_count, 0), (int64_t)InterlockedAdd64(&chaos_test_data.executed_work_functions, 0));

LogInfo("Chaos test executed %" PRIu64 " work items, %" PRIu64 " timers",
Copy link
Member

@dcristoloveanu dcristoloveanu Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRIu64

interlocked APIs work with signed values, this should be PRId64. Please check the other places too. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@dcristoloveanu
Copy link
Member

TEST_FUNCTION(schedule_after_close_works_v2)

I think the same chaos tests should be run on Linux also.
As a matter of fact, why are we even having 2 separate test suites for Linux and Windows for the integration part?

Unit tests should be different, but at integration level we should be running exactly the same test code on both platforms and it should pass ....


Refers to: linux/tests/threadpool_linux_int/threadpool_linux_int.c:869 in 491e0ff. [](commit_id = 491e0ff, deletion_comment = False)

Copy link
Member

@dcristoloveanu dcristoloveanu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕐

Copy link

azure-pipelines bot commented Nov 20, 2024

Pull request contains merge conflicts.

#Resolved

1 similar comment
Copy link

azure-pipelines bot commented Nov 20, 2024

Pull request contains merge conflicts.

#Resolved

@M-iceberg
Copy link
Contributor Author

    break;

done


In reply to: 2487086295


Refers to: win32/tests/threadpool_win32_int/threadpool_win32_int.c:1064 in 491e0ff. [](commit_id = 491e0ff, deletion_comment = False)

@M-iceberg
Copy link
Contributor Author

/azp run

Copy link

Pull request contains merge conflicts.

@M-iceberg
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@M-iceberg
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@M-iceberg
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@M-iceberg
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants