Print this page
usr/src/lib/libfakekernel/common/mutex.c
   1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  14  * Copyright 2017 RackTop Systems.
  15  */
  16 
  17 /*
  18  * mutex(9f)
  19  */
  20 
  21 /* This is the API we're emulating */
  22 #include <sys/mutex.h>
  23 
  24 #include <sys/errno.h>
  25 #include <sys/debug.h>
  26 #include <sys/thread.h>
  27 
  28 int     _lwp_mutex_lock(lwp_mutex_t *);
  29 int     _lwp_mutex_unlock(lwp_mutex_t *);
  30 int     _lwp_mutex_trylock(lwp_mutex_t *);
  31 
  32 extern clock_t ddi_get_lbolt(void);
  33 

  34 static const lwp_mutex_t default_mutex =
  35         {{0, 0, 0, {USYNC_THREAD}, _MUTEX_MAGIC},
  36         {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0};
  37 
  38 /* ARGSUSED */
  39 void
  40 kmutex_init(kmutex_t *mp, char *name, kmutex_type_t typ, void *arg)
  41 {
  42         mp->m_lock = default_mutex;
  43         mp->m_owner = _KTHREAD_INVALID;
  44 }
  45 
  46 /* ARGSUSED */
  47 void
  48 kmutex_destroy(kmutex_t *mp)
  49 {
  50         mp->m_owner = _KTHREAD_INVALID;
  51 }
  52 
  53 void
  54 kmutex_enter(kmutex_t *mp)
  55 {



  56         VERIFY(0 == _lwp_mutex_lock(&mp->m_lock));
  57         mp->m_owner = _curthread();
  58 }
  59 
  60 int
  61 mutex_tryenter(kmutex_t *mp)
  62 {
  63         int rc;
  64 
  65         rc = _lwp_mutex_trylock(&mp->m_lock);
  66         if (rc == 0) {
  67                 mp->m_owner = _curthread();
  68                 return (1);
  69         }
  70         return (0);
  71 }
  72 
  73 void
  74 kmutex_exit(kmutex_t *mp)
  75 {
  76         ASSERT(mp->m_owner == _curthread());
  77         mp->m_owner = _KTHREAD_INVALID;
   1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  14  * Copyright 2017 RackTop Systems.
  15  */
  16 
  17 /*
  18  * mutex(9f)
  19  */
  20 
  21 /* This is the API we're emulating */
  22 #include <sys/mutex.h>
  23 
  24 #include <sys/errno.h>
  25 #include <sys/debug.h>
  26 #include <sys/thread.h>
  27 
  28 int     _lwp_mutex_lock(lwp_mutex_t *);
  29 int     _lwp_mutex_unlock(lwp_mutex_t *);
  30 int     _lwp_mutex_trylock(lwp_mutex_t *);
  31 
  32 extern clock_t ddi_get_lbolt(void);
  33 
  34 /* See: head/synch.h ERRORCHECKMUTEX */
  35 static const lwp_mutex_t default_mutex =
  36         {{0, 0, 0, {USYNC_THREAD|LOCK_ERRORCHECK}, _MUTEX_MAGIC},
  37         {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0};
  38 
  39 /* ARGSUSED */
  40 void
  41 kmutex_init(kmutex_t *mp, char *name, kmutex_type_t typ, void *arg)
  42 {
  43         mp->m_lock = default_mutex;
  44         mp->m_owner = _KTHREAD_INVALID;
  45 }
  46 
  47 /* ARGSUSED */
  48 void
  49 kmutex_destroy(kmutex_t *mp)
  50 {
  51         mp->m_owner = _KTHREAD_INVALID;
  52 }
  53 
  54 void
  55 kmutex_enter(kmutex_t *mp)
  56 {
  57         kthread_t *t = _curthread();
  58 
  59         VERIFY(mp->m_owner != t);
  60         VERIFY(0 == _lwp_mutex_lock(&mp->m_lock));
  61         mp->m_owner = t;
  62 }
  63 
  64 int
  65 mutex_tryenter(kmutex_t *mp)
  66 {
  67         int rc;
  68 
  69         rc = _lwp_mutex_trylock(&mp->m_lock);
  70         if (rc == 0) {
  71                 mp->m_owner = _curthread();
  72                 return (1);
  73         }
  74         return (0);
  75 }
  76 
  77 void
  78 kmutex_exit(kmutex_t *mp)
  79 {
  80         ASSERT(mp->m_owner == _curthread());
  81         mp->m_owner = _KTHREAD_INVALID;