Skip to content

Commit

Permalink
posix: pthread: add logging to pthread key
Browse files Browse the repository at this point in the history
To align with other supported POSIX features, add logging to
pthread key to provide better error reporting and diagnostics.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
  • Loading branch information
cfriedt committed Nov 30, 2023
1 parent 5dc561f commit 742de21
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/posix/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "posix_internal.h"

#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/posix/pthread.h>
#include <zephyr/posix/pthread_key.h>
#include <zephyr/sys/bitarray.h>
Expand All @@ -17,6 +18,8 @@ struct pthread_key_data {
pthread_thread_data thread_data;
};

LOG_MODULE_REGISTER(pthread_key, CONFIG_PTHREAD_KEY_LOG_LEVEL);

static struct k_spinlock pthread_key_lock;

/* This is non-standard (i.e. an implementation detail) */
Expand Down Expand Up @@ -50,16 +53,19 @@ static pthread_key_obj *get_posix_key(pthread_key_t key)

/* if the provided cond does not claim to be initialized, its invalid */
if (!is_pthread_obj_initialized(key)) {
LOG_ERR("Key is uninitialized (%x)", key);
return NULL;
}

/* Mask off the MSB to get the actual bit index */
if (sys_bitarray_test_bit(&posix_key_bitarray, bit, &actually_initialized) < 0) {
LOG_ERR("Key is invalid (%x)", key);
return NULL;
}

if (actually_initialized == 0) {
/* The cond claims to be initialized but is actually not */
LOG_ERR("Key claims to be initialized (%x)", key);
return NULL;
}

Expand Down Expand Up @@ -110,6 +116,7 @@ int pthread_key_create(pthread_key_t *key,
sys_slist_init(&(new_key->key_data_l));

new_key->destructor = destructor;
LOG_DBG("Initialized key %p (%x)", new_key, *key);

return 0;
}
Expand Down Expand Up @@ -146,6 +153,7 @@ int pthread_key_delete(pthread_key_t key)

/* Deallocate the object's memory */
k_free((void *)key_data);
LOG_DBG("Freed key data %p for key %x in thread %x", key_data, key, pthread_self());
}

bit = posix_key_to_offset(key_obj);
Expand All @@ -154,6 +162,8 @@ int pthread_key_delete(pthread_key_t key)

k_spin_unlock(&pthread_key_lock, key_key);

LOG_DBG("Deleted key %p (%x)", key_obj, key);

return 0;
}

Expand Down Expand Up @@ -194,6 +204,8 @@ int pthread_setspecific(pthread_key_t key, const void *value)
* associate thread specific data
*/
thread_spec_data->spec_data = (void *)value;
LOG_DBG("Paired key %x to value %p for thread %x", key, value,
pthread_self());
goto out;
}
}
Expand All @@ -202,10 +214,14 @@ int pthread_setspecific(pthread_key_t key, const void *value)
key_data = k_malloc(sizeof(struct pthread_key_data));

if (key_data == NULL) {
LOG_ERR("Failed to allocate key data for key %x", key);
retval = ENOMEM;
goto out;
}

LOG_DBG("Allocated key data %p for key %x in thread %x", key_data, key,
pthread_self());

/* Associate thread specific data, initialize new key */
key_data->thread_data.key = key_obj;
key_data->thread_data.spec_data = (void *)value;
Expand All @@ -215,6 +231,8 @@ int pthread_setspecific(pthread_key_t key, const void *value)

/* Append new key data to the key object's list */
sys_slist_append(&(key_obj->key_data_l), (sys_snode_t *)key_data);

LOG_DBG("Paired key %x to value %p for thread %x", key, value, pthread_self());
}

out:
Expand Down

0 comments on commit 742de21

Please sign in to comment.