posix: add pthread_key and pthread_once APIs

Added 4 new pthread_key APIs for thread-specific data
key creation, deletion, setting and getting the values.

Added a key list to the posix_struct for threads.

Added pthread_once API.

Signed-off-by: Niranjhana N <niranjhana.n@intel.com>
This commit is contained in:
Niranjhana N 2018-04-11 16:00:09 +05:30 committed by Anas Nashif
commit 414c39fc94
6 changed files with 277 additions and 1 deletions

View file

@ -13,6 +13,7 @@
#include <posix/unistd.h>
#include "sys/types.h"
#include "posix_sched.h"
#include <posix/pthread_key.h>
#include <stdlib.h>
#include <string.h>
@ -30,6 +31,9 @@ enum pthread_state {
struct posix_thread {
struct k_thread thread;
/* List of keys that thread has called pthread_setspecific() on */
sys_slist_t key_list;
/* Exit status */
void *retval;
@ -53,6 +57,9 @@ struct posix_thread {
#define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS)
#define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS)
/* Passed to pthread_once */
#define PTHREAD_ONCE_INIT 1
/**
* @brief Declare a pthread condition variable
*
@ -480,6 +487,7 @@ int pthread_attr_getstack(const pthread_attr_t *attr,
void **stackaddr, size_t *stacksize);
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
size_t stacksize);
int pthread_once(pthread_once_t *once, void (*initFunc)(void));
void pthread_exit(void *retval);
int pthread_join(pthread_t thread, void **status);
int pthread_cancel(pthread_t pthread);
@ -503,5 +511,10 @@ int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_key_create(pthread_key_t *key,
void (*destructor)(void *));
int pthread_key_delete(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);
void *pthread_getspecific(pthread_key_t key);
#endif /* __PTHREAD_H__ */

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __POSIX_THREAD_KEY_H__
#define __POSIX_THREAD_KEY_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef CONFIG_PTHREAD_IPC
#include <misc/slist.h>
#include <zephyr/types.h>
typedef u32_t pthread_once_t;
/* pthread_key */
typedef void *pthread_key_t;
typedef struct pthread_key_obj {
/* List of pthread_key_data objects that contain thread
* specific data for the key
*/
sys_slist_t key_data_l;
/* Optional destructor that is passed to pthread_key_create() */
void (*destructor)(void *);
} pthread_key_obj;
typedef struct pthread_thread_data {
sys_snode_t node;
/* Key and thread specific data passed to pthread_setspecific() */
pthread_key_obj *key;
void *spec_data;
} pthread_thread_data;
typedef struct pthread_key_data {
sys_snode_t node;
pthread_thread_data thread_data;
} pthread_key_data;
#endif /* CONFIG_PTHREAD_IPC */
#ifdef __cplusplus
}
#endif
#endif /* __POSIX_THREAD_KEY_H__ */