2013-04-30 16:39:20 +04:00
|
|
|
/* $NetBSD: rumpuser_pth.c,v 1.20 2013/04/30 12:39:20 pooka Exp $ */
|
2007-10-31 18:57:19 +03:00
|
|
|
|
|
|
|
/*
|
2010-02-26 21:54:20 +03:00
|
|
|
* Copyright (c) 2007-2010 Antti Kantee. All Rights Reserved.
|
2007-10-31 18:57:19 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-07-27 13:09:05 +04:00
|
|
|
#include "rumpuser_port.h"
|
|
|
|
|
2008-12-18 03:21:52 +03:00
|
|
|
#if !defined(lint)
|
2013-04-30 16:39:20 +04:00
|
|
|
__RCSID("$NetBSD: rumpuser_pth.c,v 1.20 2013/04/30 12:39:20 pooka Exp $");
|
2008-12-18 03:21:52 +03:00
|
|
|
#endif /* !lint */
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
#include <assert.h>
|
2007-11-17 23:50:18 +03:00
|
|
|
#include <errno.h>
|
2012-07-27 13:09:05 +04:00
|
|
|
#include <fcntl.h>
|
2007-10-31 18:57:19 +03:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2008-03-11 13:50:16 +03:00
|
|
|
#include <string.h>
|
2008-07-01 16:33:32 +04:00
|
|
|
#include <stdint.h>
|
2009-04-27 18:28:58 +04:00
|
|
|
#include <unistd.h>
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2008-10-09 05:19:06 +04:00
|
|
|
#include <rump/rumpuser.h>
|
|
|
|
|
|
|
|
#include "rumpuser_int.h"
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2007-11-07 18:41:18 +03:00
|
|
|
static pthread_key_t curlwpkey;
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2007-11-07 21:59:18 +03:00
|
|
|
struct rumpuser_mtx {
|
|
|
|
pthread_mutex_t pthmtx;
|
2010-12-01 17:59:37 +03:00
|
|
|
struct lwp *owner;
|
2013-04-27 17:59:46 +04:00
|
|
|
int flags;
|
2007-11-07 21:59:18 +03:00
|
|
|
};
|
|
|
|
|
2010-12-01 17:59:37 +03:00
|
|
|
#define RURW_AMWRITER(rw) (rw->writer == rumpuser_get_curlwp() \
|
2009-01-26 22:34:12 +03:00
|
|
|
&& rw->readers == -1)
|
|
|
|
#define RURW_HASREAD(rw) (rw->readers > 0)
|
|
|
|
|
2009-01-11 01:28:42 +03:00
|
|
|
#define RURW_SETWRITE(rw) \
|
|
|
|
do { \
|
2009-01-26 22:34:12 +03:00
|
|
|
assert(rw->readers == 0); \
|
2010-12-01 17:59:37 +03:00
|
|
|
rw->writer = rumpuser_get_curlwp(); \
|
2009-01-26 22:34:12 +03:00
|
|
|
rw->readers = -1; \
|
2009-01-11 01:28:42 +03:00
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
#define RURW_CLRWRITE(rw) \
|
|
|
|
do { \
|
2009-01-26 22:34:12 +03:00
|
|
|
assert(rw->readers == -1 && RURW_AMWRITER(rw)); \
|
|
|
|
rw->readers = 0; \
|
2009-01-11 01:28:42 +03:00
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
#define RURW_INCREAD(rw) \
|
|
|
|
do { \
|
|
|
|
pthread_spin_lock(&rw->spin); \
|
2009-01-26 22:34:12 +03:00
|
|
|
assert(rw->readers >= 0); \
|
|
|
|
++(rw)->readers; \
|
2009-01-11 01:28:42 +03:00
|
|
|
pthread_spin_unlock(&rw->spin); \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
#define RURW_DECREAD(rw) \
|
|
|
|
do { \
|
|
|
|
pthread_spin_lock(&rw->spin); \
|
2009-01-26 22:34:12 +03:00
|
|
|
assert(rw->readers > 0); \
|
|
|
|
--(rw)->readers; \
|
2009-01-11 01:28:42 +03:00
|
|
|
pthread_spin_unlock(&rw->spin); \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
2007-11-07 18:41:18 +03:00
|
|
|
struct rumpuser_rw {
|
|
|
|
pthread_rwlock_t pthrw;
|
2009-01-11 01:28:42 +03:00
|
|
|
pthread_spinlock_t spin;
|
2009-01-26 22:34:12 +03:00
|
|
|
int readers;
|
2010-12-01 17:59:37 +03:00
|
|
|
struct lwp *writer;
|
2007-11-07 18:41:18 +03:00
|
|
|
};
|
|
|
|
|
2007-11-07 21:59:18 +03:00
|
|
|
struct rumpuser_cv {
|
|
|
|
pthread_cond_t pthcv;
|
2009-01-11 01:28:42 +03:00
|
|
|
int nwaiters;
|
2007-11-07 21:59:18 +03:00
|
|
|
};
|
|
|
|
|
2013-04-27 18:59:08 +04:00
|
|
|
void
|
|
|
|
rumpuser__thrinit(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
pthread_key_create(&curlwpkey, NULL);
|
|
|
|
}
|
2009-01-08 01:50:08 +03:00
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
int
|
2010-06-01 03:09:29 +04:00
|
|
|
rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname,
|
|
|
|
int joinable, void **ptcookie)
|
2007-10-31 18:57:19 +03:00
|
|
|
{
|
|
|
|
pthread_t ptid;
|
2010-06-01 03:09:29 +04:00
|
|
|
pthread_t *ptidp;
|
|
|
|
pthread_attr_t pattr;
|
2008-12-17 23:16:28 +03:00
|
|
|
int rv;
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2010-06-01 03:09:29 +04:00
|
|
|
if ((rv = pthread_attr_init(&pattr)) != 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (joinable) {
|
|
|
|
NOFAIL(ptidp = malloc(sizeof(*ptidp)));
|
|
|
|
pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
|
|
|
|
} else {
|
|
|
|
ptidp = &ptid;
|
|
|
|
pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = pthread_create(ptidp, &pattr, f, arg);
|
2012-10-08 22:02:04 +04:00
|
|
|
#if defined(__NetBSD__)
|
2008-12-17 23:16:28 +03:00
|
|
|
if (rv == 0 && thrname)
|
|
|
|
pthread_setname_np(ptid, thrname, NULL);
|
2012-10-08 22:02:04 +04:00
|
|
|
#elif defined(__linux__)
|
2013-02-11 20:02:31 +04:00
|
|
|
/*
|
|
|
|
* The pthread_setname_np() call varies from one Linux distro to
|
|
|
|
* another. Comment out the call pending autoconf support.
|
|
|
|
*/
|
|
|
|
#if 0
|
2012-10-08 22:02:04 +04:00
|
|
|
if (rv == 0 && thrname)
|
|
|
|
pthread_setname_np(ptid, thrname);
|
2013-02-11 20:02:31 +04:00
|
|
|
#endif
|
2008-12-17 23:16:28 +03:00
|
|
|
#endif
|
|
|
|
|
2010-06-01 03:09:29 +04:00
|
|
|
if (joinable) {
|
|
|
|
assert(ptcookie);
|
|
|
|
*ptcookie = ptidp;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_attr_destroy(&pattr);
|
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
2009-07-15 01:00:53 +04:00
|
|
|
__dead void
|
2009-03-18 13:22:21 +03:00
|
|
|
rumpuser_thread_exit(void)
|
2007-10-31 18:57:19 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2010-06-01 03:09:29 +04:00
|
|
|
int
|
|
|
|
rumpuser_thread_join(void *ptcookie)
|
|
|
|
{
|
|
|
|
pthread_t *pt = ptcookie;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
KLOCK_WRAP((rv = pthread_join(*pt, NULL)));
|
|
|
|
if (rv == 0)
|
|
|
|
free(pt);
|
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2010-06-01 03:09:29 +04:00
|
|
|
}
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
void
|
2013-04-27 20:32:56 +04:00
|
|
|
rumpuser_mutex_init(struct rumpuser_mtx **mtx, int flags)
|
2007-10-31 18:57:19 +03:00
|
|
|
{
|
2009-09-09 00:04:03 +04:00
|
|
|
pthread_mutexattr_t att;
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
NOFAIL(*mtx = malloc(sizeof(struct rumpuser_mtx)));
|
2009-09-09 00:04:03 +04:00
|
|
|
|
|
|
|
pthread_mutexattr_init(&att);
|
|
|
|
pthread_mutexattr_settype(&att, PTHREAD_MUTEX_ERRORCHECK);
|
|
|
|
NOFAIL_ERRNO(pthread_mutex_init(&((*mtx)->pthmtx), &att));
|
|
|
|
pthread_mutexattr_destroy(&att);
|
|
|
|
|
2009-01-11 01:28:42 +03:00
|
|
|
(*mtx)->owner = NULL;
|
2013-04-27 20:32:56 +04:00
|
|
|
assert(flags != 0);
|
|
|
|
(*mtx)->flags = flags;
|
2010-12-01 17:59:37 +03:00
|
|
|
}
|
|
|
|
|
2009-01-11 01:28:42 +03:00
|
|
|
static void
|
|
|
|
mtxenter(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
2013-04-27 20:32:56 +04:00
|
|
|
if (!(mtx->flags & RUMPUSER_MTX_KMUTEX))
|
2010-12-01 17:59:37 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
assert(mtx->owner == NULL);
|
|
|
|
mtx->owner = rumpuser_get_curlwp();
|
2009-01-11 01:28:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mtxexit(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
2013-04-27 20:32:56 +04:00
|
|
|
if (!(mtx->flags & RUMPUSER_MTX_KMUTEX))
|
2010-12-01 17:59:37 +03:00
|
|
|
return;
|
|
|
|
|
2009-01-11 01:28:42 +03:00
|
|
|
assert(mtx->owner != NULL);
|
2010-12-01 17:59:37 +03:00
|
|
|
mtx->owner = NULL;
|
2009-01-11 01:28:42 +03:00
|
|
|
}
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
void
|
|
|
|
rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
2013-04-27 20:32:56 +04:00
|
|
|
if (mtx->flags & RUMPUSER_MTX_SPIN) {
|
2013-04-27 17:59:46 +04:00
|
|
|
rumpuser_mutex_enter_nowrap(mtx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-27 20:32:56 +04:00
|
|
|
assert(mtx->flags & RUMPUSER_MTX_KMUTEX);
|
2009-10-15 04:28:46 +04:00
|
|
|
if (pthread_mutex_trylock(&mtx->pthmtx) != 0)
|
|
|
|
KLOCK_WRAP(NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx)));
|
|
|
|
mtxenter(mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
2013-04-27 20:32:56 +04:00
|
|
|
assert(mtx->flags & RUMPUSER_MTX_SPIN);
|
2009-10-15 04:28:46 +04:00
|
|
|
NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx));
|
2009-01-11 01:28:42 +03:00
|
|
|
mtxenter(mtx);
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
2009-01-11 01:28:42 +03:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = pthread_mutex_trylock(&mtx->pthmtx);
|
|
|
|
if (rv == 0) {
|
|
|
|
mtxenter(mtx);
|
|
|
|
}
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_mutex_exit(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
2009-01-11 01:28:42 +03:00
|
|
|
mtxexit(mtx);
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_mutex_unlock(&mtx->pthmtx));
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_mutex_destroy(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_mutex_destroy(&mtx->pthmtx));
|
2007-10-31 18:57:19 +03:00
|
|
|
free(mtx);
|
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
void
|
|
|
|
rumpuser_mutex_owner(struct rumpuser_mtx *mtx, struct lwp **lp)
|
2008-01-30 12:50:19 +03:00
|
|
|
{
|
|
|
|
|
2013-04-27 20:32:56 +04:00
|
|
|
if (__predict_false(!(mtx->flags & RUMPUSER_MTX_KMUTEX))) {
|
2010-12-01 17:59:37 +03:00
|
|
|
printf("panic: rumpuser_mutex_held unsupported on non-kmtx\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
*lp = mtx->owner;
|
2008-01-30 12:50:19 +03:00
|
|
|
}
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
void
|
|
|
|
rumpuser_rw_init(struct rumpuser_rw **rw)
|
|
|
|
{
|
|
|
|
|
|
|
|
NOFAIL(*rw = malloc(sizeof(struct rumpuser_rw)));
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_rwlock_init(&((*rw)->pthrw), NULL));
|
2012-11-26 21:54:51 +04:00
|
|
|
NOFAIL_ERRNO(pthread_spin_init(&((*rw)->spin),PTHREAD_PROCESS_PRIVATE));
|
2009-01-11 01:28:42 +03:00
|
|
|
(*rw)->readers = 0;
|
|
|
|
(*rw)->writer = NULL;
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-27 02:26:59 +04:00
|
|
|
rumpuser_rw_enter(struct rumpuser_rw *rw, int iswrite)
|
2007-10-31 18:57:19 +03:00
|
|
|
{
|
|
|
|
|
2009-04-27 02:26:59 +04:00
|
|
|
if (iswrite) {
|
2009-10-15 04:28:46 +04:00
|
|
|
if (pthread_rwlock_trywrlock(&rw->pthrw) != 0)
|
|
|
|
KLOCK_WRAP(NOFAIL_ERRNO(
|
|
|
|
pthread_rwlock_wrlock(&rw->pthrw)));
|
2009-01-11 01:28:42 +03:00
|
|
|
RURW_SETWRITE(rw);
|
|
|
|
} else {
|
2009-10-15 04:28:46 +04:00
|
|
|
if (pthread_rwlock_tryrdlock(&rw->pthrw) != 0)
|
|
|
|
KLOCK_WRAP(NOFAIL_ERRNO(
|
|
|
|
pthread_rwlock_rdlock(&rw->pthrw)));
|
2009-01-11 01:28:42 +03:00
|
|
|
RURW_INCREAD(rw);
|
|
|
|
}
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-04-27 02:26:59 +04:00
|
|
|
rumpuser_rw_tryenter(struct rumpuser_rw *rw, int iswrite)
|
2007-10-31 18:57:19 +03:00
|
|
|
{
|
2009-01-11 01:28:42 +03:00
|
|
|
int rv;
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2009-04-27 02:26:59 +04:00
|
|
|
if (iswrite) {
|
2009-01-11 01:28:42 +03:00
|
|
|
rv = pthread_rwlock_trywrlock(&rw->pthrw);
|
|
|
|
if (rv == 0)
|
|
|
|
RURW_SETWRITE(rw);
|
|
|
|
} else {
|
|
|
|
rv = pthread_rwlock_tryrdlock(&rw->pthrw);
|
|
|
|
if (rv == 0)
|
|
|
|
RURW_INCREAD(rw);
|
|
|
|
}
|
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_rw_exit(struct rumpuser_rw *rw)
|
|
|
|
{
|
|
|
|
|
2009-01-26 22:34:12 +03:00
|
|
|
if (RURW_HASREAD(rw))
|
2009-01-11 01:28:42 +03:00
|
|
|
RURW_DECREAD(rw);
|
2009-01-26 22:34:12 +03:00
|
|
|
else
|
|
|
|
RURW_CLRWRITE(rw);
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_rwlock_unlock(&rw->pthrw));
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_rw_destroy(struct rumpuser_rw *rw)
|
|
|
|
{
|
|
|
|
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_rwlock_destroy(&rw->pthrw));
|
2009-01-11 01:28:42 +03:00
|
|
|
NOFAIL_ERRNO(pthread_spin_destroy(&rw->spin));
|
2007-10-31 18:57:19 +03:00
|
|
|
free(rw);
|
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
void
|
|
|
|
rumpuser_rw_held(struct rumpuser_rw *rw, int *rv)
|
2008-01-30 12:50:19 +03:00
|
|
|
{
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
*rv = rw->readers != 0;
|
2008-01-30 12:50:19 +03:00
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
void
|
|
|
|
rumpuser_rw_rdheld(struct rumpuser_rw *rw, int *rv)
|
2008-01-30 12:50:19 +03:00
|
|
|
{
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
*rv = RURW_HASREAD(rw);
|
2008-01-30 12:50:19 +03:00
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
void
|
|
|
|
rumpuser_rw_wrheld(struct rumpuser_rw *rw, int *rv)
|
2008-01-30 12:50:19 +03:00
|
|
|
{
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
*rv = RURW_AMWRITER(rw);
|
2008-01-30 12:50:19 +03:00
|
|
|
}
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
void
|
|
|
|
rumpuser_cv_init(struct rumpuser_cv **cv)
|
|
|
|
{
|
|
|
|
|
|
|
|
NOFAIL(*cv = malloc(sizeof(struct rumpuser_cv)));
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_cond_init(&((*cv)->pthcv), NULL));
|
2009-01-11 01:28:42 +03:00
|
|
|
(*cv)->nwaiters = 0;
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_cv_destroy(struct rumpuser_cv *cv)
|
|
|
|
{
|
|
|
|
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_cond_destroy(&cv->pthcv));
|
2007-10-31 18:57:19 +03:00
|
|
|
free(cv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
|
|
|
|
{
|
2010-05-18 18:58:41 +04:00
|
|
|
int nlocks;
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2009-01-11 01:28:42 +03:00
|
|
|
cv->nwaiters++;
|
2013-04-29 18:51:39 +04:00
|
|
|
rumpkern_unsched(&nlocks, mtx);
|
2009-01-11 01:28:42 +03:00
|
|
|
mtxexit(mtx);
|
2010-05-18 18:58:41 +04:00
|
|
|
NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
|
2009-01-11 01:28:42 +03:00
|
|
|
mtxenter(mtx);
|
2013-04-29 18:51:39 +04:00
|
|
|
rumpkern_sched(nlocks, mtx);
|
2009-01-11 01:28:42 +03:00
|
|
|
cv->nwaiters--;
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
2009-10-15 04:28:46 +04:00
|
|
|
void
|
|
|
|
rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
|
|
|
cv->nwaiters++;
|
|
|
|
mtxexit(mtx);
|
|
|
|
NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
|
|
|
|
mtxenter(mtx);
|
|
|
|
cv->nwaiters--;
|
|
|
|
}
|
|
|
|
|
2007-11-17 23:50:18 +03:00
|
|
|
int
|
|
|
|
rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx,
|
2009-11-11 19:46:50 +03:00
|
|
|
int64_t sec, int64_t nsec)
|
2007-11-17 23:50:18 +03:00
|
|
|
{
|
2009-11-11 19:46:50 +03:00
|
|
|
struct timespec ts;
|
2010-05-18 18:58:41 +04:00
|
|
|
int rv, nlocks;
|
2007-11-17 23:50:18 +03:00
|
|
|
|
2013-04-28 17:37:51 +04:00
|
|
|
/*
|
|
|
|
* Get clock already here, just in case we will be put to sleep
|
|
|
|
* after releasing the kernel context.
|
|
|
|
*
|
|
|
|
* The condition variables should use CLOCK_MONOTONIC, but since
|
|
|
|
* that's not available everywhere, leave it for another day.
|
|
|
|
*/
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
2009-11-11 19:46:50 +03:00
|
|
|
|
2009-01-11 01:28:42 +03:00
|
|
|
cv->nwaiters++;
|
2013-04-29 18:51:39 +04:00
|
|
|
rumpkern_unsched(&nlocks, mtx);
|
2009-01-11 01:28:42 +03:00
|
|
|
mtxexit(mtx);
|
2013-04-28 17:37:51 +04:00
|
|
|
|
|
|
|
ts.tv_sec += sec;
|
|
|
|
ts.tv_nsec += nsec;
|
|
|
|
if (ts.tv_nsec >= 1000*1000*1000) {
|
|
|
|
ts.tv_sec++;
|
|
|
|
ts.tv_nsec -= 1000*1000*1000;
|
|
|
|
}
|
2010-05-18 18:58:41 +04:00
|
|
|
rv = pthread_cond_timedwait(&cv->pthcv, &mtx->pthmtx, &ts);
|
2009-01-11 01:28:42 +03:00
|
|
|
mtxenter(mtx);
|
2013-04-29 18:51:39 +04:00
|
|
|
rumpkern_sched(nlocks, mtx);
|
2009-01-11 01:28:42 +03:00
|
|
|
cv->nwaiters--;
|
2007-11-17 23:50:18 +03:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-11-17 23:50:18 +03:00
|
|
|
}
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
void
|
|
|
|
rumpuser_cv_signal(struct rumpuser_cv *cv)
|
|
|
|
{
|
|
|
|
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_cond_signal(&cv->pthcv));
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
2007-11-19 17:17:22 +03:00
|
|
|
void
|
|
|
|
rumpuser_cv_broadcast(struct rumpuser_cv *cv)
|
|
|
|
{
|
|
|
|
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_cond_broadcast(&cv->pthcv));
|
2007-11-19 17:17:22 +03:00
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
void
|
|
|
|
rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *nwaiters)
|
2008-07-18 20:19:12 +04:00
|
|
|
{
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
*nwaiters = cv->nwaiters;
|
2008-07-18 20:19:12 +04:00
|
|
|
}
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
/*
|
|
|
|
* curlwp
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_set_curlwp(struct lwp *l)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(pthread_getspecific(curlwpkey) == NULL || l == NULL);
|
|
|
|
pthread_setspecific(curlwpkey, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lwp *
|
2009-03-18 13:22:21 +03:00
|
|
|
rumpuser_get_curlwp(void)
|
2007-10-31 18:57:19 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
return pthread_getspecific(curlwpkey);
|
|
|
|
}
|