Skip to content

Commit 55b7aa1

Browse files
committed
Allow user to override pthread mutex and cond
1 parent 4705705 commit 55b7aa1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+216
-102
lines changed

libc/calls/fcntl-nt.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "libc/sysv/consts/fio.h"
5252
#include "libc/sysv/consts/o.h"
5353
#include "libc/sysv/errfuns.h"
54+
#include "libc/thread/posixthread.internal.h"
5455
#include "libc/thread/thread.h"
5556

5657
struct FileLock {
@@ -67,7 +68,9 @@ struct FileLocks {
6768
struct FileLock *free;
6869
};
6970

70-
static struct FileLocks g_locks;
71+
static struct FileLocks g_locks = {
72+
.mu = PTHREAD_MUTEX_INITIALIZER,
73+
};
7174

7275
static textwindows struct FileLock *NewFileLock(void) {
7376
struct FileLock *fl;
@@ -110,7 +113,7 @@ static textwindows bool EqualsFileLock(struct FileLock *fl, int64_t off,
110113

111114
textwindows void sys_fcntl_nt_lock_cleanup(int fd) {
112115
struct FileLock *fl, *ft, **flp;
113-
pthread_mutex_lock(&g_locks.mu);
116+
_pthread_mutex_lock(&g_locks.mu);
114117
for (flp = &g_locks.list, fl = *flp; fl;) {
115118
if (fl->fd == fd) {
116119
*flp = fl->next;
@@ -122,7 +125,7 @@ textwindows void sys_fcntl_nt_lock_cleanup(int fd) {
122125
fl = *flp;
123126
}
124127
}
125-
pthread_mutex_unlock(&g_locks.mu);
128+
_pthread_mutex_unlock(&g_locks.mu);
126129
}
127130

128131
static textwindows int64_t GetfileSize(int64_t handle) {
@@ -353,9 +356,9 @@ textwindows int sys_fcntl_nt(int fd, int cmd, uintptr_t arg) {
353356
} else if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK) {
354357
struct Fd *f = g_fds.p + fd;
355358
if (f->cursor) {
356-
pthread_mutex_lock(&g_locks.mu);
359+
_pthread_mutex_lock(&g_locks.mu);
357360
rc = sys_fcntl_nt_lock(f, fd, cmd, arg);
358-
pthread_mutex_unlock(&g_locks.mu);
361+
_pthread_mutex_unlock(&g_locks.mu);
359362
} else {
360363
rc = ebadf();
361364
}

libc/calls/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ int64_t GetConsoleOutputHandle(void);
3434
void EchoConsoleNt(const char *, size_t, bool);
3535
int IsWindowsExecutable(int64_t, const char16_t *);
3636
void InterceptTerminalCommands(const char *, size_t);
37+
void sys_read_nt_wipe_keystrokes(void);
3738

3839
forceinline bool __isfdopen(int fd) {
3940
return 0 <= fd && fd < g_fds.n && g_fds.p[fd].kind != kFdEmpty;

libc/calls/read-nt.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,15 @@ struct Keystrokes {
136136
struct Keystroke pool[512];
137137
};
138138

139-
static struct Keystrokes __keystroke;
139+
static struct Keystrokes __keystroke = {
140+
.lock = PTHREAD_MUTEX_INITIALIZER,
141+
};
140142

141-
textwindows void WipeKeystrokes(void) {
143+
textwindows void sys_read_nt_wipe_keystrokes(void) {
144+
pthread_mutex_t lock = __keystroke.lock;
142145
bzero(&__keystroke, sizeof(__keystroke));
146+
__keystroke.lock = lock;
147+
_pthread_mutex_wipe_np(&__keystroke.lock);
143148
}
144149

145150
textwindows static void FreeKeystrokeImpl(struct Dll *key) {
@@ -191,11 +196,11 @@ textwindows static void InitConsole(void) {
191196
}
192197

193198
textwindows static void LockKeystrokes(void) {
194-
pthread_mutex_lock(&__keystroke.lock);
199+
_pthread_mutex_lock(&__keystroke.lock);
195200
}
196201

197202
textwindows static void UnlockKeystrokes(void) {
198-
pthread_mutex_unlock(&__keystroke.lock);
203+
_pthread_mutex_unlock(&__keystroke.lock);
199204
}
200205

201206
textwindows int64_t GetConsoleInputHandle(void) {

libc/dlopen/dlopen.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "libc/sysv/consts/prot.h"
5858
#include "libc/sysv/errfuns.h"
5959
#include "libc/temp.h"
60+
#include "libc/thread/posixthread.internal.h"
6061
#include "libc/thread/thread.h"
6162
#include "libc/thread/tls.h"
6263

@@ -131,6 +132,8 @@ struct {
131132

132133
long __sysv2nt14();
133134
long foreign_tramp();
135+
void __dlopen_lock(void);
136+
void __dlopen_unlock(void);
134137

135138
static _Thread_local char dlerror_buf[128];
136139

@@ -435,14 +438,13 @@ static dontinline char *foreign_alloc_block(void) {
435438
static dontinline void *foreign_alloc(size_t n) {
436439
void *res;
437440
static char *block;
438-
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
439-
pthread_mutex_lock(&lock);
441+
__dlopen_lock();
440442
if (!block || READ32LE(block) + n > 65536)
441443
if (!(block = foreign_alloc_block()))
442444
return 0;
443445
res = block + READ32LE(block);
444446
WRITE32LE(block, READ32LE(block) + n);
445-
pthread_mutex_unlock(&lock);
447+
__dlopen_unlock();
446448
return res;
447449
}
448450

libc/intrin/cursor.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020
#include "libc/intrin/atomic.h"
2121
#include "libc/intrin/fds.h"
2222
#include "libc/runtime/runtime.h"
23+
#include "libc/thread/posixthread.internal.h"
2324

2425
struct Cursor *__cursor_new(void) {
2526
struct Cursor *c;
2627
if ((c = _mapanon(sizeof(struct Cursor)))) {
2728
if ((c->shared = _mapshared(sizeof(struct CursorShared)))) {
28-
pthread_mutexattr_t attr;
29-
pthread_mutexattr_init(&attr);
30-
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
31-
pthread_mutex_init(&c->shared->lock, &attr);
32-
pthread_mutexattr_destroy(&attr);
29+
c->shared->lock = (pthread_mutex_t)PTHREAD_SHARED_MUTEX_INITIALIZER_NP;
3330
} else {
3431
munmap(c, sizeof(struct Cursor));
3532
c = 0;
@@ -56,9 +53,9 @@ int __cursor_unref(struct Cursor *c) {
5653
}
5754

5855
void __cursor_lock(struct Cursor *c) {
59-
pthread_mutex_lock(&c->shared->lock);
56+
_pthread_mutex_lock(&c->shared->lock);
6057
}
6158

6259
void __cursor_unlock(struct Cursor *c) {
63-
pthread_mutex_unlock(&c->shared->lock);
60+
_pthread_mutex_unlock(&c->shared->lock);
6461
}

libc/intrin/cxalock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/intrin/cxaatexit.h"
20+
#include "libc/thread/posixthread.internal.h"
2021
#include "libc/thread/thread.h"
2122

2223
pthread_mutex_t __cxa_lock_obj = PTHREAD_MUTEX_INITIALIZER;
2324

2425
void __cxa_lock(void) {
25-
pthread_mutex_lock(&__cxa_lock_obj);
26+
_pthread_mutex_lock(&__cxa_lock_obj);
2627
}
2728

2829
void __cxa_unlock(void) {
29-
pthread_mutex_unlock(&__cxa_lock_obj);
30+
_pthread_mutex_unlock(&__cxa_lock_obj);
3031
}

libc/intrin/dlopen.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2024 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/thread/posixthread.internal.h"
20+
#include "libc/thread/thread.h"
21+
22+
pthread_mutex_t __dlopen_lock_obj = PTHREAD_MUTEX_INITIALIZER;
23+
24+
void __dlopen_lock(void) {
25+
_pthread_mutex_lock(&__dlopen_lock_obj);
26+
}
27+
28+
void __dlopen_unlock(void) {
29+
_pthread_mutex_unlock(&__dlopen_lock_obj);
30+
}

libc/intrin/fds_lock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/state.internal.h"
20+
#include "libc/thread/posixthread.internal.h"
2021
#include "libc/thread/thread.h"
2122

2223
void __fds_lock(void) {
23-
pthread_mutex_lock(&__fds_lock_obj);
24+
_pthread_mutex_lock(&__fds_lock_obj);
2425
}
2526

2627
void __fds_unlock(void) {
27-
pthread_mutex_unlock(&__fds_lock_obj);
28+
_pthread_mutex_unlock(&__fds_lock_obj);
2829
}

libc/intrin/itimer.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,26 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/thread/itimer.h"
2020
#include "libc/str/str.h"
21+
#include "libc/thread/posixthread.internal.h"
2122

2223
struct IntervalTimer __itimer = {
2324
.lock = PTHREAD_MUTEX_INITIALIZER,
2425
.cond = PTHREAD_COND_INITIALIZER,
2526
};
2627

2728
textwindows void __itimer_lock(void) {
28-
pthread_mutex_lock(&__itimer.lock);
29+
_pthread_mutex_lock(&__itimer.lock);
2930
}
3031

3132
textwindows void __itimer_unlock(void) {
32-
pthread_mutex_unlock(&__itimer.lock);
33+
_pthread_mutex_unlock(&__itimer.lock);
3334
}
3435

3536
textwindows void __itimer_wipe_and_reset(void) {
3637
// timers aren't inherited by forked subprocesses
3738
bzero(&__itimer.it, sizeof(__itimer.it));
38-
pthread_mutex_wipe_np(&__itimer.lock);
39-
pthread_cond_init(&__itimer.cond, 0);
39+
_pthread_mutex_wipe_np(&__itimer.lock);
40+
bzero(&__itimer.cond, sizeof(__itimer.cond));
4041
__itimer.thread = 0;
4142
__itimer.once = 0;
4243
}

libc/intrin/localtime_lock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/thread/posixthread.internal.h"
1920
#include "third_party/tz/lock.h"
2021

2122
pthread_mutex_t __localtime_lock_obj = PTHREAD_MUTEX_INITIALIZER;
2223

2324
void __localtime_lock(void) {
24-
pthread_mutex_lock(&__localtime_lock_obj);
25+
_pthread_mutex_lock(&__localtime_lock_obj);
2526
}
2627

2728
void __localtime_unlock(void) {
28-
pthread_mutex_unlock(&__localtime_lock_obj);
29+
_pthread_mutex_unlock(&__localtime_lock_obj);
2930
}

0 commit comments

Comments
 (0)