1*7836SJohn.Forte@Sun.COM /* 2*7836SJohn.Forte@Sun.COM * CDDL HEADER START 3*7836SJohn.Forte@Sun.COM * 4*7836SJohn.Forte@Sun.COM * The contents of this file are subject to the terms of the 5*7836SJohn.Forte@Sun.COM * Common Development and Distribution License (the "License"). 6*7836SJohn.Forte@Sun.COM * You may not use this file except in compliance with the License. 7*7836SJohn.Forte@Sun.COM * 8*7836SJohn.Forte@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*7836SJohn.Forte@Sun.COM * or http://www.opensolaris.org/os/licensing. 10*7836SJohn.Forte@Sun.COM * See the License for the specific language governing permissions 11*7836SJohn.Forte@Sun.COM * and limitations under the License. 12*7836SJohn.Forte@Sun.COM * 13*7836SJohn.Forte@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 14*7836SJohn.Forte@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*7836SJohn.Forte@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 16*7836SJohn.Forte@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 17*7836SJohn.Forte@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 18*7836SJohn.Forte@Sun.COM * 19*7836SJohn.Forte@Sun.COM * CDDL HEADER END 20*7836SJohn.Forte@Sun.COM */ 21*7836SJohn.Forte@Sun.COM /* 22*7836SJohn.Forte@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23*7836SJohn.Forte@Sun.COM * Use is subject to license terms. 24*7836SJohn.Forte@Sun.COM */ 25*7836SJohn.Forte@Sun.COM 26*7836SJohn.Forte@Sun.COM #ifndef _NSC_THREAD_H 27*7836SJohn.Forte@Sun.COM #define _NSC_THREAD_H 28*7836SJohn.Forte@Sun.COM 29*7836SJohn.Forte@Sun.COM #ifdef __cplusplus 30*7836SJohn.Forte@Sun.COM extern "C" { 31*7836SJohn.Forte@Sun.COM #endif 32*7836SJohn.Forte@Sun.COM 33*7836SJohn.Forte@Sun.COM #ifdef _KERNEL 34*7836SJohn.Forte@Sun.COM 35*7836SJohn.Forte@Sun.COM #include <sys/ksynch.h> /* for kmutex_t and kcondvar_t */ 36*7836SJohn.Forte@Sun.COM 37*7836SJohn.Forte@Sun.COM /* 38*7836SJohn.Forte@Sun.COM * A simple way to marshal kthreads into sets for use by nsctl / nskern 39*7836SJohn.Forte@Sun.COM * clients. The ns threads are created in user land by nskernd, and 40*7836SJohn.Forte@Sun.COM * then call into the nskern kernel module for allocation into sets. 41*7836SJohn.Forte@Sun.COM */ 42*7836SJohn.Forte@Sun.COM 43*7836SJohn.Forte@Sun.COM struct nsthread; 44*7836SJohn.Forte@Sun.COM struct nstset; 45*7836SJohn.Forte@Sun.COM 46*7836SJohn.Forte@Sun.COM #ifndef _BLIND_T 47*7836SJohn.Forte@Sun.COM #define _BLIND_T 48*7836SJohn.Forte@Sun.COM typedef void * blind_t; 49*7836SJohn.Forte@Sun.COM #endif /* _BLIND_T */ 50*7836SJohn.Forte@Sun.COM 51*7836SJohn.Forte@Sun.COM 52*7836SJohn.Forte@Sun.COM /* 53*7836SJohn.Forte@Sun.COM * Queue stuff that should really be in the DDI. 54*7836SJohn.Forte@Sun.COM */ 55*7836SJohn.Forte@Sun.COM 56*7836SJohn.Forte@Sun.COM typedef struct nst_q { 57*7836SJohn.Forte@Sun.COM struct nst_q *q_forw; 58*7836SJohn.Forte@Sun.COM struct nst_q *q_back; 59*7836SJohn.Forte@Sun.COM } nst_q_t; 60*7836SJohn.Forte@Sun.COM 61*7836SJohn.Forte@Sun.COM 62*7836SJohn.Forte@Sun.COM /* 63*7836SJohn.Forte@Sun.COM * Per thread data structure. 64*7836SJohn.Forte@Sun.COM */ 65*7836SJohn.Forte@Sun.COM 66*7836SJohn.Forte@Sun.COM typedef struct nsthread { 67*7836SJohn.Forte@Sun.COM nst_q_t tp_link; /* Doubly linked free list */ 68*7836SJohn.Forte@Sun.COM 69*7836SJohn.Forte@Sun.COM struct nstset *tp_set; /* Set to which thread belongs */ 70*7836SJohn.Forte@Sun.COM struct nsthread *tp_chain; /* Link in chain of threads in set */ 71*7836SJohn.Forte@Sun.COM 72*7836SJohn.Forte@Sun.COM kcondvar_t tp_cv; /* Suspend/resume synchronisation */ 73*7836SJohn.Forte@Sun.COM 74*7836SJohn.Forte@Sun.COM /* 75*7836SJohn.Forte@Sun.COM * Everything past this point is cleared when the thread is 76*7836SJohn.Forte@Sun.COM * initialised for (re)use. 77*7836SJohn.Forte@Sun.COM */ 78*7836SJohn.Forte@Sun.COM 79*7836SJohn.Forte@Sun.COM int tp_flag; /* State (below) */ 80*7836SJohn.Forte@Sun.COM 81*7836SJohn.Forte@Sun.COM void (*tp_func)(); /* First function */ 82*7836SJohn.Forte@Sun.COM blind_t tp_arg; /* Argument to tp_func */ 83*7836SJohn.Forte@Sun.COM } nsthread_t; 84*7836SJohn.Forte@Sun.COM 85*7836SJohn.Forte@Sun.COM /* 86*7836SJohn.Forte@Sun.COM * Flags for nst_init 87*7836SJohn.Forte@Sun.COM */ 88*7836SJohn.Forte@Sun.COM #define NST_CREATE 0x1 /* Create resources to run thread */ 89*7836SJohn.Forte@Sun.COM #define NST_SLEEP 0x2 /* Wait for resources to be available */ 90*7836SJohn.Forte@Sun.COM 91*7836SJohn.Forte@Sun.COM /* 92*7836SJohn.Forte@Sun.COM * Thread state flags 93*7836SJohn.Forte@Sun.COM */ 94*7836SJohn.Forte@Sun.COM #define NST_TF_INUSE 0x1 /* Thread currently in use */ 95*7836SJohn.Forte@Sun.COM #define NST_TF_ACTIVE 0x2 /* Thread is being manipulated */ 96*7836SJohn.Forte@Sun.COM #define NST_TF_PENDING 0x4 /* Thread is pending a create */ 97*7836SJohn.Forte@Sun.COM #define NST_TF_DESTROY 0x8 /* Destroy thread when finished */ 98*7836SJohn.Forte@Sun.COM #define NST_TF_KILL 0x10 /* Thread is being killed */ 99*7836SJohn.Forte@Sun.COM 100*7836SJohn.Forte@Sun.COM /* 101*7836SJohn.Forte@Sun.COM * Thread set. 102*7836SJohn.Forte@Sun.COM */ 103*7836SJohn.Forte@Sun.COM typedef struct nstset { 104*7836SJohn.Forte@Sun.COM struct nstset *set_next; /* Next set in list of sets */ 105*7836SJohn.Forte@Sun.COM 106*7836SJohn.Forte@Sun.COM nsthread_t *set_chain; /* Chain of all threads in set */ 107*7836SJohn.Forte@Sun.COM nst_q_t set_reuse; /* Chain of reusable threads */ 108*7836SJohn.Forte@Sun.COM nst_q_t set_free; /* Chain of free threads */ 109*7836SJohn.Forte@Sun.COM 110*7836SJohn.Forte@Sun.COM char set_name[32]; /* Name associated with set */ 111*7836SJohn.Forte@Sun.COM 112*7836SJohn.Forte@Sun.COM ushort_t set_nlive; /* No. of active threads */ 113*7836SJohn.Forte@Sun.COM ushort_t set_nthread; /* No. of threads in set */ 114*7836SJohn.Forte@Sun.COM int set_flag; /* State (below) */ 115*7836SJohn.Forte@Sun.COM int set_pending; /* Operation is pending */ 116*7836SJohn.Forte@Sun.COM 117*7836SJohn.Forte@Sun.COM kmutex_t set_lock; /* Mutex for chains and counts */ 118*7836SJohn.Forte@Sun.COM kcondvar_t set_kill_cv; /* Kill synchronisation */ 119*7836SJohn.Forte@Sun.COM kcondvar_t set_destroy_cv; /* Shutdown synchronisation */ 120*7836SJohn.Forte@Sun.COM volatile int set_destroy_cnt; /* No. of waiters */ 121*7836SJohn.Forte@Sun.COM 122*7836SJohn.Forte@Sun.COM kcondvar_t set_res_cv; /* Resource alloc synchronisation */ 123*7836SJohn.Forte@Sun.COM int set_res_cnt; /* No. of waiters */ 124*7836SJohn.Forte@Sun.COM } nstset_t; 125*7836SJohn.Forte@Sun.COM 126*7836SJohn.Forte@Sun.COM /* 127*7836SJohn.Forte@Sun.COM * Set state flags 128*7836SJohn.Forte@Sun.COM */ 129*7836SJohn.Forte@Sun.COM #define NST_SF_KILL 1 /* Set is being killed */ 130*7836SJohn.Forte@Sun.COM 131*7836SJohn.Forte@Sun.COM /* 132*7836SJohn.Forte@Sun.COM * General defines 133*7836SJohn.Forte@Sun.COM */ 134*7836SJohn.Forte@Sun.COM #define NST_KILL_TIMEOUT 100000 /* usec to wait for threads to die */ 135*7836SJohn.Forte@Sun.COM #define NST_MEMORY_TIMEOUT 500000 /* usec to wait for memory */ 136*7836SJohn.Forte@Sun.COM 137*7836SJohn.Forte@Sun.COM /* 138*7836SJohn.Forte@Sun.COM * Function prototypes 139*7836SJohn.Forte@Sun.COM */ 140*7836SJohn.Forte@Sun.COM 141*7836SJohn.Forte@Sun.COM int nst_add_thread(nstset_t *, int); 142*7836SJohn.Forte@Sun.COM nsthread_t *nst_create(nstset_t *, void (*)(), blind_t, int); 143*7836SJohn.Forte@Sun.COM int nst_del_thread(nstset_t *, int); 144*7836SJohn.Forte@Sun.COM void nst_destroy(nstset_t *); 145*7836SJohn.Forte@Sun.COM nstset_t *nst_init(char *, int); 146*7836SJohn.Forte@Sun.COM int nst_nlive(nstset_t *); 147*7836SJohn.Forte@Sun.COM int nst_nthread(nstset_t *); 148*7836SJohn.Forte@Sun.COM int nst_startup(void); 149*7836SJohn.Forte@Sun.COM void nst_shutdown(void); 150*7836SJohn.Forte@Sun.COM 151*7836SJohn.Forte@Sun.COM #endif /* _KERNEL */ 152*7836SJohn.Forte@Sun.COM 153*7836SJohn.Forte@Sun.COM #ifdef __cplusplus 154*7836SJohn.Forte@Sun.COM } 155*7836SJohn.Forte@Sun.COM #endif 156*7836SJohn.Forte@Sun.COM 157*7836SJohn.Forte@Sun.COM #endif /* _NSC_THREAD_H */ 158