Add a futex implementation for CloudABI.

Summary:
CloudABI provides two different types of futex objects: read-write locks
and condition variables. There is no need to provide separate support
for once objects and thread joining, as these are efficiently simulated
by blocking on a read-write lock. Mutexes simply use read-write locks.

Condition variables always have a lock object associated to them. They
always know to which lock a thread needs to be migrated if woken up.
This allows us to implement requeueing. A broadcast on a condition
variable will never cause multiple threads to be woken up at once. They
will be woken up iteratively.

This implementation still has lots of room for improvement. Locking is
coarse and right now we use linked lists to store all of the locks and
condition variables, instead of using a hash table. The primary goal of
this implementation was to behave correctly. Performance will be
improved as we go.

Test Plan:
This futex implementation has been in use for the last couple of months
and seems to work pretty well. All of the cloudlibc and libc++ unit
tests seem to pass.

Reviewers: dchagin, kib, vangyzen

Subscribers: imp

Differential Revision: https://reviews.freebsd.org/D3148
This commit is contained in:
Ed Schouten 2015-07-27 10:07:29 +00:00
parent 533c8a29da
commit af7e75f59d
4 changed files with 1238 additions and 20 deletions

View File

@ -80,6 +80,24 @@ cloudabi_convert_timespec(const struct timespec *in, cloudabi_timestamp_t *out)
return (0);
}
/* Fetches the time value of a clock. */
int
cloudabi_clock_time_get(struct thread *td, cloudabi_clockid_t clock_id,
cloudabi_timestamp_t *ret)
{
struct timespec ts;
int error;
clockid_t clockid;
error = cloudabi_convert_clockid(clock_id, &clockid);
if (error != 0)
return (error);
error = kern_clock_gettime(td, clockid, &ts);
if (error != 0)
return (error);
return (cloudabi_convert_timespec(&ts, ret));
}
int
cloudabi_sys_clock_res_get(struct thread *td,
struct cloudabi_sys_clock_res_get_args *uap)
@ -106,20 +124,10 @@ int
cloudabi_sys_clock_time_get(struct thread *td,
struct cloudabi_sys_clock_time_get_args *uap)
{
struct timespec ts;
cloudabi_timestamp_t cts;
cloudabi_timestamp_t ts;
int error;
clockid_t clockid;
error = cloudabi_convert_clockid(uap->clock_id, &clockid);
if (error != 0)
return (error);
error = kern_clock_gettime(td, clockid, &ts);
if (error != 0)
return (error);
error = cloudabi_convert_timespec(&ts, &cts);
if (error != 0)
return (error);
td->td_retval[0] = cts;
return (0);
error = cloudabi_clock_time_get(td, uap->clock_id, &ts);
td->td_retval[0] = ts;
return (error);
}

File diff suppressed because it is too large Load Diff

View File

@ -29,16 +29,30 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/syscallsubr.h>
#include <compat/cloudabi/cloudabi_proto.h>
#include <compat/cloudabi/cloudabi_syscalldefs.h>
int
cloudabi_sys_thread_exit(struct thread *td,
struct cloudabi_sys_thread_exit_args *uap)
{
struct cloudabi_sys_lock_unlock_args cloudabi_sys_lock_unlock_args = {
.lock = uap->lock,
.scope = uap->scope,
};
/* Not implemented. */
return (ENOSYS);
/* Wake up joining thread. */
cloudabi_sys_lock_unlock(td, &cloudabi_sys_lock_unlock_args);
/*
* Attempt to terminate the thread. Terminate the process if
* it's the last thread.
*/
kern_thr_exit(td);
exit1(td, 0, 0);
/* NOTREACHED */
}
int

View File

@ -30,12 +30,33 @@
#include <compat/cloudabi/cloudabi_syscalldefs.h>
struct thread;
struct timespec;
/* Fetches the time value of a clock. */
int cloudabi_clock_time_get(struct thread *, cloudabi_clockid_t,
cloudabi_timestamp_t *);
/* Converts a FreeBSD errno to a CloudABI errno. */
cloudabi_errno_t cloudabi_convert_errno(int);
/* Converts a struct timespec to a CloudABI timestamp. */
int cloudabi_convert_timespec(const struct timespec *, cloudabi_timestamp_t *);
/*
* Blocking futex functions.
*
* These functions are called by CloudABI's polling system calls to
* sleep on a lock or condition variable.
*/
int cloudabi_futex_condvar_wait(struct thread *, cloudabi_condvar_t *,
cloudabi_mflags_t, cloudabi_lock_t *, cloudabi_mflags_t, cloudabi_clockid_t,
cloudabi_timestamp_t, cloudabi_timestamp_t);
int cloudabi_futex_lock_rdlock(struct thread *, cloudabi_lock_t *,
cloudabi_mflags_t, cloudabi_clockid_t, cloudabi_timestamp_t,
cloudabi_timestamp_t);
int cloudabi_futex_lock_wrlock(struct thread *, cloudabi_lock_t *,
cloudabi_mflags_t, cloudabi_clockid_t, cloudabi_timestamp_t,
cloudabi_timestamp_t);
#endif