<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cradicle Explorer</title>
    <link href="/css/bootstrap/bootstrap.min.css" rel="stylesheet">
    <style>
      .form-control-dark::placeholder {
          color: #aaa;
          opacity: 1;
      }
    </style>
    <link rel="stylesheet" href="/assets/fontawesome/css/all.min.css">
    <link rel="icon" type="image/png" href="/favicon.png">


                <link href="/css/dashboard.css" rel="stylesheet">
                </head>
                <body>
                <header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
                  <a class="navbar-brand col-md-3 col-lg-2 me-0 px-3 fs-6" href="/">Cradicle Explorer</a>
                  <button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                  </button>
                  <form method="get" action="/cgi-bin/main" style="width:100%;"><input class="form-control form-control-dark w-100 rounded-0 border-0" type="text" name="q" placeholder="Search repos" aria-label="Search"></form>
                  <div class="navbar-nav flex-row">
                    <div class="nav-item text-nowrap">
                      <a class="nav-link px-3 active" href="/cgi-bin/repo?id=z3U39rx694kJom2vhQFRe2c9YgGMU">sharded_mutex</a>
                    </div>
                  </div>
                </header>
                <div class="container-fluid">
                  <div class="row">
                    <nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-dark sidebar collapse">
                      <div class="position-sticky pt-3 sidebar-sticky">
                        <ul class="nav flex-column">
                          <li class="nav-item">
                            <a class="nav-link active" href="/cgi-bin/repo?id=z3U39rx694kJom2vhQFRe2c9YgGMU">
                              <i class="align-text-bottom fa-solid fa-info"></i>
                              Info
                            </a>
                          </li>
                          <li class="nav-item">
                            <a class="nav-link" href="/cgi-bin/repo?id=z3U39rx694kJom2vhQFRe2c9YgGMU&issue=list">
                              <i class="align-text-bottom fa-solid fa-layer-group"></i>
                              Issues
                            </a>
                          </li>
                          <li class="nav-item">
                            <a class="nav-link" href="/cgi-bin/repo?id=z3U39rx694kJom2vhQFRe2c9YgGMU&patch=list">
                              <i class="align-text-bottom fa-solid fa-vest-patches"></i>
                              Patches
                            </a>
                          </li>
                          <li class="nav-item">
                            <a class="nav-link" href="/cgi-bin/repo?id=z3U39rx694kJom2vhQFRe2c9YgGMU&wallet=list">
                              <i class="align-text-bottom fa-solid fa-wallet"></i>
                              Wallets
                            </a>
                          </li>
                          <li class="nav-item">
                            <a class="nav-link" href="/cgi-bin/repo?id=z3U39rx694kJom2vhQFRe2c9YgGMU&source=.">
                              <i class="align-text-bottom fa-solid fa-code"></i>
                              Source
                            </a>
                          </li>
                        <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted text-uppercase">
                          <span></span>
                        </h6>
                        <ul class="nav flex-column mb-2">
                        
                        </ul>
                      </div>
                    </nav>
                <main class="col-md-9 ms-sm-auto col-lg-10">
                  <div class="container px-1 py-3">
        

    <div class="list-group">
    <div class="list-group-item">
    <div style="font-size:1.3rem;">sharded_mutex</div>
    <div class="repo-item"></div>
    <div>rad:z3U39rx694kJom2vhQFRe2c9YgGMU</div>
    </div>
    <div class="list-group-item">
    <div>Visibility</div>
    <div class="repo-item">public</div>
    </div>
    <div class="list-group-item">
    <div>Delegates</div><div class="repo-item">did:key:z6MksaJkZuQj49mbrft8JNvo3vBDU5AC5owW1EnCZJJ3goRX</div>
    </div>
    <div class="list-group-item">
    <div>Default branch</div>
    <div><span class="repo-item">master &#8594 8f2d3c77f8eae5297b1dd2d677d1a641c5acc21b</span> (Tue Oct 22 01:07:56 2024)</div>
    </div>
    <div class="list-group-item">
    <div>Threshold</div>
    <div class="repo-item">1</div>
    </div>
    </div>
    
        <div class="list-group mt-3">
        <div class="list-group-item">
        <div class="mb-2" style="font-weight:bold;"><i class="fa-solid fa-book"></i> README.md</div>
        <pre style="margin:0; font-size:0.85rem; overflow-x:auto; color:#fafafa;"># `ShardedMutex`, atomic Everything

This library provides global locks for (pseudo-) atomic access to data without memory overhead
per object. Concurrency is improved by selecting a Mutex from a pool based on the Address of
the object to be locked.

There is one pool of mutexes per guarded type, thus it is possible to lock values of different
types at the same time.

* Being sharded, these Mutexes act still as global and non-recursive locks. One must not
  `lock()` another object while a lock on the same type/domain is already hold, otherwise deadlocks
  will happen. The `try_lock()` and `try_lock_for()` methods do not have this limitation but
  will fail when the lock is already hold.
* The `multi_lock()` methods allow to obtain locks on multiple objects of the same type at the same
  time.
* The `then_lock()` method implements hand-over-hand locking where the lock of a new object is
  obtained before an lock already hold is dropped.

Same types may have different locking domains using type tags.

Provides pseudo atomic access for types that implement `Copy` and `PartialEq`. These can never
deadlock because they are always leaf locks.

In debug builds a deadlock detector is active which will panic when one tries to lock objects
from the same type/domain while already holding a lock.

**Example usage:**
```
use sharded_mutex::ShardedMutex;

// create 2 values that need locking
let x = ShardedMutex::new(123);
let y = ShardedMutex::new(234);

// a single lock
assert_eq!(*x.lock(), 123);

// Multiple locks
let mut guards = ShardedMutex::multi_lock([&amp;x, &amp;y]);

assert_eq!(*guards[0], 123);
assert_eq!(*guards[1], 234);

// can write as well
*guards[1] = 456;

// unlocks
drop(guards);

// lock again
assert_eq!(*y.lock(), 456);

// Pseudo atomic access
use sharded_mutex::PseudoAtomicOps;

x.store(&amp;234);
assert_eq!(x.load(), 234);

let mut swapping = 345;
x.swap(&amp;mut swapping);
assert_eq!(swapping, 234);
assert_eq!(x.load(), 345);

assert!(!x.compare_and_set(&amp;123, &amp;456));
assert!(x.compare_and_set(&amp;345, &amp;456));
assert_eq!(x.load(), 456);
```


# Features

## Alignment

`ShardedMutex` using arrays of mutexes for locking objects. This would pack Mutexes for
unrelated objects pretty close together which in turn impacts performance because of false
cache sharing. To alleviate this problem the internal aligment of these mutexes can be
increased. The cost for this is a larger memory footprint.

### *`align_none`*

Packs Mutexes as tight as possible. Good for embedded systems that have only little caches or
none at all and memory is premium.

### *`align_narrow`*

This is the **default**, it places 8 Mutexes per cacheline which should be a good compromise
between space and performance.

### *`align_wide`*

Places 4 mutexes per cacheline, should improve performance even further. Probably only
necessary when it is proven that there is cache contention.

### *`align_cacheline`*

Places one mutex per cacheline. This should give the best performance without any
cache contention, on the cost of wasting a lot memory.


## Pool Sizes

Locking performs best when there is little contention on the mutexes. We do this by sharding
accesses over pools of mutexes. The size of these pools can be adjusted to the expected number
of threads that will access the mutexes concurrently. The pool sizes are powers of two minus
one (or even mersenne-primes) for spreading the load evenly over the mutexes.

### *`normal_pool_size`*

Mutex pools have 127 entries. This should be good enough for most applications. This is the
default.

### *`tiny_pool_size`*

Mutex pools have 7 entries. This serverly limits concurrency. Use it only when memory is
at premium (embedded) and only very few threads try to lock objects.

### *`small_pool_size`*

Mutex pools have 31 entries. This may limit concurrency. Use it only when memory is
at premium (embedded) or only few threads try to lock objects.

### *`big_pool_size`*

Mutex pools have 511 entries. To be used for highly concurrent systems with many cores
where hundreds of threads locking objects concurrently.

### *`huge_pool_size`*

Mutex pools have 8191 entries. To be used for massively concurrent systems with many cores
where hundreds to thousands of threads locking objects concurrently.
</pre>
        </div>
        </div>

</div>
</main>
</div>
</div>


</body>
</html>

