Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

mut_method.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2004
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: mut_method.c,v 12.5 2005/10/07 20:21:34 ubell Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <string.h>
00016 #endif
00017 
00018 #include "db_int.h"
00019 #include "dbinc/mutex_int.h"
00020 
00021 /*
00022  * __mutex_alloc_pp --
00023  *      Allocate a mutex, application method.
00024  *
00025  * PUBLIC: int __mutex_alloc_pp __P((DB_ENV *, u_int32_t, db_mutex_t *));
00026  */
00027 int
00028 __mutex_alloc_pp(dbenv, flags, indxp)
00029         DB_ENV *dbenv;
00030         u_int32_t flags;
00031         db_mutex_t *indxp;
00032 {
00033         DB_THREAD_INFO *ip;
00034         int ret;
00035 
00036         PANIC_CHECK(dbenv);
00037 
00038         if (flags != 0 && flags != DB_MUTEX_SELF_BLOCK)
00039                 return (__db_ferr(dbenv, "DB_ENV->mutex_alloc", 0));
00040 
00041         ENV_ENTER(dbenv, ip);
00042         ret = __mutex_alloc(dbenv, MTX_APPLICATION, flags, indxp);
00043         ENV_LEAVE(dbenv, ip);
00044 
00045         return (ret);
00046 }
00047 
00048 /*
00049  * __mutex_free_pp --
00050  *      Destroy a mutex, application method.
00051  *
00052  * PUBLIC: int __mutex_free_pp __P((DB_ENV *, db_mutex_t));
00053  */
00054 int
00055 __mutex_free_pp(dbenv, indx)
00056         DB_ENV *dbenv;
00057         db_mutex_t indx;
00058 {
00059         DB_THREAD_INFO *ip;
00060         int ret;
00061 
00062         PANIC_CHECK(dbenv);
00063 
00064         if (indx == MUTEX_INVALID)
00065                 return (EINVAL);
00066 
00067         /*
00068          * Internally Berkeley DB passes around the db_mutex_t address on
00069          * free, because we want to make absolutely sure the slot gets
00070          * overwritten with MUTEX_INVALID.  We don't export MUTEX_INVALID,
00071          * so we don't export that part of the API, either.
00072          */
00073         ENV_ENTER(dbenv, ip);
00074         ret = __mutex_free(dbenv, &indx);
00075         ENV_LEAVE(dbenv, ip);
00076 
00077         return (ret);
00078 }
00079 
00080 /*
00081  * __mutex_lock --
00082  *      Lock a mutex, application method.
00083  *
00084  * PUBLIC: int __mutex_lock_pp __P((DB_ENV *, db_mutex_t));
00085  */
00086 int
00087 __mutex_lock_pp(dbenv, indx)
00088         DB_ENV *dbenv;
00089         db_mutex_t indx;
00090 {
00091         PANIC_CHECK(dbenv);
00092 
00093         if (indx == MUTEX_INVALID)
00094                 return (EINVAL);
00095 
00096         return (__mutex_lock(dbenv, indx));
00097 }
00098 
00099 /*
00100  * __mutex_unlock --
00101  *      Unlock a mutex, application method.
00102  *
00103  * PUBLIC: int __mutex_unlock_pp __P((DB_ENV *, db_mutex_t));
00104  */
00105 int
00106 __mutex_unlock_pp(dbenv, indx)
00107         DB_ENV *dbenv;
00108         db_mutex_t indx;
00109 {
00110         PANIC_CHECK(dbenv);
00111 
00112         if (indx == MUTEX_INVALID)
00113                 return (EINVAL);
00114 
00115         return (__mutex_unlock(dbenv, indx));
00116 }
00117 
00118 /*
00119  * __mutex_get_align --
00120  *      DB_ENV->mutex_get_align.
00121  *
00122  * PUBLIC: int __mutex_get_align __P((DB_ENV *, u_int32_t *));
00123  */
00124 int
00125 __mutex_get_align(dbenv, alignp)
00126         DB_ENV *dbenv;
00127         u_int32_t *alignp;
00128 {
00129         if (MUTEX_ON(dbenv))
00130                 *alignp = ((DB_MUTEXREGION *)((DB_MUTEXMGR *)
00131                     dbenv->mutex_handle)->reginfo.primary)->stat.st_mutex_align;
00132         else
00133                 *alignp = dbenv->mutex_align;
00134         return (0);
00135 }
00136 
00137 /*
00138  * __mutex_set_align --
00139  *      DB_ENV->mutex_set_align.
00140  *
00141  * PUBLIC: int __mutex_set_align __P((DB_ENV *, u_int32_t));
00142  */
00143 int
00144 __mutex_set_align(dbenv, align)
00145         DB_ENV *dbenv;
00146         u_int32_t align;
00147 {
00148         ENV_ILLEGAL_AFTER_OPEN(dbenv, "DB_ENV->set_mutex_align");
00149 
00150         if (align == 0 || !POWER_OF_TWO(align)) {
00151                 __db_err(dbenv,
00152     "DB_ENV->mutex_set_align: alignment value must be a non-zero power-of-two");
00153                 return (EINVAL);
00154         }
00155 
00156         dbenv->mutex_align = align;
00157         return (0);
00158 }
00159 
00160 /*
00161  * __mutex_get_increment --
00162  *      DB_ENV->mutex_get_increment.
00163  *
00164  * PUBLIC: int __mutex_get_increment __P((DB_ENV *, u_int32_t *));
00165  */
00166 int
00167 __mutex_get_increment(dbenv, incrementp)
00168         DB_ENV *dbenv;
00169         u_int32_t *incrementp;
00170 {
00171         /*
00172          * We don't maintain the increment in the region (it just makes
00173          * no sense).  Return whatever we have configured on this handle,
00174          * nobody is ever going to notice.
00175          */
00176         *incrementp = dbenv->mutex_inc;
00177         return (0);
00178 }
00179 
00180 /*
00181  * __mutex_set_increment --
00182  *      DB_ENV->mutex_set_increment.
00183  *
00184  * PUBLIC: int __mutex_set_increment __P((DB_ENV *, u_int32_t));
00185  */
00186 int
00187 __mutex_set_increment(dbenv, increment)
00188         DB_ENV *dbenv;
00189         u_int32_t increment;
00190 {
00191         ENV_ILLEGAL_AFTER_OPEN(dbenv, "DB_ENV->set_mutex_increment");
00192 
00193         dbenv->mutex_cnt = 0;
00194         dbenv->mutex_inc = increment;
00195         return (0);
00196 }
00197 
00198 /*
00199  * __mutex_get_max --
00200  *      DB_ENV->mutex_get_max.
00201  *
00202  * PUBLIC: int __mutex_get_max __P((DB_ENV *, u_int32_t *));
00203  */
00204 int
00205 __mutex_get_max(dbenv, maxp)
00206         DB_ENV *dbenv;
00207         u_int32_t *maxp;
00208 {
00209         if (MUTEX_ON(dbenv))
00210                 *maxp = ((DB_MUTEXREGION *)((DB_MUTEXMGR *)
00211                     dbenv->mutex_handle)->reginfo.primary)->stat.st_mutex_cnt;
00212         else
00213                 *maxp = dbenv->mutex_cnt;
00214         return (0);
00215 }
00216 
00217 /*
00218  * __mutex_set_max --
00219  *      DB_ENV->mutex_set_max.
00220  *
00221  * PUBLIC: int __mutex_set_max __P((DB_ENV *, u_int32_t));
00222  */
00223 int
00224 __mutex_set_max(dbenv, max)
00225         DB_ENV *dbenv;
00226         u_int32_t max;
00227 {
00228         ENV_ILLEGAL_AFTER_OPEN(dbenv, "DB_ENV->set_mutex_max");
00229 
00230         dbenv->mutex_cnt = max;
00231         dbenv->mutex_inc = 0;
00232         return (0);
00233 }
00234 
00235 /*
00236  * __mutex_get_tas_spins --
00237  *      DB_ENV->mutex_get_tas_spins.
00238  *
00239  * PUBLIC: int __mutex_get_tas_spins __P((DB_ENV *, u_int32_t *));
00240  */
00241 int
00242 __mutex_get_tas_spins(dbenv, tas_spinsp)
00243         DB_ENV *dbenv;
00244         u_int32_t *tas_spinsp;
00245 {
00246         if (MUTEX_ON(dbenv))
00247                 *tas_spinsp = ((DB_MUTEXREGION *)((DB_MUTEXMGR *)dbenv->
00248                     mutex_handle)->reginfo.primary)->stat.st_mutex_tas_spins;
00249         else
00250                 *tas_spinsp = dbenv->mutex_tas_spins;
00251         return (0);
00252 }
00253 
00254 /*
00255  * __mutex_set_tas_spins --
00256  *      DB_ENV->mutex_set_tas_spins.
00257  *
00258  * PUBLIC: int __mutex_set_tas_spins __P((DB_ENV *, u_int32_t));
00259  */
00260 int
00261 __mutex_set_tas_spins(dbenv, tas_spins)
00262         DB_ENV *dbenv;
00263         u_int32_t tas_spins;
00264 {
00265         /*
00266          * There's a theoretical race here, but I'm not interested in locking
00267          * the test-and-set spin count.  The worst possibility is a thread
00268          * reads out a bad spin count and spins until it gets the lock, but
00269          * that's awfully unlikely.
00270          */
00271         if (MUTEX_ON(dbenv))
00272                 ((DB_MUTEXREGION *)((DB_MUTEXMGR *)dbenv->mutex_handle)
00273                     ->reginfo.primary)->stat.st_mutex_tas_spins = tas_spins;
00274         else
00275                 dbenv->mutex_tas_spins = tas_spins;
00276         return (0);
00277 }

Generated on Sun Dec 25 12:14:41 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2