Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of pthread #107

Merged
merged 18 commits into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions user/apps/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# found in the LICENSE file.

user-apps-dirs = \
l4test \
pingpong
posixtest \

ifdef CONFIG_BOARD_STM32F429DISCOVERY
user-apps-dirs += \
Expand Down
2 changes: 1 addition & 1 deletion user/apps/l4test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "l4test.h"
#include "assert.h"

#define STACK_SIZE 512
#define STACK_SIZE 256

/* where to start allocating RAM */
__USER_BSS static char *free_page;
Expand Down
6 changes: 6 additions & 0 deletions user/apps/posixtest/build.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) 2013 The F9 Microkernel Project. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

user-apps-posixtest-y = \
main.o \
69 changes: 69 additions & 0 deletions user/apps/posixtest/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <user_runtime.h>
#include <platform/link.h>
#include <l4/utcb.h>
#include <l4/ipc.h>
#include <l4io.h>

/* posix layer */
#include <libposix/pthread.h>

#define STACK_SIZE 512

__USER_DATA pthread_mutex_t mutex;

__USER_DATA int shared = 0;

__USER_TEXT void *child_thread2(void *args)
{
printf("child task 2 start\n");

while(1) {
shared = shared + 1;
printf("task 2: %d\n", shared);
L4_Sleep(L4_TimePeriod(1000));
}

return 0;
}

__USER_TEXT void *child_thread1(void *args)
{
printf("child task 1 start\n");
pthread_create(NULL, NULL, child_thread2, NULL);

for(int i = 0; i <= 10; i++) {
printf("%d\n", 10 - i);
L4_Sleep(L4_TimePeriod(500));
}

for(int i = 0; i <= 10; i++) {
printf("%d\n", 10 - i);
L4_Sleep(L4_TimePeriod(500));
}

while(1) {
L4_Sleep(L4_Never);
}

return 0;
}

static __USER_TEXT void *main(void *user)
{
printf("\nPosix Layer test starts\n");
mutex = 0;
pthread_create(NULL, NULL, child_thread1, NULL);

while(1)
L4_Sleep(L4_Never);

return NULL;
}

DECLARE_USER(
255,
posixtest,
main,
DECLARE_FPAGE(0x0, 4 * (UTCB_SIZE + STACK_SIZE))
DECLARE_FPAGE(0x0, 512)
);
6 changes: 6 additions & 0 deletions user/include/libposix/libposix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __LIBPOSIX_H__
#define __LIBPOSIX_H__

int fork(void);

#endif
21 changes: 21 additions & 0 deletions user/include/libposix/pthread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __PTHREAD_H__
#define __PTHREAD_H__

#include <libposix/sys/types.h>

int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
int pthread_detach(pthread_t thread);
void pthread_exit(void *value_ptr);
int pthread_join(pthread_t thread, void **value_ptr);
int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);

#endif
17 changes: 17 additions & 0 deletions user/include/libposix/sys/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef __TYPES_H_
#define __TYPES_H_

#include <stdint.h>

/* FIXME: This should be moved to time.h */
struct timespec {
uint64_t nsec;
};

/* FIXME: Define proper type for pthread type */
typedef uint32_t pthread_mutex_t;
typedef uint32_t pthread_mutexattr_t;
typedef uint32_t pthread_t;
typedef uint32_t pthread_attr_t;

#endif
18 changes: 18 additions & 0 deletions user/include/libposix/unimplemented.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __UNIMPLEMENTED_H__
#define __UNIMPLEMENTED_H__

#define ESC "\e["
#define LIGHT_RED ESC "31;1m"
#define BLACK ESC "0m"

#include <l4io.h>

#define UNIMPLEMENTED() \
do { \
printf("%s", LIGHT_RED); \
printf("\nUnimplemented: %s, at %s:%d", \
__func__, __FILE__, __LINE__); \
printf("%s\n", BLACK); \
} while (0) \

#endif
1 change: 1 addition & 0 deletions user/include/user_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define USER_RUNTIME_H

#include <l4/types.h>
#include <l4/ipc.h>

typedef struct user_struct user_struct;
typedef void (*user_entry)(user_struct *);
Expand Down
1 change: 1 addition & 0 deletions user/lib/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
user-lib-dirs = \
l4 \
io \
libposix \
7 changes: 7 additions & 0 deletions user/lib/libposix/build.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2013 The F9 Microkernel Project. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

user-lib-libposix-y = \
fork.o \
pthread.o
9 changes: 9 additions & 0 deletions user/lib/libposix/fork.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <libposix/libposix.h>
#include <libposix/unimplemented.h>
#include <platform/link.h>

int __USER_TEXT fork()
{
UNIMPLEMENTED();
return 0;
}
83 changes: 83 additions & 0 deletions user/lib/libposix/pthread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <libposix/pthread.h>
#include <l4/ipc.h>
#include <l4/utcb.h>
#include <l4/pager.h>
#include <l4io.h>
#include <platform/link.h>

int __USER_TEXT pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg)
{
L4_ThreadId_t tid = pager_create_thread();

pager_start_thread(tid, start_routine, NULL);

return *(int *)&tid;
}

__USER_TEXT int pthread_detach(pthread_t thread)
{
return 0;
}

__USER_TEXT void pthread_exit(void *value_ptr)
{
return;
}

__USER_TEXT int pthread_join(pthread_t thread, void **value_ptr)
{
return 0;
}

__USER_TEXT int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *restrict attr)
{
return 0;
}

__USER_TEXT int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
return 0;
}

__USER_TEXT int pthread_mutex_lock(pthread_mutex_t *mutex)
{
/* Busy trying */
while(pthread_mutex_trylock(mutex));

return 0;
}

__USER_TEXT int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
register int result = 1;

__asm__ __volatile__(
"mov r1, #1\n"
"mov r2, %[mutex]\n"
"ldrex r0, [r2]\n" /* Load value [r2] */
"cmp r0, #0\n" /* Checking is word set to 1 */

"itt eq\n"
"strexeq r0, r1, [r2]\n"
"moveq %[result], r0\n"
: [result] "=r"(result)
: [mutex] "r"(mutex)
: "r0", "r1", "r2");

return result;
}

__USER_TEXT int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
*mutex = 0;
return 0;
}

__USER_TEXT int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime)
{
return 0;
}
1 change: 1 addition & 0 deletions user/root_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <l4/kip.h>
#include <l4/utcb.h>
#include <l4/ipc.h>
#include <l4io.h>
#include <types.h>
#include <user_runtime.h>

Expand Down