11 ms·
> i mean awk supported arbitrarily many hash tables in a single process This API supports that, too, as long as you don’t call one of the functions while anoth
by Someone 2y ago
> i mean awk supported arbitrarily many hash tables in a single process
This API supports that, too, as long as you don’t call one of the functions while another call is in progress. You get that for free if your process is single threaded.
https://en.wikipedia.org/wiki/Thread_(computing)#Threading_models https://en.wikipedia.org/wiki/Thread_(computing)#Threading_m... says
“History of threading models in Unix systems
SunOS 4.x implemented light-weight processes or LWPs. NetBSD 2.x+, and DragonFly BSD implement LWPs as kernel threads (1:1 model)”
According to Wikipedia, SunOS 4.0 is from 1988, NetBSD 2.0 from 2004, so I guess chances are good that AT&T System V UNIX didn’t support threads in user space programs.
- p_l 2y agoAdding threading support is why there was a bunch of interest in using Mach microkernel in Unix systems.
- kimixa 2y agoOr a signal handler? Even if there's a single kernel thread, if it can be interrupted while inside one of these functions you can hit the same sort of problem. Reentrancy was an issue on consumer devices well before multithreading was commonly supported there, after all
- rdtsc 2y agoGood point. There are multiple levels of "safety". I think glibc API does a decent job marking and indicating which is which: https://sourceware.org/glibc/manual/2.40/html_node/POSIX-Safety-Concepts.html https://sourceware.org/glibc/manual/2.40/html_node/POSIX-Saf... Thread-Safe Async-Signal-Safe Async-Cancel-Safe (mostly limited to pthread_*cancel*) There is a host of other "safety" remarks also useful to keep in mind: https://sourceware.org/glibc/manual/2.40/html_node/Other-Safety-Remarks.html https://sourceware.org/glibc/manual/2.40/html_node/Other-Saf...
- kragen 2y agothis isn't about safety, though; hcreate() and hsearch() don't support having two hash tables in the same process at the same time, not even in an unsafe way. that also makes them non-reentrant, but that isn't the main problem!
- kragen 2y agoyou seem to be discussing what does or doesn't support multiple threads, but what we were talking about is what does or doesn't support multiple simultaneously existing hash tables, which hcreate() doesn't. i've edited my comment upthread to clarify (it's also true, as kimixa points out, that a single-threaded process with signal handlers isn't really single-threaded, but i don't think that's really the important consideration in this context) for what it's worth, it's not very difficult to use alarm() to invoke a context-switching subroutine to get preemptive user-space multithreading on unix without any kernel support. in theory you have to write the context-switching subroutine in assembly language, which isn't difficult at all; here's one i wrote in arm assembly for the arm eabi, for cooperative multithreading: .thumb_func .globl einyield einyield: push {r0, r4-r11, lr} @ save all callee-saved regs plus r0 ldr r0, =current_task_pointer ldr r1, [r0] str sp, [r1] @ save stack pointer in current eintask ldr r1, [r1, #4] @ load pointer to next eintask str r1, [r0] ldr sp, [r1] @ switch to next eintask's stack pop {r0, r4-r11, pc} @ return into einyielded context there (from http://canonical.org/~kragen/sw/dev3/einkornix.S http://canonical.org/~kragen/sw/dev3/einkornix.S) but in practice you can usually just use longjmp() preemptive multithreading requires you to save all the registers, not just the callee-saved registers, but so does signal handling, so you can usually just use the signal-handling machinery and use the above code (or its equivalent on your vax or 68000 or whatever) to context-switch between signal-handler stack frames