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

implement mutex/condvar attributes #365

Merged
merged 3 commits into from
Jul 26, 2024
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
8 changes: 4 additions & 4 deletions include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ extern "C" {

#define PTHREAD_MUTEX_INITIALIZER { 0, 0 }

#define PTHREAD_MUTEX_DEFAULT 0
#define PTHREAD_MUTEX_ERRORCHECK 1
#define PTHREAD_MUTEX_NORMAL 2
#define PTHREAD_MUTEX_RECURSIVE 3
#define PTHREAD_MUTEX_NORMAL PH_LOCK_NORMAL
#define PTHREAD_MUTEX_ERRORCHECK PH_LOCK_ERRORCHECK
#define PTHREAD_MUTEX_RECURSIVE PH_LOCK_RECURSIVE
#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL

#define PTHREAD_ONCE_INIT 1

Expand Down
21 changes: 20 additions & 1 deletion include/sys/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#include <stddef.h>
#include <phoenix/sysinfo.h>
#include <phoenix/signal.h>

#include <phoenix/threads.h>

Check failure on line 24 in include/sys/threads.h

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a7-imx6ull-evk)

phoenix/threads.h: No such file or directory

Check failure on line 24 in include/sys/threads.h

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a9-zynq7000-qemu)

phoenix/threads.h: No such file or directory

Check failure on line 24 in include/sys/threads.h

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a9-zynq7000-zedboard)

phoenix/threads.h: No such file or directory

Check failure on line 24 in include/sys/threads.h

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a9-zynq7000-zturn)

phoenix/threads.h: No such file or directory

Check failure on line 24 in include/sys/threads.h

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7m7-imxrt105x-evk)

phoenix/threads.h: No such file or directory
#include <phoenix/time.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -88,9 +89,18 @@
extern int priority(int priority);


extern int phMutexCreate(handle_t *h, const struct lockAttr *attr);


extern int mutexCreate(handle_t *h);


static inline int mutexCreateWithAttr(handle_t *h, const struct lockAttr *attr)
{
return phMutexCreate(h, attr);
}


extern int phMutexLock(handle_t h);


Expand Down Expand Up @@ -118,9 +128,18 @@
extern int semaphoreDone(semaphore_t *s);


extern int phCondCreate(handle_t *h, const struct condAttr *attr);


extern int condCreate(handle_t *h);


static inline int condCreateWithAttr(handle_t *h, const struct condAttr *attr)
{
return phCondCreate(h, attr);
}


extern int condWait(handle_t h, handle_t m, time_t timeout);


Expand Down
5 changes: 1 addition & 4 deletions include/sys/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,12 @@ typedef struct {
} pthread_mutex_t;


typedef struct pthread_mutexattr_t {
int type;
} pthread_mutexattr_t;
typedef struct lockAttr pthread_mutexattr_t;


typedef struct {
handle_t condh;
volatile int initialized;
clockid_t clock_id;
} pthread_cond_t;


Expand Down
83 changes: 59 additions & 24 deletions pthread/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,10 +716,13 @@ static int pthread_mutex_lazy_init(pthread_mutex_t *mutex)
int pthread_mutex_init(pthread_mutex_t *__restrict mutex, const pthread_mutexattr_t *__restrict attr)
{
int err;
if (attr == NULL) {
err = mutexCreate(&mutex->mutexh);
}
else {
err = mutexCreateWithAttr(&mutex->mutexh, attr);
}

(void)attr;

err = mutexCreate(&mutex->mutexh);
if (err == 0) {
mutex->initialized = 1;
}
Expand Down Expand Up @@ -804,7 +807,11 @@ int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
{
int err = EINVAL;

if (type >= PTHREAD_MUTEX_DEFAULT && type <= PTHREAD_MUTEX_RECURSIVE) {
/* clang-format off */
if ((type == PTHREAD_MUTEX_NORMAL) || (type == PTHREAD_MUTEX_ERRORCHECK) ||
(type == PTHREAD_MUTEX_RECURSIVE) || (type == PTHREAD_MUTEX_DEFAULT)) {
/* clang-format on */

attr->type = type;
err = 0;
}
Expand Down Expand Up @@ -900,15 +907,38 @@ int pthread_condattr_getclock(const pthread_condattr_t *__restrict attr, clockid
}


static int pthread_condattr_to_condAttr(const pthread_condattr_t *pattr, struct condAttr *attr)
{
int ret = 0;
switch (pattr->clock_id) {
case CLOCK_REALTIME:
attr->clock = PH_CLOCK_REALTIME;
break;

case CLOCK_MONOTONIC:
case CLOCK_MONOTONIC_RAW:
attr->clock = PH_CLOCK_MONOTONIC;
break;

default:
ret = EINVAL;
break;
}
return ret;
}


static int pthread_cond_lazy_init(pthread_cond_t *cond)
{
struct condAttr cattr;
int err = 0;

if (cond->initialized == 0) {
mutexLock(pthread_common.mutex_cond_init_lock);
if (cond->initialized == 0) {
cond->clock_id = PTHREAD_COND_CLOCK_DEFAULT;
err = condCreate(&cond->condh);
(void)pthread_condattr_to_condAttr(&(pthread_condattr_t) { .clock_id = PTHREAD_COND_CLOCK_DEFAULT }, &cattr);

err = condCreateWithAttr(&cond->condh, &cattr);
if (err == 0) {
cond->initialized = 1;
}
Expand All @@ -921,11 +951,18 @@ static int pthread_cond_lazy_init(pthread_cond_t *cond)

int pthread_cond_init(pthread_cond_t *__restrict cond, const pthread_condattr_t *__restrict attr)
{
int err;
struct condAttr cattr;

cond->clock_id = (attr == NULL) ? PTHREAD_COND_CLOCK_DEFAULT : attr->clock_id;
if (attr != NULL) {
if (pthread_condattr_to_condAttr(attr, &cattr) != 0) {
return EINVAL;
}
}
else {
(void)pthread_condattr_to_condAttr(&(pthread_condattr_t) { .clock_id = PTHREAD_COND_CLOCK_DEFAULT }, &cattr);
}

err = condCreate(&cond->condh);
int err = condCreateWithAttr(&cond->condh, &cattr);
if (err == 0) {
cond->initialized = 1;
}
Expand Down Expand Up @@ -996,26 +1033,24 @@ int pthread_cond_timedwait(pthread_cond_t *__restrict cond,
const struct timespec *__restrict abstime)
{
int err = 0;
struct timespec now;
clock_gettime(cond->clock_id, &now);
if ((now.tv_sec > abstime->tv_sec) || ((now.tv_sec == abstime->tv_sec) && (now.tv_nsec >= abstime->tv_nsec))) {
err = -ETIMEDOUT;

const time_t abstime_us = timespec_to_us(abstime);

/* check timeout as condWait with timeout 0 will wait indefinitely */
if (abstime_us == 0) {
lukileczo marked this conversation as resolved.
Show resolved Hide resolved
return ETIMEDOUT;
}
else {
time_t now_us = timespec_to_us(&now);
const time_t abstime_us = timespec_to_us(abstime);
time_t timeout_us = abstime_us - now_us;

err = pthread_cond_lazy_init(cond);
err = pthread_cond_lazy_init(cond);

if (err == 0) {
err = pthread_mutex_lazy_init(mutex);
}
if (err == 0) {
err = pthread_mutex_lazy_init(mutex);
}

if (err == 0) {
err = condWait(cond->condh, mutex->mutexh, timeout_us);
}
if (err == 0) {
err = condWait(cond->condh, mutex->mutexh, abstime_us);
}

if (err == -ETIME) {
err = -ETIMEDOUT;
}
Expand Down
16 changes: 16 additions & 0 deletions sys/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
#include <errno.h>


int mutexCreate(handle_t *h)
{
static const struct lockAttr defaultAttr = { .type = PH_LOCK_NORMAL };

return phMutexCreate(h, &defaultAttr);
}


int mutexLock(handle_t m)
{
int err;
Expand All @@ -25,6 +33,14 @@ int mutexLock(handle_t m)
}


int condCreate(handle_t *h)
{
static const struct condAttr defaultAttr = { .clock = PH_CLOCK_RELATIVE };

return phCondCreate(h, &defaultAttr);
}


int condWait(handle_t h, handle_t m, time_t timeout)
{
int err, mut_err;
Expand Down
Loading