2022-05-27 13:25:46 -07:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
2020-06-15 07:18:57 -07:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2022-05-27 13:25:46 -07:00
|
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
2020-06-15 07:18:57 -07:00
|
|
|
│ │
|
2020-12-27 17:18:44 -08:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 07:18:57 -07:00
|
|
|
│ │
|
2020-12-27 17:18:44 -08:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 07:18:57 -07:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-05-27 13:25:46 -07:00
|
|
|
#include "libc/calls/calls.h"
|
2022-10-16 12:05:08 -07:00
|
|
|
#include "libc/calls/state.internal.h"
|
2022-06-12 19:33:42 -07:00
|
|
|
#include "libc/errno.h"
|
2022-09-05 08:26:03 -07:00
|
|
|
#include "libc/intrin/atomic.h"
|
2022-10-16 12:05:08 -07:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-09-11 11:02:07 -07:00
|
|
|
#include "libc/intrin/weaken.h"
|
2022-10-16 12:05:08 -07:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-09-10 02:56:25 -07:00
|
|
|
#include "libc/thread/thread.h"
|
2022-09-11 11:02:07 -07:00
|
|
|
#include "libc/thread/tls.h"
|
|
|
|
#include "third_party/nsync/mu.h"
|
2022-06-19 01:13:03 -07:00
|
|
|
|
2022-06-11 01:59:26 -07:00
|
|
|
/**
|
2022-06-12 19:33:42 -07:00
|
|
|
* Locks mutex.
|
2022-06-19 01:13:03 -07:00
|
|
|
*
|
2022-09-05 08:26:03 -07:00
|
|
|
* Here's an example of using a normal mutex:
|
|
|
|
*
|
|
|
|
* pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
* pthread_mutex_lock(&lock);
|
|
|
|
* // do work...
|
|
|
|
* pthread_mutex_unlock(&lock);
|
|
|
|
* pthread_mutex_destroy(&lock);
|
|
|
|
*
|
|
|
|
* Cosmopolitan permits succinct notation for normal mutexes:
|
|
|
|
*
|
|
|
|
* pthread_mutex_t lock = {0};
|
|
|
|
* pthread_mutex_lock(&lock);
|
|
|
|
* // do work...
|
|
|
|
* pthread_mutex_unlock(&lock);
|
|
|
|
*
|
|
|
|
* Here's an example of the proper way to do recursive mutexes:
|
|
|
|
*
|
|
|
|
* pthread_mutex_t lock;
|
|
|
|
* pthread_mutexattr_t attr;
|
|
|
|
* pthread_mutexattr_init(&attr);
|
|
|
|
* pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
* pthread_mutex_init(&lock, &attr);
|
|
|
|
* pthread_mutexattr_destroy(&attr);
|
|
|
|
* pthread_mutex_lock(&lock);
|
|
|
|
* // do work...
|
|
|
|
* pthread_mutex_unlock(&lock);
|
|
|
|
* pthread_mutex_destroy(&lock);
|
|
|
|
*
|
2022-10-16 12:05:08 -07:00
|
|
|
* This function does nothing in vfork() children.
|
|
|
|
*
|
2022-06-12 19:33:42 -07:00
|
|
|
* @return 0 on success, or error number on failure
|
2022-09-07 21:13:50 -07:00
|
|
|
* @see pthread_spin_lock()
|
2022-10-16 12:05:08 -07:00
|
|
|
* @vforksafe
|
2022-06-11 01:59:26 -07:00
|
|
|
*/
|
2022-09-05 13:06:34 -07:00
|
|
|
int pthread_mutex_lock(pthread_mutex_t *mutex) {
|
2022-09-10 09:14:40 -07:00
|
|
|
int c, d, t;
|
|
|
|
|
2022-10-16 12:05:08 -07:00
|
|
|
if (__vforked) return 0;
|
|
|
|
|
|
|
|
LOCKTRACE("pthread_mutex_lock(%t)", mutex);
|
|
|
|
|
2022-09-13 14:57:38 -07:00
|
|
|
if (__tls_enabled && //
|
|
|
|
mutex->_type == PTHREAD_MUTEX_NORMAL && //
|
|
|
|
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
|
|
|
|
_weaken(nsync_mu_lock)) {
|
2022-09-12 23:10:38 -07:00
|
|
|
_weaken(nsync_mu_lock)((nsync_mu *)mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mutex->_type == PTHREAD_MUTEX_NORMAL) {
|
|
|
|
while (atomic_exchange_explicit(&mutex->_lock, 1, memory_order_acquire)) {
|
|
|
|
pthread_yield();
|
|
|
|
}
|
2022-09-10 09:14:40 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = gettid();
|
2022-09-13 14:57:38 -07:00
|
|
|
if (mutex->_owner == t) {
|
|
|
|
if (mutex->_type != PTHREAD_MUTEX_ERRORCHECK) {
|
2023-01-03 05:01:00 -08:00
|
|
|
if (mutex->_depth < 63) {
|
2022-09-13 14:57:38 -07:00
|
|
|
++mutex->_depth;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return EAGAIN;
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-10 09:14:40 -07:00
|
|
|
return EDEADLK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 14:57:38 -07:00
|
|
|
while (atomic_exchange_explicit(&mutex->_lock, 1, memory_order_acquire)) {
|
|
|
|
pthread_yield();
|
2022-06-11 01:59:26 -07:00
|
|
|
}
|
2022-09-10 09:14:40 -07:00
|
|
|
|
2022-09-13 14:57:38 -07:00
|
|
|
mutex->_depth = 0;
|
|
|
|
mutex->_owner = t;
|
2022-10-16 12:05:08 -07:00
|
|
|
mutex->_pid = __pid;
|
2022-09-13 14:57:38 -07:00
|
|
|
|
2022-09-10 09:14:40 -07:00
|
|
|
return 0;
|
2022-05-27 13:25:46 -07:00
|
|
|
}
|