Skip to content

Commit

Permalink
Merge pull request torvalds#125 from pscollins/return-pthread-tid
Browse files Browse the repository at this point in the history
lkl: Return thread id from thread_create host op
  • Loading branch information
Octavian Purdila committed Mar 17, 2016
2 parents a4349bf + 0b78db8 commit 2f10dec
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
5 changes: 3 additions & 2 deletions arch/lkl/include/uapi/asm/host_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* Defined in {posix,nt}-host.c */
struct lkl_mutex_t;
struct lkl_sem_t;
typedef unsigned long lkl_thread_t;

/**
* lkl_host_operations - host operations used by the Linux kernel
Expand Down Expand Up @@ -31,7 +32,7 @@ struct lkl_sem_t;
* @mutex_unlock - release the mutex
*
* @thread_create - create a new thread and run f(arg) in its context; returns a
* thread handle or NULL if the thread could not be created
* thread handle or 0 if the thread could not be created
* @thread_exit - terminates the current thread
*
* @tls_alloc - allocate a thread local storage key; returns 0 if succesful
Expand Down Expand Up @@ -74,7 +75,7 @@ struct lkl_host_operations {
void (*mutex_lock)(struct lkl_mutex_t *mutex);
void (*mutex_unlock)(struct lkl_mutex_t *mutex);

int (*thread_create)(void (*f)(void *), void *arg);
lkl_thread_t (*thread_create)(void (*f)(void *), void *arg);
void (*thread_exit)(void);

int (*tls_alloc)(unsigned int *key);
Expand Down
2 changes: 1 addition & 1 deletion arch/lkl/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int __init lkl_start_kernel(struct lkl_host_operations *ops,
}

ret = lkl_ops->thread_create(lkl_run_kernel, NULL);
if (ret) {
if (!ret) {
ret = -ENOMEM;
goto out_free_idle_sem;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/lkl/kernel/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ int copy_thread(unsigned long clone_flags, unsigned long esp,
tba->ti = ti;

ret = lkl_ops->thread_create(thread_bootstrap, tba);
if (ret) {
if (!ret) {
kfree(tba);
return -ENOMEM;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/lkl/lib/nt-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ static void mutex_free(struct lkl_mutex_t *_mutex)
free(_mutex);
}

static int thread_create(void (*fn)(void *), void *arg)
static lkl_thread_t thread_create(void (*fn)(void *), void *arg)
{
DWORD WINAPI (*win_fn)(LPVOID arg) = (DWORD WINAPI (*)(LPVOID))fn;

return CreateThread(NULL, 0, win_fn, arg, 0, NULL) ? 0 : -1;
return CreateThread(NULL, 0, win_fn, arg, 0, NULL);
}

static void thread_exit(void)
Expand Down
24 changes: 15 additions & 9 deletions tools/lkl/lib/posix-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ struct lkl_sem_t {
lkl_printf("%s: %s\n", #exp, strerror(errno)); \
} while (0)

/* pthread_* functions use the reverse convention */
#define WARN_PTHREAD(exp) do { \
int __ret = exp; \
if (__ret > 0) \
lkl_printf("%s: %s\n", #exp, strerror(__ret)); \
} while (0)
static int _warn_pthread(int ret, char *str_exp)
{
if (ret > 0)
lkl_printf("%s: %s\n", str_exp, strerror(ret));

return ret;
}


/* pthread_* functions use the reverse convention */
#define WARN_PTHREAD(exp) _warn_pthread(exp, #exp)

static struct lkl_sem_t *sem_alloc(int count)
{
Expand Down Expand Up @@ -176,11 +180,13 @@ static void mutex_free(struct lkl_mutex_t *_mutex)
free(_mutex);
}

static int thread_create(void (*fn)(void *), void *arg)
static lkl_thread_t thread_create(void (*fn)(void *), void *arg)
{
pthread_t thread;

return pthread_create(&thread, NULL, (void* (*)(void *))fn, arg);
if (WARN_PTHREAD(pthread_create(&thread, NULL, (void* (*)(void *))fn, arg)))
return 0;
else
return (lkl_thread_t) thread;
}

static void thread_exit(void)
Expand Down
4 changes: 2 additions & 2 deletions tools/lkl/lib/virtio_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ int lkl_netdev_add(struct lkl_netdev *nd, void *mac)
if (ret)
goto out_free;

if (lkl_host_ops.thread_create(poll_thread, &dev->rx_poll) < 0)
if (lkl_host_ops.thread_create(poll_thread, &dev->rx_poll) == 0)
goto out_cleanup_dev;

if (lkl_host_ops.thread_create(poll_thread, &dev->tx_poll) < 0)
if (lkl_host_ops.thread_create(poll_thread, &dev->tx_poll) == 0)
goto out_cleanup_dev;

/* RX/TX thread polls will exit when the host netdev handle is closed */
Expand Down
2 changes: 1 addition & 1 deletion tools/lkl/tests/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ static int test_syscall_thread(char *str, int len)
}

ret = lkl_host_ops.thread_create(test_thread, pipe_fds);
if (ret) {
if (!ret) {
snprintf(str, len, "failed to create thread");
return TEST_FAILURE;
}
Expand Down

0 comments on commit 2f10dec

Please sign in to comment.