News for hackers. Stuff that matters.
Re:Re: 100k Threads? (2002) by pdpi on Saturday September 23, 2023 @07:48AM (#37622399)
All else being equal, the one thing Java can do that the OS can’t is avoid paying for context switches. There is a cost to crossing the user space/kernel space boundary that you can avoid with user space lightweight threads.
Re:Re: 100k Threads? (2002) by otabdeveloper4 on Saturday September 23, 2023 @07:56AM (#37622460)
False. The context will need to be switched regardless of where you're doing it. (Userspace or kernel.)

The cost of crossing the userspace/kernel boundary happens on any system call and has nothing to do with context switching.

(Modern OS's typically context switch during system calls for i/o, exactly the same as userspace threads.)

Re:Re: 100k Threads? (2002) by Sesse__ on Saturday September 23, 2023 @08:05AM (#37622524)
The page tables change in a kernel context switch, whereas they don't in a user context switch. Depending on your CPU and OS, this may or may not matter; e.g., modern CPUs typically have caches that are tagged with the context (e.g. PCID on Intel), but the Spectre flavor of the day may require that you simply flush the entire L1 cache on context switch anyway if you're unlucky.

The biggest cost of a context switch, though, is typically that your CPU goes to do something else, which reduces the efficiency of any caches (including branch prediction etc.). This is the same whether you have kernel or userspace context switches, assuming the threads you're switching between touch different code and/or data.

Re:Re: 100k Threads? (2002) by gpderetta on Saturday September 23, 2023 @08:07AM (#37622534)
Well, page tables don't change on a thread context switch, that is relevant for this discussion.
Re:Re: 100k Threads? (2002) by PhilipRoman on Saturday September 23, 2023 @10:04AM (#37623320)
Why do page tables need to change in a context switch between two threads? AFAIK the kernel is mapped in every process, just not accessible in user mode.
Re:Re: 100k Threads? (2002) by ninkendo on Saturday September 23, 2023 @08:18AM (#37622619)
Green threads can be switched between in a single time slice without ever doing a single syscall. This is trivially true: you can make a simple coroutine implementation in C that can switch back and forth between routines thousands of times in a single timeslice.
Re:Re: 100k Threads? (2002) by otabdeveloper4 on Saturday September 23, 2023 @10:13AM (#37623390)
Technically true, but nobody ever does this because it is a massive waste of CPU cycles.

Both OS and "green" threads are meant to switch when doing i/o.

Re:Re: 100k Threads? (2002) by gpderetta on Saturday September 23, 2023 @11:19AM (#37624049)
Userspace IO is a thing though.

Even with traditional kernel side IO, you might not want to attempt a syscall if the socket is known not to be ready.

Also when implementing purely userspace synchronization primitives, being able to swap to a different context purely in userspace is nice.

Re:Re: 100k Threads? (2002) by gpderetta on Saturday September 23, 2023 @08:01AM (#37622495)
User/kernel space switch is a different kind of switch. Also when you do it cooperatively, context switches can be a bit more efficient as the runtime has better knowledge about the set of live registers at the switch point.
Re:Re: 100k Threads? (2002) by otabdeveloper4 on Saturday September 23, 2023 @10:12AM (#37623381)
The "different kind of switch" you talk about doesn't exist on any OS of the past 30 years.
Re:Re: 100k Threads? (2002) by gpderetta on Saturday September 23, 2023 @11:23AM (#37624085)
I'm talking about raising a process privilege level, via whatever syscall mechanism. Which is a completely different code path than a process/thread context switch and, among other things, doesn't require going through the scheduler.
Re:Re: 100k Threads? (2002) by fulafel on Saturday September 23, 2023 @09:36AM (#37623094)
Both of course pay something, but virtual threads may pay less.

Is this significant for virtual thread use cases? OS threads are cheap since there's no virtual memory context switch.