Print this page
XXXX semaphores behavior is inconsistent in panicstr case


   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 /*
  30  * This file contains the semaphore operations.
  31  */
  32 
  33 #include <sys/param.h>
  34 #include <sys/types.h>
  35 #include <sys/systm.h>
  36 #include <sys/schedctl.h>
  37 #include <sys/semaphore.h>
  38 #include <sys/sema_impl.h>
  39 #include <sys/t_lock.h>
  40 #include <sys/thread.h>
  41 #include <sys/proc.h>
  42 #include <sys/cmn_err.h>
  43 #include <sys/debug.h>
  44 #include <sys/disp.h>
  45 #include <sys/sobject.h>
  46 #include <sys/cpuvar.h>
  47 #include <sys/sleepq.h>
  48 #include <sys/sdt.h>


 214                 sema_queue(sp, t);
 215         } else
 216                 panic("sema_change_pri: %p not on sleep queue", (void *)t);
 217 }
 218 
 219 /*
 220  * the semaphore is granted when the semaphore's
 221  * count is greater than zero and blocks when equal
 222  * to zero.
 223  */
 224 void
 225 sema_p(ksema_t *sp)
 226 {
 227         sema_impl_t     *s;
 228         disp_lock_t     *sqlp;
 229 
 230         s = (sema_impl_t *)sp;
 231         sqlp = &SQHASH(s)->sq_lock;
 232         disp_lock_enter(sqlp);
 233         ASSERT(s->s_count >= 0);
 234         while (s->s_count == 0) {
 235                 if (panicstr) {
 236                         disp_lock_exit(sqlp);
 237                         return;
 238                 }

 239                 thread_lock_high(curthread);
 240                 SEMA_BLOCK(s, sqlp);
 241                 thread_unlock_nopreempt(curthread);
 242                 swtch();
 243                 disp_lock_enter(sqlp);
 244         }
 245         s->s_count--;
 246         disp_lock_exit(sqlp);
 247 }
 248 
 249 /*
 250  * similiar to sema_p except that it blocks at an interruptible
 251  * priority. if a signal is present then return 1 otherwise 0.
 252  */
 253 int
 254 sema_p_sig(ksema_t *sp)
 255 {
 256         kthread_t       *t = curthread;
 257         klwp_t          *lwp = ttolwp(t);
 258         int             cancel_pending;


 359                 disp_lock_exit(sqlp);
 360         }
 361 }
 362 
 363 /*
 364  * try to acquire the semaphore. if the semaphore is greater than
 365  * zero, then the semaphore is granted and returns 1. otherwise
 366  * return 0.
 367  */
 368 int
 369 sema_tryp(ksema_t *sp)
 370 {
 371         sema_impl_t     *s;
 372         sleepq_head_t   *sqh;
 373 
 374         int     gotit = 0;
 375 
 376         s = (sema_impl_t *)sp;
 377         sqh = SQHASH(s);
 378         disp_lock_enter(&sqh->sq_lock);




 379         if (s->s_count > 0) {
 380                 s->s_count--;
 381                 gotit = 1;
 382         }
 383         disp_lock_exit(&sqh->sq_lock);
 384         return (gotit);
 385 }
 386 
 387 int
 388 sema_held(ksema_t *sp)
 389 {
 390         sema_impl_t     *s;
 391 
 392 
 393         s = (sema_impl_t *)sp;
 394         if (panicstr)
 395                 return (1);
 396         else
 397                 return (s->s_count <= 0);
 398 }


   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 


  27 /*
  28  * This file contains the semaphore operations.
  29  */
  30 
  31 #include <sys/param.h>
  32 #include <sys/types.h>
  33 #include <sys/systm.h>
  34 #include <sys/schedctl.h>
  35 #include <sys/semaphore.h>
  36 #include <sys/sema_impl.h>
  37 #include <sys/t_lock.h>
  38 #include <sys/thread.h>
  39 #include <sys/proc.h>
  40 #include <sys/cmn_err.h>
  41 #include <sys/debug.h>
  42 #include <sys/disp.h>
  43 #include <sys/sobject.h>
  44 #include <sys/cpuvar.h>
  45 #include <sys/sleepq.h>
  46 #include <sys/sdt.h>


 212                 sema_queue(sp, t);
 213         } else
 214                 panic("sema_change_pri: %p not on sleep queue", (void *)t);
 215 }
 216 
 217 /*
 218  * the semaphore is granted when the semaphore's
 219  * count is greater than zero and blocks when equal
 220  * to zero.
 221  */
 222 void
 223 sema_p(ksema_t *sp)
 224 {
 225         sema_impl_t     *s;
 226         disp_lock_t     *sqlp;
 227 
 228         s = (sema_impl_t *)sp;
 229         sqlp = &SQHASH(s)->sq_lock;
 230         disp_lock_enter(sqlp);
 231         ASSERT(s->s_count >= 0);

 232         if (panicstr) {
 233                 disp_lock_exit(sqlp);
 234                 return;
 235         }
 236         while (s->s_count == 0) {
 237                 thread_lock_high(curthread);
 238                 SEMA_BLOCK(s, sqlp);
 239                 thread_unlock_nopreempt(curthread);
 240                 swtch();
 241                 disp_lock_enter(sqlp);
 242         }
 243         s->s_count--;
 244         disp_lock_exit(sqlp);
 245 }
 246 
 247 /*
 248  * similiar to sema_p except that it blocks at an interruptible
 249  * priority. if a signal is present then return 1 otherwise 0.
 250  */
 251 int
 252 sema_p_sig(ksema_t *sp)
 253 {
 254         kthread_t       *t = curthread;
 255         klwp_t          *lwp = ttolwp(t);
 256         int             cancel_pending;


 357                 disp_lock_exit(sqlp);
 358         }
 359 }
 360 
 361 /*
 362  * try to acquire the semaphore. if the semaphore is greater than
 363  * zero, then the semaphore is granted and returns 1. otherwise
 364  * return 0.
 365  */
 366 int
 367 sema_tryp(ksema_t *sp)
 368 {
 369         sema_impl_t     *s;
 370         sleepq_head_t   *sqh;
 371 
 372         int     gotit = 0;
 373 
 374         s = (sema_impl_t *)sp;
 375         sqh = SQHASH(s);
 376         disp_lock_enter(&sqh->sq_lock);
 377         if (panicstr) {
 378                 disp_lock_exit(sqlp);
 379                 return (1);
 380         }
 381         if (s->s_count > 0) {
 382                 s->s_count--;
 383                 gotit = 1;
 384         }
 385         disp_lock_exit(&sqh->sq_lock);
 386         return (gotit);
 387 }
 388 
 389 int
 390 sema_held(ksema_t *sp)
 391 {
 392         sema_impl_t     *s;
 393 
 394 
 395         s = (sema_impl_t *)sp;
 396         if (panicstr)
 397                 return (1);
 398         else
 399                 return (s->s_count <= 0);
 400 }