mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-03 14:01:53 +00:00
pthread: Fix behaviour when pthread destructor calls pthread_getspecific/pthread_setspecific
Update as per specification at https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html Specifically: - Before a destructor is called then the value for the corresponding key is already set to NULL. - If a destructor calls pthread_setspecific() to assign a non-NULL value then this destructor is called again, after all existing non-NULL values have been called. Adds a test for this relatively complex behaviour. Closes https://github.com/espressif/esp-idf/issues/6643
This commit is contained in:
committed by
Ivan Grokhotkov
parent
3c0d892d43
commit
564229c9a6
@@ -162,3 +162,90 @@ TEST_CASE("pthread local storage stress test", "[pthread]")
|
||||
TEST_ASSERT_EQUAL(0, pthread_join(threads[i], NULL));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define NUM_KEYS 4 // number of keys used in repeat destructor test
|
||||
#define NUM_REPEATS 17 // number of times we re-set a key to a non-NULL value to re-trigger destructor
|
||||
|
||||
typedef struct {
|
||||
pthread_key_t keys[NUM_KEYS]; // pthread local storage keys used in test
|
||||
unsigned count; // number of times the destructor has been called
|
||||
int last_idx; // index of last key where destructor was called
|
||||
} destr_test_state_t;
|
||||
|
||||
static void s_test_repeat_destructor(void *vp_state);
|
||||
static void *s_test_repeat_destructor_thread(void *vp_state);
|
||||
|
||||
// Test the correct behaviour of a pthread destructor function that uses
|
||||
// pthread_setspecific() to set another value when it runs, and also
|
||||
//
|
||||
// As described in https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html
|
||||
TEST_CASE("pthread local storage 'repeat' destructor test", "[pthread]")
|
||||
{
|
||||
int r;
|
||||
destr_test_state_t state = { .last_idx = -1 };
|
||||
pthread_t thread;
|
||||
|
||||
for (int i = 0; i < NUM_KEYS; i++) {
|
||||
r = pthread_key_create(&state.keys[i], s_test_repeat_destructor);
|
||||
TEST_ASSERT_EQUAL(0, r);
|
||||
}
|
||||
|
||||
r = pthread_create(&thread, NULL, s_test_repeat_destructor_thread, &state);
|
||||
TEST_ASSERT_EQUAL(0, r);
|
||||
|
||||
r = pthread_join(thread, NULL);
|
||||
TEST_ASSERT_EQUAL(0 ,r);
|
||||
|
||||
// Cheating here to make sure compiler reads the value of 'count' from memory not from a register
|
||||
//
|
||||
// We expect the destructor was called NUM_REPEATS times when it repeated, then NUM_KEYS times when it didn't
|
||||
TEST_ASSERT_EQUAL(NUM_REPEATS + NUM_KEYS, ((volatile destr_test_state_t)state).count);
|
||||
|
||||
// cleanup
|
||||
for (int i = 0; i < NUM_KEYS; i++) {
|
||||
r = pthread_key_delete(state.keys[i]);
|
||||
TEST_ASSERT_EQUAL(0, r);
|
||||
}
|
||||
}
|
||||
|
||||
static void s_test_repeat_destructor(void *vp_state)
|
||||
{
|
||||
destr_test_state_t *state = vp_state;
|
||||
|
||||
state->count++;
|
||||
printf("Destructor! Arg %p Count %d\n", state, state->count);
|
||||
if (state->count > NUM_REPEATS) {
|
||||
return; // Stop replacing values after NUM_REPEATS destructors have been called, they will be NULLed out now
|
||||
}
|
||||
|
||||
// Find the key which has a NULL value, this is the key for this destructor. We will set it back to 'state' to repeat later.
|
||||
// At this point only one key should have a NULL value
|
||||
int null_idx = -1;
|
||||
for (int i = 0; i < NUM_KEYS; i++) {
|
||||
if (pthread_getspecific(state->keys[i]) == NULL) {
|
||||
TEST_ASSERT_EQUAL(-1, null_idx); // If more than one key has a NULL value, something has gone wrong
|
||||
null_idx = i;
|
||||
// don't break, verify the other keys have non-NULL values
|
||||
}
|
||||
}
|
||||
|
||||
TEST_ASSERT_NOT_EQUAL(-1, null_idx); // One key should have a NULL value
|
||||
|
||||
// The same key shouldn't be destroyed twice in a row, as new non-NULL values should be destroyed
|
||||
// after existing non-NULL values (to match spec behaviour)
|
||||
TEST_ASSERT_NOT_EQUAL(null_idx, state->last_idx);
|
||||
|
||||
printf("Re-setting index %d\n", null_idx);
|
||||
pthread_setspecific(state->keys[null_idx], state);
|
||||
state->last_idx = null_idx;
|
||||
}
|
||||
|
||||
static void *s_test_repeat_destructor_thread(void *vp_state)
|
||||
{
|
||||
destr_test_state_t *state = vp_state;
|
||||
for (int i = 0; i < NUM_KEYS; i++) {
|
||||
pthread_setspecific(state->keys[i], state);
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user