Merge pull request #835 from RaduNichita/pthread

Add POSIX Like functions for pthread_create and pthread_join
This commit is contained in:
johnjiang 2024-08-27 16:19:02 +08:00 committed by GitHub
commit 30867396f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 0 deletions

View File

@ -262,6 +262,7 @@ endif
FF_HOST_SRCS+= \ FF_HOST_SRCS+= \
ff_host_interface.c \ ff_host_interface.c \
ff_thread.c \
ff_config.c \ ff_config.c \
ff_ini_parser.c \ ff_ini_parser.c \
ff_dpdk_if.c \ ff_dpdk_if.c \

View File

@ -147,6 +147,10 @@ int ff_gettimeofday(struct timeval *tv, struct timezone *tz);
int ff_dup(int oldfd); int ff_dup(int oldfd);
int ff_dup2(int oldfd, int newfd); int ff_dup2(int oldfd, int newfd);
int ff_pthread_create(pthread_t * thread, const pthread_attr_t * attr,
void * (* start_routine) (void *), void * arg);
int ff_pthread_join(pthread_t thread, void **retval);
/* POSIX-LIKE api end */ /* POSIX-LIKE api end */

View File

@ -58,3 +58,5 @@ ff_zc_mbuf_get
ff_zc_mbuf_write ff_zc_mbuf_write
ff_zc_mbuf_read ff_zc_mbuf_read
ff_get_traffic ff_get_traffic
ff_pthread_create
ff_pthread_join

51
lib/ff_thread.c Normal file
View File

@ -0,0 +1,51 @@
#include <pthread.h>
#include <errno.h>
#include "ff_api.h"
#include "ff_host_interface.h"
extern __thread struct thread *pcurthread;
struct thread_data {
void * (* start_routine) (void *);
void * arg;
struct thread *parent;
};
static void
ff_set_thread(struct thread *other) {
pcurthread = other;
}
static
void* ff_start_routine(void * data) {
struct thread_data *p_data = (struct thread_data *) data;
void * (* start_routine) (void *) = p_data->start_routine;
void *arg = p_data->arg;
ff_set_thread(p_data->parent);
ff_free(data);
start_routine(arg);
return NULL;
}
int
ff_pthread_create(pthread_t *thread, const pthread_attr_t * attr, void * (* start_routine) (void *), void * arg) {
struct thread_data *data;
data = ff_malloc(sizeof(struct thread_data));
if (!data) {
errno = ENOMEM;
return -ff_ENOMEM;
}
data->start_routine = start_routine;
data->arg = arg;
data->parent = pcurthread;
return pthread_create(thread, attr, ff_start_routine, data);
}
int
ff_pthread_join(pthread_t thread, void **retval) {
return pthread_join(thread, retval);
}