Added thread_switch and some bits of Mach semaphores
This commit is contained in:
parent
32d2556b0c
commit
530e1a6b60
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: files.mach,v 1.9 2002/12/07 21:23:03 manu Exp $
|
||||
# $NetBSD: files.mach,v 1.10 2002/12/12 23:18:20 manu Exp $
|
||||
#
|
||||
# Config file description for machine-independent Mach compat code.
|
||||
# Included by ports that need it.
|
||||
|
@ -15,6 +15,7 @@ file compat/mach/mach_misc.c compat_mach | compat_darwin
|
|||
file compat/mach/mach_message.c compat_mach | compat_darwin
|
||||
file compat/mach/mach_namemap.c compat_mach | compat_darwin
|
||||
file compat/mach/mach_port.c compat_mach | compat_darwin
|
||||
file compat/mach/mach_semaphore.c compat_mach | compat_darwin
|
||||
file compat/mach/mach_syscalls.c compat_mach | compat_darwin
|
||||
file compat/mach/mach_sysent.c compat_mach | compat_darwin
|
||||
file compat/mach/mach_task.c compat_mach | compat_darwin
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: mach_exec.c,v 1.13 2002/12/07 15:33:01 manu Exp $ */
|
||||
/* $NetBSD: mach_exec.c,v 1.14 2002/12/12 23:18:20 manu Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2001 The NetBSD Foundation, Inc.
|
||||
|
@ -37,12 +37,13 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_exec.c,v 1.13 2002/12/07 15:33:01 manu Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_exec.c,v 1.14 2002/12/12 23:18:20 manu Exp $");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/exec.h>
|
||||
#include <sys/queue.h>
|
||||
#include <sys/exec_macho.h>
|
||||
#include <sys/malloc.h>
|
||||
|
||||
|
@ -52,6 +53,8 @@ __KERNEL_RCSID(0, "$NetBSD: mach_exec.c,v 1.13 2002/12/07 15:33:01 manu Exp $");
|
|||
#include <uvm/uvm_param.h>
|
||||
|
||||
#include <compat/mach/mach_types.h>
|
||||
#include <compat/mach/mach_message.h>
|
||||
#include <compat/mach/mach_semaphore.h>
|
||||
#include <compat/mach/mach_exec.h>
|
||||
|
||||
static void mach_e_proc_exec(struct proc *, struct exec_package *);
|
||||
|
@ -224,6 +227,10 @@ mach_e_proc_init(p, vmspace)
|
|||
med = (struct mach_emuldata *)p->p_emuldata;
|
||||
med->med_p = 0;
|
||||
|
||||
/* Initialize semaphores if needed. Not the best place for that... */
|
||||
if (mach_semaphore_cold == 1)
|
||||
mach_semaphore_init();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -234,5 +241,7 @@ mach_e_proc_exit(p)
|
|||
free(p->p_emuldata, M_EMULDATA);
|
||||
p->p_emuldata = NULL;
|
||||
|
||||
mach_semaphore_cleanup(p);
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: mach_exec.h,v 1.6 2002/12/07 15:33:01 manu Exp $ */
|
||||
/* $NetBSD: mach_exec.h,v 1.7 2002/12/12 23:18:20 manu Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2001 The NetBSD Foundation, Inc.
|
||||
|
@ -45,6 +45,7 @@
|
|||
|
||||
struct mach_emuldata {
|
||||
mach_cproc_t med_p;
|
||||
int med_thpri;
|
||||
};
|
||||
|
||||
int exec_mach_copyargs(struct proc *, struct exec_package *,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: mach_misc.c,v 1.14 2002/11/29 17:08:16 manu Exp $ */
|
||||
/* $NetBSD: mach_misc.c,v 1.15 2002/12/12 23:18:21 manu Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2001 The NetBSD Foundation, Inc.
|
||||
|
@ -43,7 +43,7 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_misc.c,v 1.14 2002/11/29 17:08:16 manu Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_misc.c,v 1.15 2002/12/12 23:18:21 manu Exp $");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
|
@ -85,17 +85,6 @@ __KERNEL_RCSID(0, "$NetBSD: mach_misc.c,v 1.14 2002/11/29 17:08:16 manu Exp $");
|
|||
#include <compat/mach/mach_clock.h>
|
||||
#include <compat/mach/mach_syscallargs.h>
|
||||
|
||||
int
|
||||
mach_sys_semaphore_signal_trap(struct proc *p, void *v, register_t *r) {
|
||||
#ifdef DEBUG_MACH
|
||||
struct mach_sys_semaphore_signal_trap_args *ap = v;
|
||||
#endif
|
||||
*r = 0;
|
||||
DPRINTF(("mach_sys_semaphore_signal_trap(0x%x);\n",
|
||||
SCARG(ap, signal_name)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
mach_sys_semaphore_signal_all_trap(struct proc *p, void *v, register_t *r) {
|
||||
|
@ -121,18 +110,6 @@ mach_sys_semaphore_signal_thread_trap(struct proc *p, void *v, register_t *r) {
|
|||
}
|
||||
|
||||
|
||||
int
|
||||
mach_sys_semaphore_wait_trap(struct proc *p, void *v, register_t *r) {
|
||||
#ifdef DEBUG_MACH
|
||||
struct mach_sys_semaphore_wait_trap_args *ap = v;
|
||||
#endif
|
||||
*r = 0;
|
||||
DPRINTF(("mach_sys_semaphore_wait_trap(0x%x);\n",
|
||||
SCARG(ap, wait_name)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
mach_sys_semaphore_wait_signal_trap(struct proc *p, void *v, register_t *r) {
|
||||
#ifdef DEBUG_MACH
|
||||
|
@ -260,18 +237,6 @@ mach_sys_swtch(struct proc *p, void *v, register_t *r) {
|
|||
}
|
||||
|
||||
|
||||
int
|
||||
mach_sys_syscall_thread_switch(struct proc *p, void *v, register_t *r) {
|
||||
#ifdef DEBUG_MACH
|
||||
struct mach_sys_syscall_thread_switch_args *ap = v;
|
||||
#endif
|
||||
*r = 0;
|
||||
DPRINTF(("mach_sys_syscall_thread_switch(0x%x, %d, %d);\n",
|
||||
SCARG(ap, thread_name), SCARG(ap, option), SCARG(ap, option_time)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
mach_sys_wait_until(struct proc *p, void *v, register_t *r) {
|
||||
#ifdef DEBUG_MACH
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: mach_namemap.c,v 1.10 2002/12/12 00:29:24 manu Exp $ */
|
||||
/* $NetBSD: mach_namemap.c,v 1.11 2002/12/12 23:18:21 manu Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
||||
|
@ -37,7 +37,7 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_namemap.c,v 1.10 2002/12/12 00:29:24 manu Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_namemap.c,v 1.11 2002/12/12 23:18:21 manu Exp $");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
|
@ -50,6 +50,7 @@ __KERNEL_RCSID(0, "$NetBSD: mach_namemap.c,v 1.10 2002/12/12 00:29:24 manu Exp $
|
|||
#include <compat/mach/mach_port.h>
|
||||
#include <compat/mach/mach_task.h>
|
||||
#include <compat/mach/mach_thread.h>
|
||||
#include <compat/mach/mach_semaphore.h>
|
||||
#include <compat/mach/mach_vm.h>
|
||||
|
||||
struct mach_subsystem_namemap mach_namemap[] = {
|
||||
|
@ -70,6 +71,8 @@ struct mach_subsystem_namemap mach_namemap[] = {
|
|||
{ 3409, mach_task_get_special_port, "task_get_special_port" },
|
||||
{ 3410, mach_task_set_special_port, "task_set_special_port" },
|
||||
{ 3412, mach_thread_create_running, "thread_create_running" },
|
||||
{ 3418, mach_semaphore_create, "semaphore_create" },
|
||||
{ 3419, mach_semaphore_destroy, "semaphore_destroy" },
|
||||
{ 3616, mach_thread_policy, "thread_policy" },
|
||||
{ 3801, mach_vm_allocate, "vm_allocate" },
|
||||
{ 3802, mach_vm_deallocate, "vm_deallocate" },
|
||||
|
|
|
@ -0,0 +1,309 @@
|
|||
/* $NetBSD: mach_semaphore.c,v 1.1 2002/12/12 23:18:21 manu Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Emmanuel Dreyfus
|
||||
*
|
||||
* 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.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the NetBSD
|
||||
* Foundation, Inc. and its contributors.
|
||||
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``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 FOUNDATION 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_semaphore.c,v 1.1 2002/12/12 23:18:21 manu Exp $");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/signal.h>
|
||||
#include <sys/pool.h>
|
||||
#include <sys/lock.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/proc.h>
|
||||
|
||||
#include <compat/mach/mach_types.h>
|
||||
#include <compat/mach/mach_message.h>
|
||||
#include <compat/mach/mach_semaphore.h>
|
||||
#include <compat/mach/mach_clock.h>
|
||||
#include <compat/mach/mach_syscallargs.h>
|
||||
|
||||
/* Semaphore list, lock, pools and inited flag */
|
||||
static LIST_HEAD(mach_semaphore_list, mach_semaphore) mach_semaphore_list;
|
||||
static struct lock mach_semaphore_list_lock;
|
||||
static struct pool mach_semaphore_list_pool;
|
||||
static struct pool mach_waiting_proc_pool;
|
||||
int mach_semaphore_cold = 1;
|
||||
|
||||
/* Function to manipulate them */
|
||||
static struct mach_semaphore *mach_semaphore_get(int, int);
|
||||
static void mach_semaphore_put(struct mach_semaphore *);
|
||||
static struct mach_waiting_proc *mach_waiting_proc_get
|
||||
(struct proc *, struct mach_semaphore *);
|
||||
static void mach_waiting_proc_put
|
||||
(struct mach_waiting_proc *, struct mach_semaphore *, int);
|
||||
|
||||
int
|
||||
mach_sys_semaphore_wait_trap(p, v, retval)
|
||||
struct proc *p;
|
||||
void *v;
|
||||
register_t *retval;
|
||||
{
|
||||
struct mach_sys_semaphore_wait_trap_args /* {
|
||||
syscallarg(mach_port_name_t) wait_name;
|
||||
} */ *uap = v;
|
||||
struct mach_semaphore *ms;
|
||||
struct mach_waiting_proc *mwp;
|
||||
int blocked = 0;
|
||||
|
||||
ms = (struct mach_semaphore *)SCARG(uap, wait_name);
|
||||
|
||||
lockmgr(&ms->ms_lock, LK_EXCLUSIVE, NULL);
|
||||
ms->ms_value--;
|
||||
if (ms->ms_value < 0)
|
||||
blocked = 1;
|
||||
lockmgr(&ms->ms_lock, LK_RELEASE, NULL);
|
||||
|
||||
if (blocked != 0) {
|
||||
mwp = mach_waiting_proc_get(p, ms);
|
||||
while (ms->ms_value < 0)
|
||||
tsleep(mwp, PZERO, "sem_wait", 0);
|
||||
mach_waiting_proc_put(mwp, ms, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
mach_sys_semaphore_signal_trap(p, v, retval)
|
||||
struct proc *p;
|
||||
void *v;
|
||||
register_t *retval;
|
||||
{
|
||||
struct mach_sys_semaphore_signal_trap_args /* {
|
||||
syscallarg(mach_port_name_t) signal_name;
|
||||
} */ *uap = v;
|
||||
struct mach_semaphore *ms;
|
||||
struct mach_waiting_proc *mwp;
|
||||
int unblocked = 0;
|
||||
|
||||
ms = (struct mach_semaphore *)SCARG(uap, signal_name);
|
||||
|
||||
lockmgr(&ms->ms_lock, LK_EXCLUSIVE, NULL);
|
||||
ms->ms_value++;
|
||||
if (ms->ms_value >= 0)
|
||||
unblocked = 1;
|
||||
lockmgr(&ms->ms_lock, LK_RELEASE, NULL);
|
||||
|
||||
if (unblocked != 0) {
|
||||
lockmgr(&ms->ms_lock, LK_SHARED, NULL);
|
||||
mwp = TAILQ_FIRST(&ms->ms_waiting);
|
||||
wakeup(mwp);
|
||||
lockmgr(&ms->ms_lock, LK_RELEASE, NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
mach_semaphore_create(p, msgh, maxlen, dst)
|
||||
struct proc *p;
|
||||
mach_msg_header_t *msgh;
|
||||
size_t maxlen;
|
||||
mach_msg_header_t *dst;
|
||||
{
|
||||
mach_semaphore_create_request_t req;
|
||||
mach_semaphore_create_reply_t rep;
|
||||
struct mach_semaphore *ms;
|
||||
int error;
|
||||
|
||||
if ((error = copyin(msgh, &req, sizeof(req))) != 0)
|
||||
return error;
|
||||
|
||||
ms = mach_semaphore_get(req.req_value, req.req_policy);
|
||||
|
||||
bzero(&rep, sizeof(rep));
|
||||
|
||||
rep.rep_msgh.msgh_bits =
|
||||
MACH_MSGH_REPLY_LOCAL_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE);
|
||||
rep.rep_msgh.msgh_size = sizeof(rep) - sizeof(rep.rep_trailer);
|
||||
rep.rep_msgh.msgh_local_port = req.req_msgh.msgh_local_port;
|
||||
rep.rep_msgh.msgh_id = req.req_msgh.msgh_id + 100;
|
||||
rep.rep_sem.name = (mach_port_t)ms; /* Waiting for better */
|
||||
rep.rep_trailer.msgh_trailer_size = 8;
|
||||
|
||||
return MACH_MSG_RETURN(p, &rep, msgh, sizeof(rep), maxlen, dst);
|
||||
}
|
||||
|
||||
int
|
||||
mach_semaphore_destroy(p, msgh, maxlen, dst)
|
||||
struct proc *p;
|
||||
mach_msg_header_t *msgh;
|
||||
size_t maxlen;
|
||||
mach_msg_header_t *dst;
|
||||
{
|
||||
mach_semaphore_destroy_request_t req;
|
||||
mach_semaphore_destroy_reply_t rep;
|
||||
struct mach_semaphore *ms;
|
||||
int error;
|
||||
|
||||
if ((error = copyin(msgh, &req, sizeof(req))) != 0)
|
||||
return error;
|
||||
|
||||
ms = (struct mach_semaphore *)req.req_sem.name;
|
||||
mach_semaphore_put(ms);
|
||||
|
||||
bzero(&rep, sizeof(rep));
|
||||
|
||||
rep.rep_msgh.msgh_bits =
|
||||
MACH_MSGH_REPLY_LOCAL_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE);
|
||||
rep.rep_msgh.msgh_size = sizeof(rep) - sizeof(rep.rep_trailer);
|
||||
rep.rep_msgh.msgh_local_port = req.req_msgh.msgh_local_port;
|
||||
rep.rep_msgh.msgh_id = req.req_msgh.msgh_id + 100;
|
||||
rep.rep_trailer.msgh_trailer_size = 8;
|
||||
|
||||
return MACH_MSG_RETURN(p, &rep, msgh, sizeof(rep), maxlen, dst);
|
||||
}
|
||||
|
||||
void
|
||||
mach_semaphore_init(void)
|
||||
{
|
||||
LIST_INIT(&mach_semaphore_list);
|
||||
lockinit(&mach_semaphore_list_lock, PZERO|PCATCH, "mach_sem", 0, 0);
|
||||
pool_init(&mach_semaphore_list_pool, sizeof (struct mach_semaphore),
|
||||
0, 0, 128, "mach_sem_pool", NULL);
|
||||
pool_init(&mach_waiting_proc_pool, sizeof (struct mach_waiting_proc),
|
||||
0, 0, 128, "mach_waitp_pool", NULL);
|
||||
mach_semaphore_cold = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static struct mach_semaphore *
|
||||
mach_semaphore_get(value, policy)
|
||||
int value;
|
||||
int policy;
|
||||
{
|
||||
struct mach_semaphore *ms;
|
||||
|
||||
ms = (struct mach_semaphore *)pool_get(&mach_semaphore_list_pool,
|
||||
M_WAITOK);
|
||||
ms->ms_value = value;
|
||||
ms->ms_policy = policy;
|
||||
TAILQ_INIT(&ms->ms_waiting);
|
||||
lockinit(&ms->ms_lock, PZERO|PCATCH, "mach_waitp", 0, 0);
|
||||
|
||||
lockmgr(&mach_semaphore_list_lock, LK_EXCLUSIVE, NULL);
|
||||
LIST_INSERT_HEAD(&mach_semaphore_list, ms, ms_list);
|
||||
lockmgr(&mach_semaphore_list_lock, LK_RELEASE, NULL);
|
||||
|
||||
return ms;
|
||||
}
|
||||
|
||||
static void
|
||||
mach_semaphore_put(ms)
|
||||
struct mach_semaphore *ms;
|
||||
{
|
||||
struct mach_waiting_proc *mwp;
|
||||
|
||||
lockmgr(&ms->ms_lock, LK_EXCLUSIVE, NULL);
|
||||
while ((mwp = TAILQ_FIRST(&ms->ms_waiting)) != NULL)
|
||||
mach_waiting_proc_put(mwp, ms, 0);
|
||||
lockmgr(&ms->ms_lock, LK_RELEASE, NULL);
|
||||
lockmgr(&ms->ms_lock, LK_DRAIN, NULL);
|
||||
|
||||
lockmgr(&mach_semaphore_list_lock, LK_EXCLUSIVE, NULL);
|
||||
LIST_REMOVE(ms, ms_list);
|
||||
lockmgr(&mach_semaphore_list_lock, LK_RELEASE, NULL);
|
||||
|
||||
pool_put(&mach_semaphore_list_pool, ms);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static struct mach_waiting_proc *
|
||||
mach_waiting_proc_get(p, ms)
|
||||
struct proc *p;
|
||||
struct mach_semaphore *ms;
|
||||
{
|
||||
struct mach_waiting_proc *mwp;
|
||||
|
||||
mwp = (struct mach_waiting_proc *)pool_get(&mach_waiting_proc_pool,
|
||||
M_WAITOK);
|
||||
mwp->mwp_p = p;
|
||||
|
||||
lockmgr(&ms->ms_lock, LK_EXCLUSIVE, NULL);
|
||||
TAILQ_INSERT_TAIL(&ms->ms_waiting, mwp, mwp_list);
|
||||
lockmgr(&ms->ms_lock, LK_RELEASE, NULL);
|
||||
|
||||
return mwp;
|
||||
}
|
||||
|
||||
static void
|
||||
mach_waiting_proc_put(mwp, ms, locked)
|
||||
struct mach_waiting_proc *mwp;
|
||||
struct mach_semaphore *ms;
|
||||
int locked;
|
||||
{
|
||||
if (!locked)
|
||||
lockmgr(&ms->ms_lock, LK_EXCLUSIVE, NULL);
|
||||
TAILQ_REMOVE(&ms->ms_waiting, mwp, mwp_list);
|
||||
if (!locked)
|
||||
lockmgr(&ms->ms_lock, LK_RELEASE, NULL);
|
||||
pool_put(&mach_waiting_proc_pool, mwp);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Cleanup after process exit. Need improvements, there
|
||||
* can be some memory leaks here.
|
||||
*/
|
||||
void
|
||||
mach_semaphore_cleanup(p)
|
||||
struct proc *p;
|
||||
{
|
||||
struct mach_semaphore *ms;
|
||||
struct mach_waiting_proc *mwp;
|
||||
|
||||
lockmgr(&mach_semaphore_list_lock, LK_SHARED, NULL);
|
||||
LIST_FOREACH(ms, &mach_semaphore_list, ms_list) {
|
||||
lockmgr(&ms->ms_lock, LK_SHARED, NULL);
|
||||
TAILQ_FOREACH(mwp, &ms->ms_waiting, mwp_list)
|
||||
if (mwp->mwp_p == p) {
|
||||
lockmgr(&ms->ms_lock, LK_UPGRADE, NULL);
|
||||
mach_waiting_proc_put(mwp, ms, 0);
|
||||
ms->ms_value++;
|
||||
if (ms->ms_value >= 0)
|
||||
wakeup(TAILQ_FIRST(&ms->ms_waiting));
|
||||
}
|
||||
lockmgr(&ms->ms_lock, LK_RELEASE, NULL);
|
||||
}
|
||||
lockmgr(&mach_semaphore_list_lock, LK_RELEASE, NULL);
|
||||
|
||||
return;
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
/* $NetBSD: mach_semaphore.h,v 1.1 2002/12/12 23:18:21 manu Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Emmanuel Dreyfus
|
||||
*
|
||||
* 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.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the NetBSD
|
||||
* Foundation, Inc. and its contributors.
|
||||
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``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 FOUNDATION 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.
|
||||
*/
|
||||
|
||||
#ifndef _MACH_SEMAPHORE_H_
|
||||
#define _MACH_SEMAPHORE_H_
|
||||
|
||||
#include <sys/lock.h>
|
||||
#include <sys/queue.h>
|
||||
|
||||
extern int mach_semaphore_cold;
|
||||
|
||||
struct mach_waiting_proc {
|
||||
TAILQ_ENTRY(mach_waiting_proc) mwp_list;
|
||||
struct proc *mwp_p;
|
||||
};
|
||||
|
||||
struct mach_semaphore {
|
||||
int ms_value;
|
||||
int ms_policy;
|
||||
LIST_ENTRY(mach_semaphore) ms_list;
|
||||
TAILQ_HEAD(ms_waiting, mach_waiting_proc) ms_waiting;
|
||||
struct lock ms_lock;
|
||||
};
|
||||
|
||||
/* semaphore_create */
|
||||
|
||||
#define MACH_SYNC_POLICY_FIFO 0
|
||||
#define MACH_SYNC_POLICY_FIXED_PRIORITY 1
|
||||
|
||||
typedef struct {
|
||||
mach_msg_header_t req_msgh;
|
||||
mach_ndr_record_t req_ndr;
|
||||
int req_policy;
|
||||
int req_value;
|
||||
} mach_semaphore_create_request_t;
|
||||
|
||||
typedef struct {
|
||||
mach_msg_header_t rep_msgh;
|
||||
mach_msg_body_t rep_body;
|
||||
mach_msg_port_descriptor_t rep_sem;
|
||||
mach_msg_trailer_t rep_trailer;
|
||||
} mach_semaphore_create_reply_t;
|
||||
|
||||
/* semaphore_destroy */
|
||||
|
||||
typedef struct {
|
||||
mach_msg_header_t req_msgh;
|
||||
mach_msg_body_t req_body;
|
||||
mach_msg_port_descriptor_t req_sem;
|
||||
} mach_semaphore_destroy_request_t;
|
||||
|
||||
typedef struct {
|
||||
mach_msg_header_t rep_msgh;
|
||||
mach_ndr_record_t rep_ndr;
|
||||
mach_kern_return_t rep_retval;
|
||||
mach_msg_trailer_t rep_trailer;
|
||||
} mach_semaphore_destroy_reply_t;
|
||||
|
||||
void mach_semaphore_init(void);
|
||||
void mach_semaphore_cleanup(struct proc *);
|
||||
int mach_semaphore_create(struct proc *, mach_msg_header_t *,
|
||||
size_t, mach_msg_header_t *);
|
||||
int mach_semaphore_destroy(struct proc *, mach_msg_header_t *,
|
||||
size_t, mach_msg_header_t *);
|
||||
|
||||
#endif /* _MACH_SEMAPHORE_H_ */
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: mach_thread.c,v 1.4 2002/12/12 00:29:24 manu Exp $ */
|
||||
/* $NetBSD: mach_thread.c,v 1.5 2002/12/12 23:18:22 manu Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
||||
|
@ -37,21 +37,71 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_thread.c,v 1.4 2002/12/12 00:29:24 manu Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: mach_thread.c,v 1.5 2002/12/12 23:18:22 manu Exp $");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/signal.h>
|
||||
#include <sys/proc.h>
|
||||
|
||||
#include <compat/mach/mach_types.h>
|
||||
#include <compat/mach/mach_message.h>
|
||||
#include <compat/mach/mach_exec.h>
|
||||
#include <compat/mach/mach_clock.h>
|
||||
#include <compat/mach/mach_thread.h>
|
||||
#include <compat/mach/mach_errno.h>
|
||||
#include <compat/mach/mach_syscallargs.h>
|
||||
|
||||
int
|
||||
mach_sys_syscall_thread_switch(p, v, retval)
|
||||
struct proc *p;
|
||||
void *v;
|
||||
register_t *retval;
|
||||
{
|
||||
struct mach_sys_syscall_thread_switch_args /* {
|
||||
syscallarg(mach_port_name_t) thread_name;
|
||||
syscallarg(int) option;
|
||||
syscallarg(mach_msg_timeout_t) option_time;
|
||||
} */ *uap = v;
|
||||
int timeout;
|
||||
struct mach_emuldata *med;
|
||||
|
||||
med = (struct mach_emuldata *)p->p_emuldata;
|
||||
timeout = SCARG(uap, option_time) * hz / 1000;
|
||||
|
||||
/*
|
||||
* The day we will be able to find out the struct proc from
|
||||
* the port numeber, try to use preempt() to call the right thread.
|
||||
*/
|
||||
switch(SCARG(uap, option)) {
|
||||
case MACH_SWITCH_OPTION_NONE:
|
||||
yield();
|
||||
break;
|
||||
|
||||
case MACH_SWITCH_OPTION_WAIT:
|
||||
med->med_thpri = 1;
|
||||
while (med->med_thpri != 0)
|
||||
(void)tsleep(&med->med_thpri, PZERO,
|
||||
"thread_switch", timeout);
|
||||
break;
|
||||
|
||||
case MACH_SWITCH_OPTION_DEPRESS:
|
||||
case MACH_SWITCH_OPTION_IDLE:
|
||||
/* Use a callout to restore the priority after depression? */
|
||||
med->med_thpri = p->p_priority;
|
||||
p->p_priority = MAXPRI;
|
||||
break;
|
||||
|
||||
default:
|
||||
uprintf("mach_sys_syscall_thread_switch(): unknown option %d\n", SCARG(uap, option));
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
mach_thread_policy(p, msgh, maxlen, dst)
|
||||
struct proc *p;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: mach_thread.h,v 1.3 2002/12/12 00:29:24 manu Exp $ */
|
||||
/* $NetBSD: mach_thread.h,v 1.4 2002/12/12 23:18:22 manu Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
||||
|
@ -47,6 +47,7 @@
|
|||
#include <compat/mach/mach_types.h>
|
||||
#include <compat/mach/mach_message.h>
|
||||
|
||||
/* For mach_create_thread_child() */
|
||||
struct mach_create_thread_child_args {
|
||||
struct proc **mctc_proc;
|
||||
int mctc_flavor;
|
||||
|
@ -54,6 +55,12 @@ struct mach_create_thread_child_args {
|
|||
int mctc_child_done;
|
||||
};
|
||||
|
||||
/* For mach_sys_syscall_thread_switch() */
|
||||
#define MACH_SWITCH_OPTION_NONE 0
|
||||
#define MACH_SWITCH_OPTION_DEPRESS 1
|
||||
#define MACH_SWITCH_OPTION_WAIT 2
|
||||
#define MACH_SWITCH_OPTION_IDLE 3
|
||||
|
||||
/* thread_policy */
|
||||
typedef int mach_policy_t;
|
||||
|
||||
|
|
Loading…
Reference in New Issue