jcs's openbsd hax
openbsd
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

replace the flockfile backend with a per FILE recursive mutex.

the flockfile implementation in thread/rthread_file.c used an
external lock, and associated it with the relevant FILE * as needed.
this isn't great for a lot of reasons, complexity being the big
one, but the straw that broke the camels back is that it uses a
single spinlock to coordinate all of this, which in turn generates
a lot of sched_yield syscalls.

this avoids all the code complexity and the spinlock by just embedding
a small __rctmx in every FILE.

tested by and ok tb@ jca@
ok claudio@

dlg 906b2f4e bec9d308

+41 -22
+3 -1
lib/libc/hidden/_stdio.h
··· 1 - /* $OpenBSD: _stdio.h,v 1.2 2025/07/23 08:17:23 yasuoka Exp $ */ 1 + /* $OpenBSD: _stdio.h,v 1.3 2025/08/04 01:44:32 dlg Exp $ */ 2 2 /* $NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $ */ 3 3 4 4 /*- ··· 37 37 38 38 #ifndef _LIBC__STDIO_H_ 39 39 #define _LIBC__STDIO_H_ 40 + 41 + #include "thread_private.h" 40 42 41 43 /* 42 44 * NB: to fit things in six character monocase externals, the stdio
+2 -1
lib/libc/hidden/stdio.h
··· 1 - /* $OpenBSD: stdio.h,v 1.9 2025/07/16 15:33:05 yasuoka Exp $ */ 1 + /* $OpenBSD: stdio.h,v 1.10 2025/08/04 01:44:32 dlg Exp $ */ 2 2 /* 3 3 * Copyright (c) 2015 Philip Guenther <guenther@openbsd.org> 4 4 * ··· 35 35 36 36 __BEGIN_HIDDEN_DECLS 37 37 int __cleanfile(FILE *, int _doclose); 38 + void __relefile(FILE *); 38 39 char *_mktemp(char *); 39 40 __END_HIDDEN_DECLS 40 41
+7 -1
lib/libc/include/thread_private.h
··· 1 - /* $OpenBSD: thread_private.h,v 1.39 2025/07/16 16:22:58 deraadt Exp $ */ 1 + /* $OpenBSD: thread_private.h,v 1.40 2025/08/04 01:44:33 dlg Exp $ */ 2 2 3 3 /* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */ 4 4 ··· 382 382 struct __cmtx mtx; 383 383 unsigned int depth; 384 384 }; 385 + 386 + #define __RCMTX_INITIALIZER() { \ 387 + .owner = NULL, \ 388 + .mtx = __CMTX_INITIALIZER(), \ 389 + .depth = 0, \ 390 + } 385 391 386 392 struct pthread_mutex_attr { 387 393 int ma_type;
+8 -2
lib/libc/stdio/fclose.c
··· 1 - /* $OpenBSD: fclose.c,v 1.14 2025/07/16 15:33:05 yasuoka Exp $ */ 1 + /* $OpenBSD: fclose.c,v 1.15 2025/08/04 01:44:33 dlg Exp $ */ 2 2 /*- 3 3 * Copyright (c) 1990, 1993 The Regents of the University of California. 4 4 * Copyright (c) 2013 Mariusz Zaborski <oshogbo@FreeBSD.org> ··· 52 52 if (HASLB(fp)) 53 53 FREELB(fp); 54 54 fp->_r = fp->_w = 0; /* Mess up if reaccessed. */ 55 + return r; 56 + } 57 + 58 + void 59 + __relefile(FILE *fp) 60 + { 55 61 fp->_flags = 0; /* Release this FILE for reuse. */ 56 - return r; 57 62 } 58 63 59 64 int ··· 68 73 FLOCKFILE(fp); 69 74 r = __cleanfile(fp, 1); 70 75 FUNLOCKFILE(fp); 76 + __relefile(fp); 71 77 return (r); 72 78 } 73 79 DEF_STRONG(fclose);
+2 -1
lib/libc/stdio/fdclose.c
··· 1 - /* $OpenBSD: fdclose.c,v 1.1 2025/07/16 15:33:05 yasuoka Exp $ */ 1 + /* $OpenBSD: fdclose.c,v 1.2 2025/08/04 01:44:33 dlg Exp $ */ 2 2 /*- 3 3 * Copyright (c) 1990, 1993 The Regents of the University of California. 4 4 * Copyright (c) 2013 Mariusz Zaborski <oshogbo@FreeBSD.org> ··· 67 67 r = __cleanfile(fp, 0); 68 68 } 69 69 FUNLOCKFILE(fp); 70 + __relefile(fp); 70 71 71 72 return r; 72 73 }
+3 -1
lib/libc/stdio/fileext.h
··· 1 - /* $OpenBSD: fileext.h,v 1.2 2005/06/17 20:40:32 espie Exp $ */ 1 + /* $OpenBSD: fileext.h,v 1.3 2025/08/04 01:44:33 dlg Exp $ */ 2 2 /* $NetBSD: fileext.h,v 1.5 2003/07/18 21:46:41 nathanw Exp $ */ 3 3 4 4 /*- ··· 35 35 struct __sfileext { 36 36 struct __sbuf _ub; /* ungetc buffer */ 37 37 struct wchar_io_data _wcio; /* wide char io status */ 38 + struct __rcmtx _lock; 38 39 }; 39 40 40 41 #define _EXT(fp) ((struct __sfileext *)((fp)->_ext._base)) ··· 45 46 _UB(fp)._base = NULL; \ 46 47 _UB(fp)._size = 0; \ 47 48 WCIO_INIT(fp); \ 49 + __rcmtx_init(&_EXT(fp)->_lock); \ 48 50 } while (0) 49 51 50 52 #define _FILEEXT_SETUP(f, fext) \
+7 -2
lib/libc/stdio/findfp.c
··· 1 - /* $OpenBSD: findfp.c,v 1.21 2025/07/16 15:33:05 yasuoka Exp $ */ 1 + /* $OpenBSD: findfp.c,v 1.22 2025/08/04 01:44:33 dlg Exp $ */ 2 2 /*- 3 3 * Copyright (c) 1990, 1993 4 4 * The Regents of the University of California. All rights reserved. ··· 60 60 static struct glue *lastglue = &uglue; 61 61 static void *sfp_mutex; 62 62 63 - static struct __sfileext __sFext[3]; 63 + static struct __sfileext __sFext[3] = { 64 + { ._lock = __RCMTX_INITIALIZER() }, 65 + { ._lock = __RCMTX_INITIALIZER() }, 66 + { ._lock = __RCMTX_INITIALIZER() }, 67 + }; 68 + 64 69 /* 65 70 * These are separate variables because they may end up copied 66 71 * into program images via COPY relocations, so their addresses
+3 -3
lib/libc/stdio/flockfile.c
··· 1 - /* $OpenBSD: flockfile.c,v 1.9 2016/05/07 19:05:22 guenther Exp $ */ 1 + /* $OpenBSD: flockfile.c,v 1.10 2025/08/04 01:44:33 dlg Exp $ */ 2 2 3 3 #include <stdio.h> 4 4 #include "local.h" ··· 14 14 int 15 15 ftrylockfile(FILE *fp) 16 16 { 17 - if (_thread_cb.tc_ftrylockfile != NULL) 18 - return (_thread_cb.tc_ftrylockfile(fp)); 17 + if (__isthreaded) 18 + return __rcmtx_enter_try(&_EXT(fp)->_lock) ? 0 : 1; 19 19 20 20 return 0; 21 21 }
+5 -5
lib/libc/stdio/local.h
··· 1 - /* $OpenBSD: local.h,v 1.25 2016/05/23 00:21:48 guenther Exp $ */ 1 + /* $OpenBSD: local.h,v 1.26 2025/08/04 01:44:33 dlg Exp $ */ 2 2 3 3 /*- 4 4 * Copyright (c) 1990, 1993 ··· 97 97 98 98 #define FLOCKFILE(fp) \ 99 99 do { \ 100 - if (_thread_cb.tc_flockfile != NULL) \ 101 - _thread_cb.tc_flockfile(fp); \ 100 + if (__isthreaded) \ 101 + __rcmtx_enter(&_EXT(fp)->_lock); \ 102 102 } while (0) 103 103 #define FUNLOCKFILE(fp) \ 104 104 do { \ 105 - if (_thread_cb.tc_funlockfile != NULL) \ 106 - _thread_cb.tc_funlockfile(fp); \ 105 + if (__isthreaded) \ 106 + __rcmtx_leave(&_EXT(fp)->_lock); \ 107 107 } while (0)
+1 -2
lib/libc/thread/Makefile.inc
··· 1 - # $OpenBSD: Makefile.inc,v 1.19 2020/02/06 03:13:45 jsg Exp $ 1 + # $OpenBSD: Makefile.inc,v 1.20 2025/08/04 01:44:33 dlg Exp $ 2 2 3 3 .PATH: ${LIBCSRCDIR}/thread 4 4 ··· 8 8 SRCS+= rthread.c \ 9 9 rthread_condattr.c \ 10 10 rthread_debug.c \ 11 - rthread_file.c \ 12 11 rthread_libc.c \ 13 12 rthread_once.c \ 14 13 rthread_tls.c \
-3
lib/libc/thread/callbacks.c
··· 51 51 * here when we actually need to prep for doing MT. 52 52 */ 53 53 _thread_cb.tc_canceled = _thread_canceled; 54 - _thread_cb.tc_flockfile = _thread_flockfile; 55 - _thread_cb.tc_ftrylockfile = _thread_ftrylockfile; 56 - _thread_cb.tc_funlockfile = _thread_funlockfile; 57 54 _thread_cb.tc_malloc_lock = _thread_malloc_lock; 58 55 _thread_cb.tc_malloc_unlock = _thread_malloc_unlock; 59 56 _thread_cb.tc_atexit_lock = _thread_atexit_lock;