2008-12-17 23:16:28 +03:00
|
|
|
/* $NetBSD: rumpuser_pth.c,v 1.20 2008/12/17 20:16:28 pooka Exp $ */
|
2007-10-31 18:57:19 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Antti Kantee. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Development of this software was supported by the
|
|
|
|
* Finnish Cultural Foundation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-07-01 16:33:32 +04:00
|
|
|
#ifdef __linux__
|
|
|
|
#define _XOPEN_SOURCE 500
|
|
|
|
#define _BSD_SOURCE
|
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#endif
|
2007-10-31 18:57:19 +03:00
|
|
|
|
|
|
|
#include <assert.h>
|
2007-11-17 23:50:18 +03:00
|
|
|
#include <errno.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>
|
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;
|
|
|
|
static pthread_key_t isintr;
|
2007-10-31 18:57:19 +03:00
|
|
|
|
|
|
|
#define NOFAIL(a) do {if (!(a)) abort();} while (/*CONSTCOND*/0)
|
2008-03-11 13:50:16 +03:00
|
|
|
#define NOFAIL_ERRNO(a) \
|
|
|
|
do { \
|
|
|
|
int fail_rv = (a); \
|
|
|
|
if (fail_rv) { \
|
|
|
|
printf("panic: rumpuser fatal failure %d (%s)\n", \
|
|
|
|
fail_rv, strerror(fail_rv)); \
|
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
} while (/*CONSTCOND*/0)
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2007-11-07 21:59:18 +03:00
|
|
|
struct rumpuser_mtx {
|
|
|
|
pthread_mutex_t pthmtx;
|
|
|
|
};
|
|
|
|
|
2007-11-07 18:41:18 +03:00
|
|
|
struct rumpuser_rw {
|
|
|
|
pthread_rwlock_t pthrw;
|
|
|
|
};
|
|
|
|
|
2007-11-07 21:59:18 +03:00
|
|
|
struct rumpuser_cv {
|
|
|
|
pthread_cond_t pthcv;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rumpuser_mtx rua_mtx;
|
|
|
|
struct rumpuser_cv rua_cv;
|
|
|
|
int rua_head, rua_tail;
|
|
|
|
struct rumpuser_aio *rua_aios[N_AIOS];
|
|
|
|
|
2007-11-07 18:41:18 +03:00
|
|
|
struct rumpuser_rw rumpspl;
|
|
|
|
|
2007-11-07 21:59:18 +03:00
|
|
|
static void *
|
2008-10-30 04:54:24 +03:00
|
|
|
/*ARGSUSED*/
|
2007-11-07 21:59:18 +03:00
|
|
|
iothread(void *arg)
|
|
|
|
{
|
|
|
|
struct rumpuser_aio *rua;
|
2008-11-18 15:39:35 +03:00
|
|
|
rump_biodone_fn biodone = arg;
|
2007-11-07 21:59:18 +03:00
|
|
|
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_mutex_lock(&rua_mtx.pthmtx));
|
2007-11-07 21:59:18 +03:00
|
|
|
for (;;) {
|
|
|
|
while (rua_head == rua_tail) {
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_cond_wait(&rua_cv.pthcv,
|
|
|
|
&rua_mtx.pthmtx));
|
2007-11-07 21:59:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rua = rua_aios[rua_tail];
|
|
|
|
rua_tail = (rua_tail+1) % (N_AIOS-1);
|
|
|
|
pthread_mutex_unlock(&rua_mtx.pthmtx);
|
|
|
|
|
|
|
|
if (rua->rua_op)
|
2008-01-02 21:15:12 +03:00
|
|
|
rumpuser_read_bio(rua->rua_fd, rua->rua_data,
|
2008-11-18 15:39:35 +03:00
|
|
|
rua->rua_dlen, rua->rua_off, biodone, rua->rua_bp);
|
2007-11-07 21:59:18 +03:00
|
|
|
else
|
2008-01-02 21:15:12 +03:00
|
|
|
rumpuser_write_bio(rua->rua_fd, rua->rua_data,
|
2008-11-18 15:39:35 +03:00
|
|
|
rua->rua_dlen, rua->rua_off, biodone, rua->rua_bp);
|
2007-11-07 21:59:18 +03:00
|
|
|
|
|
|
|
free(rua);
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_mutex_lock(&rua_mtx.pthmtx));
|
2007-11-07 21:59:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
int
|
|
|
|
rumpuser_thrinit()
|
|
|
|
{
|
2007-11-07 21:59:18 +03:00
|
|
|
|
|
|
|
pthread_mutex_init(&rua_mtx.pthmtx, NULL);
|
|
|
|
pthread_cond_init(&rua_cv.pthcv, NULL);
|
|
|
|
pthread_rwlock_init(&rumpspl.pthrw, NULL);
|
2007-10-31 18:57:19 +03:00
|
|
|
|
|
|
|
pthread_key_create(&curlwpkey, NULL);
|
2007-11-07 18:41:18 +03:00
|
|
|
pthread_key_create(&isintr, NULL);
|
|
|
|
|
2008-11-18 15:39:35 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rumpuser_bioinit(rump_biodone_fn biodone)
|
|
|
|
{
|
|
|
|
extern int rump_threads;
|
|
|
|
pthread_t iothr;
|
|
|
|
|
2008-08-12 14:04:57 +04:00
|
|
|
if (rump_threads)
|
2008-11-18 15:39:35 +03:00
|
|
|
pthread_create(&iothr, NULL, iothread, biodone);
|
2007-11-07 18:41:18 +03:00
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_thrdestroy()
|
|
|
|
{
|
|
|
|
|
|
|
|
pthread_key_delete(curlwpkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-12-17 23:16:28 +03:00
|
|
|
rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname)
|
2007-10-31 18:57:19 +03:00
|
|
|
{
|
|
|
|
pthread_t ptid;
|
2008-12-17 23:16:28 +03:00
|
|
|
int rv;
|
2007-10-31 18:57:19 +03:00
|
|
|
|
2008-12-17 23:16:28 +03:00
|
|
|
rv = pthread_create(&ptid, NULL, f, arg);
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
if (rv == 0 && thrname)
|
|
|
|
pthread_setname_np(ptid, thrname, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rv;
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_thread_exit()
|
|
|
|
{
|
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_mutex_init(struct rumpuser_mtx **mtx)
|
|
|
|
{
|
|
|
|
NOFAIL(*mtx = malloc(sizeof(struct rumpuser_mtx)));
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_mutex_init(&((*mtx)->pthmtx), NULL));
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
2007-11-07 21:59:18 +03:00
|
|
|
void
|
|
|
|
rumpuser_mutex_recursive_init(struct rumpuser_mtx **mtx)
|
|
|
|
{
|
|
|
|
pthread_mutexattr_t mattr;
|
|
|
|
|
|
|
|
pthread_mutexattr_init(&mattr);
|
|
|
|
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
|
|
|
|
NOFAIL(*mtx = malloc(sizeof(struct rumpuser_mtx)));
|
2008-03-11 13:50:16 +03:00
|
|
|
NOFAIL_ERRNO(pthread_mutex_init(&((*mtx)->pthmtx), &mattr));
|
2007-11-07 21:59:18 +03:00
|
|
|
|
|
|
|
pthread_mutexattr_destroy(&mattr);
|
|
|
|
}
|
|
|
|
|
2007-10-31 18:57:19 +03:00
|
|
|
void
|
|
|
|
rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
2008-10-09 05:19:06 +04:00
|
|
|
KLOCK_WRAP(NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx)));
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pthread_mutex_trylock(&mtx->pthmtx) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_mutex_exit(struct rumpuser_mtx *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);
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:50:19 +03:00
|
|
|
int
|
|
|
|
rumpuser_mutex_held(struct rumpuser_mtx *mtx)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pthread_mutex_held_np(&mtx->pthmtx);
|
|
|
|
}
|
|
|
|
|
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));
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_rw_enter(struct rumpuser_rw *rw, int write)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (write)
|
2008-10-09 05:19:06 +04:00
|
|
|
KLOCK_WRAP(NOFAIL_ERRNO(pthread_rwlock_wrlock(&rw->pthrw)));
|
2007-10-31 18:57:19 +03:00
|
|
|
else
|
2008-10-09 05:19:06 +04:00
|
|
|
KLOCK_WRAP(NOFAIL_ERRNO(pthread_rwlock_rdlock(&rw->pthrw)));
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rumpuser_rw_tryenter(struct rumpuser_rw *rw, int write)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (write)
|
|
|
|
return pthread_rwlock_trywrlock(&rw->pthrw) == 0;
|
|
|
|
else
|
|
|
|
return pthread_rwlock_tryrdlock(&rw->pthrw) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_rw_exit(struct rumpuser_rw *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));
|
2007-10-31 18:57:19 +03:00
|
|
|
free(rw);
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:50:19 +03:00
|
|
|
int
|
|
|
|
rumpuser_rw_held(struct rumpuser_rw *rw)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pthread_rwlock_held_np(&rw->pthrw);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rumpuser_rw_rdheld(struct rumpuser_rw *rw)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pthread_rwlock_rdheld_np(&rw->pthrw);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rumpuser_rw_wrheld(struct rumpuser_rw *rw)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pthread_rwlock_wrheld_np(&rw->pthrw);
|
|
|
|
}
|
|
|
|
|
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));
|
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)
|
|
|
|
{
|
|
|
|
|
2008-10-09 05:19:06 +04:00
|
|
|
KLOCK_WRAP(NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx)));
|
2007-10-31 18:57:19 +03:00
|
|
|
}
|
|
|
|
|
2007-11-17 23:50:18 +03:00
|
|
|
int
|
|
|
|
rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx,
|
|
|
|
int stdticks)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
int rv;
|
|
|
|
|
2008-01-18 17:12:19 +03:00
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
ts.tv_sec += stdticks / 100;
|
|
|
|
ts.tv_nsec += (stdticks % 100) * 10000000;
|
|
|
|
ts.tv_sec += ts.tv_nsec / 1000000000;
|
|
|
|
ts.tv_nsec %= 1000000000;
|
2007-11-17 23:50:18 +03:00
|
|
|
|
2008-10-09 05:19:06 +04:00
|
|
|
KLOCK_WRAP(rv = pthread_cond_timedwait(&cv->pthcv, &mtx->pthmtx, &ts));
|
2007-11-17 23:50:18 +03:00
|
|
|
if (rv != 0 && rv != ETIMEDOUT)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
if (rv == ETIMEDOUT)
|
|
|
|
rv = EWOULDBLOCK;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-07-20 23:03:04 +04:00
|
|
|
int
|
2008-07-18 20:19:12 +04:00
|
|
|
rumpuser_cv_has_waiters(struct rumpuser_cv *cv)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pthread_cond_has_waiters_np(&cv->pthcv);
|
|
|
|
}
|
|
|
|
|
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 *
|
|
|
|
rumpuser_get_curlwp()
|
|
|
|
{
|
|
|
|
|
|
|
|
return pthread_getspecific(curlwpkey);
|
|
|
|
}
|
2007-11-07 18:41:18 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* I am the interrupt
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2007-11-07 21:59:18 +03:00
|
|
|
rumpuser_set_ipl(int what)
|
2007-11-07 18:41:18 +03:00
|
|
|
{
|
2007-11-07 21:59:18 +03:00
|
|
|
int cur;
|
|
|
|
|
|
|
|
if (what == RUMPUSER_IPL_INTR) {
|
|
|
|
pthread_setspecific(isintr, (void *)RUMPUSER_IPL_INTR);
|
|
|
|
} else {
|
2007-11-08 13:57:19 +03:00
|
|
|
cur = (int)(intptr_t)pthread_getspecific(isintr);
|
|
|
|
pthread_setspecific(isintr, (void *)(intptr_t)(cur+1));
|
2007-11-07 21:59:18 +03:00
|
|
|
}
|
2007-11-07 18:41:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-11-07 21:59:18 +03:00
|
|
|
rumpuser_whatis_ipl()
|
2007-11-07 18:41:18 +03:00
|
|
|
{
|
|
|
|
|
2007-11-08 13:57:19 +03:00
|
|
|
return (int)(intptr_t)pthread_getspecific(isintr);
|
2007-11-07 18:41:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-07 21:59:18 +03:00
|
|
|
rumpuser_clear_ipl(int what)
|
2007-11-07 18:41:18 +03:00
|
|
|
{
|
2007-11-07 21:59:18 +03:00
|
|
|
int cur;
|
|
|
|
|
|
|
|
if (what == RUMPUSER_IPL_INTR)
|
|
|
|
cur = 1;
|
|
|
|
else
|
2007-11-08 13:57:19 +03:00
|
|
|
cur = (int)(intptr_t)pthread_getspecific(isintr);
|
2007-11-07 21:59:18 +03:00
|
|
|
cur--;
|
2007-11-07 18:41:18 +03:00
|
|
|
|
2007-11-08 13:57:19 +03:00
|
|
|
pthread_setspecific(isintr, (void *)(intptr_t)cur);
|
2007-11-07 18:41:18 +03:00
|
|
|
}
|