kernel: POSIX: Compatibility layer for pthread APIs.

This patch provides pthread APIs for POSIX 1003.1 PSE52 standard.

Signed-off-by: Youvedeep Singh <youvedeep.singh@intel.com>
This commit is contained in:
Youvedeep Singh 2017-12-28 14:31:57 +05:30 committed by Anas Nashif
commit c8aa6570c1
8 changed files with 731 additions and 6 deletions

View file

@ -7,6 +7,7 @@
#ifndef __PTHREAD_H__
#define __PTHREAD_H__
#include <kernel.h>
#ifdef CONFIG_NEWLIB_LIBC
#include <time.h>
#else
@ -20,6 +21,46 @@ struct timespec {
#endif /* CONFIG_NEWLIB_LIBC */
#include "sys/types.h"
#include "sched.h"
enum pthread_state {
/* The thread is running and joinable. */
PTHREAD_JOINABLE = 0,
/* The thread is running and detached. */
PTHREAD_DETACHED,
/* A joinable thread exited and its return code is available. */
PTHREAD_EXITED,
/* The thread structure is unallocated and available for reuse. */
PTHREAD_TERMINATED
};
struct posix_thread {
struct k_thread thread;
/* Exit status */
void *retval;
/* Pthread cancellation */
int cancel_state;
int cancel_pending;
struct k_sem cancel_lock_sem;
pthread_mutex_t cancel_lock;
/* Pthread State */
enum pthread_state state;
pthread_mutex_t state_lock;
struct k_sem state_lock_sem;
pthread_cond_t state_cond;
};
/* Pthread detach/joinable */
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1
/* Pthread cancellation */
#define _PTHREAD_CANCEL_POS 0
#define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS)
#define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS)
static inline s32_t _ts_to_ms(const struct timespec *to)
{
@ -359,4 +400,57 @@ int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *);
int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
*/
/* Base Pthread related APIs */
/**
* @brief Obtain ID of the calling thread.
*
* The results of calling this API from threads not created with
* pthread_create() are undefined.
*
* See IEEE 1003.1
*/
static inline pthread_t pthread_self(void)
{
return (pthread_t)k_current_get();
}
/**
* @brief Compare thread IDs.
*
* See IEEE 1003.1
*/
static inline int pthread_equal(pthread_t pt1, pthread_t pt2)
{
return (pt1 == pt2);
}
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_getschedparam(const pthread_attr_t *attr,
struct sched_param *schedparam);
int pthread_getschedparam(pthread_t pthread, int *policy,
struct sched_param *param);
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);
void pthread_exit(void *retval);
int pthread_join(pthread_t thread, void **status);
int pthread_cancel(pthread_t pthread);
int pthread_detach(pthread_t thread);
int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
void *(*threadroutine)(void *), void *arg);
int pthread_setcancelstate(int state, int *oldstate);
int pthread_attr_setschedparam(pthread_attr_t *attr,
const struct sched_param *schedparam);
int pthread_setschedparam(pthread_t pthread, int policy,
const struct sched_param *param);
#endif /* __PTHREAD_H__ */

33
include/posix/sched.h Normal file
View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __POSIX_SCHED_H__
#define __POSIX_SCHED_H__
#ifdef __cplusplus
extern "C" {
#endif
#include_next <sched.h>
/* Cooperative scheduling policy */
#ifndef SCHED_FIFO
#define SCHED_FIFO 0
#endif /* SCHED_FIFO */
/* Priority based prempetive scheduling policy */
#ifndef SCHED_RR
#define SCHED_RR 1
#endif /* SCHED_RR */
struct sched_param {
int priority; /* Thread execution priority */
};
#ifdef __cplusplus
}
#endif
#endif /* __POSIX_SCHED_H__ */

View file

@ -12,11 +12,24 @@
extern "C" {
#endif
#ifdef CONFIG_NEWLIB_LIBC
#include_next <sys/types.h>
#endif /* CONFIG_NEWLIB_LIBC */
#ifdef CONFIG_PTHREAD_IPC
/* Thread attributes */
typedef struct pthread_attr_t {
int priority;
void *stack;
size_t stacksize;
u32_t flags;
u32_t delayedstart;
u32_t schedpolicy;
s32_t detachstate;
u32_t initialized;
} pthread_attr_t;
typedef void *pthread_t;
/* Mutex */
typedef struct pthread_mutex {
struct k_sem *sem;
@ -26,7 +39,7 @@ typedef struct pthread_mutexattr {
int unused;
} pthread_mutexattr_t;
/* Confition Variables */
/* Condition variables */
typedef struct pthread_cond {
_wait_q_t wait_q;
} pthread_cond_t;