Skip to content

Commit

Permalink
implement some more posix apis
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Feb 27, 2022
1 parent f0b1548 commit b877bed
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
36 changes: 36 additions & 0 deletions include/re_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,47 @@ typedef int (*thread_h)(void *);
#define THREAD_ONCE_FLAG_INIT 0
typedef int thread_once_flag;

/**
* Creates a new thread
*
* @param thr Pointer to new thread
* @param func Function to execute
* @param arg Argument to pass to the function
*
* @return 0 if success, otherwise errorcode
*/
int thread_create(thread *thr, thread_h func, void *arg);

/**
* Checks whether lhs and rhs refer to the same thread.
*
* @return Non-zero value if lhs and rhs refer to the same value, 0 otherwise.
*/
int thread_equal(thread lhs, thread rhs);

/**
* @return the identifier of the calling thread.
*/
thread thread_current(void);

/**
* Detaches the thread identified by thr from the current environment.
*
* @return 0 if success, otherwise errorcode
*/
int thread_detach(thread thr);

/**
* Blocks the current thread until the thread identified by thr finishes
* execution
*
* @param thr Thread
* @param res Result code location
*
* @return 0 if success, otherwise errorcode
*/
int thread_join(thread thr, int *res);

int thread_once(thread_once_flag *flag, void (*func)(void));
void thread_exit(int res);

Expand Down
43 changes: 33 additions & 10 deletions src/thread/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ static void *thread_handler(void *p)
}


/**
* Create thread
*
* @param thr Thread
* @param func Function
* @param arg ARG
*
* @return 0 if success, otherwise errorcode
*/
int thread_create(thread *thr, thread_h func, void *arg)
{
struct th *th;
Expand All @@ -50,11 +41,43 @@ int thread_create(thread *thr, thread_h func, void *arg)
return ENOMEM;

th->func = func;
th->arg = arg;
th->arg = arg;

err = pthread_create(thr, NULL, thread_handler, th);
if (err)
mem_deref(th);

return err;
}


int thread_equal(thread lhs, thread rhs)
{
return pthread_equal(lhs, rhs);
}


thread thread_current(void)
{
return pthread_self();
}


int thread_detach(thread thr)
{
return pthread_detach(thr);
}


int thread_join(thread thr, int *res)
{
void *code;
int err;

err = pthread_join(thr, &code);

if (res)
*res = (int)(intptr_t)code;

return err;
}

0 comments on commit b877bed

Please sign in to comment.