diff --git a/include/errno.h b/include/errno.h index e82a39b..691236a 100644 --- a/include/errno.h +++ b/include/errno.h @@ -17,6 +17,7 @@ #define EALREADY 0x1BAD000D #define EINPROGRESS 0x1BAD000E #define ENFILE 0x1BAD000F +#define ETIMEDOUT 0x1BAD0010 #define EAFNOSUPPORT 0x1BAD0020 #define ENOPROTOOPT 0x1BAD0021 diff --git a/include/pthread.h b/include/pthread.h index 59e9825..0fe8afb 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -2,6 +2,8 @@ #ifndef __PTHREAD_H__ #define __PTHREAD_H__ +#include + #define PTHREAD_MUTEX_INITIALIZER NULL #define PTHREAD_COND_INITIALIZER NULL @@ -53,6 +55,8 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex); int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); int pthread_cond_destroy(pthread_cond_t *cond); int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); +int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + const struct timespec *abstime); int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); diff --git a/include/time.h b/include/time.h index ca79696..8491da0 100644 --- a/include/time.h +++ b/include/time.h @@ -2,6 +2,8 @@ #ifndef __TIME_H__ #define __TIME_H__ +#include + struct tm { int tm_sec; int tm_min; diff --git a/lib/libc/posix/pthread.c b/lib/libc/posix/pthread.c index 82a575d..af2eac8 100644 --- a/lib/libc/posix/pthread.c +++ b/lib/libc/posix/pthread.c @@ -373,6 +373,40 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) return 0; } +int +pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + const struct timespec *abstime) +{ + struct pthread_cond *cnd; + uint64_t level; + + if (*cond == NULL) { + int status = pthread_cond_init(cond, NULL); + if (status != 0) + return status; + } + cnd = *cond; + + if (mutex) + pthread_mutex_unlock(mutex); + + CoreMutex_Lock(&cnd->mtx); + level = cnd->enter; + cnd->enter++; + CoreMutex_Unlock(&cnd->mtx); + + while (level >= cnd->exit) { + OSThreadSleep(0); + + // XXX: check time + } + + if (mutex) + pthread_mutex_lock(mutex); + + return 0; +} + int pthread_cond_signal(pthread_cond_t *cond) diff --git a/sys/include/timespec.h b/sys/include/timespec.h new file mode 100644 index 0000000..c02110b --- /dev/null +++ b/sys/include/timespec.h @@ -0,0 +1,13 @@ + +#ifndef __SYS_TIMESPEC_H__ +#define __SYS_TIMESPEC_H__ + +#include + +struct timespec { + time_t tv_sec; + long tv_nsec; +}; + +#endif /* __SYS_TIMESPEC_H__ */ +