News for hackers. Stuff that matters.
Re:“POSIX advisory locks are broken by design” by throwaway76543 on Tuesday July 24, 2018 @07:52PM (#17605867)
"Unix SMP" is not synonymous with a system threading interface -- processes give parallelism as well and predate the introduction of threading interfaces. The pthread interface came about in the 1990s.

The unix interface is broken with respect to threads in many places besides this. Signals. Forking. Problems abound because the concept was bolted on decades later.

Re:“POSIX advisory locks are broken by design” by kazinator on Tuesday July 24, 2018 @07:55PM (#17605878)
signals, forking, and simple things like chdir.

A huge area that is broken w.r.t. threading is the whole internationalization stack: setlocale and all.

What if I want to write a server such that each request is executed in a potentially different locale (because it services globally distributed users?)

Re:“POSIX advisory locks are broken by design” by throwaway76543 on Tuesday July 24, 2018 @08:06PM (#17605942)
Ah yes, I was thinking about the system call interfaces but if we start looking at libc damn near nothing was reentrant until relatively recently (the late 90s and early 00s count as recent right? ... right?)

Just take a gander at all those _r functions where an extra parameter needed to be added for reentrancy.

Re:“POSIX advisory locks are broken by design” by kazinator on Tuesday July 24, 2018 @08:42PM (#17606105)
Some of them turned out to be hyper-corrective duds, like readdir_r. No need for that one, since the buffer it needs can be allocated in the DIR object. You'd never want multiple threads concurrently doing readdir_r from the same DIR stream; there is no credible use case for it. (Or, maybe, optimization? Since it can store the dirent-s directly into some space designated by the caller, eliminating a copy operation from a program that wants to retain all those dirent-s.)
Re:“POSIX advisory locks are broken by design” by throwaway76543 on Tuesday July 24, 2018 @09:04PM (#17606210)
Is this because, as I recall, a DIR object is opaque while a FILE is not? Allowing the former to be expanded without breaking the API?

edit: Nevermind, that shouldn't matter should it? The important thing is that they're both by reference.

Re:“POSIX advisory locks are broken by design” by loeg on Tuesday July 24, 2018 @10:18PM (#17606466)
FILE can be opaque. Some implementations made the (arguable) mistake of implementing e.g. fileno() as a macro that accesses FILE internals, and this means they can't change that layout without breaking libc ABI of existing programs. But you can implement all POSIX stdio FILE APIs with an opaque FILE.
Re:“POSIX advisory locks are broken by design” by caf on Tuesday July 24, 2018 @08:18PM (#17605991)
What if I want to write a server such that each request is executed in a potentially different locale?

You can use uselocale() to install a thread-specific locale.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/us...

Re:“POSIX advisory locks are broken by design” by haimez on Tuesday July 24, 2018 @09:01PM (#17606203)
Then your thread better only execute one request at a time and carry through every operation involved in that request. Works fine in PHP, not fine in most performant and even most non performant web contexts.
Re:“POSIX advisory locks are broken by design” by kazinator on Tuesday July 24, 2018 @09:54PM (#17606379)
But that's fine. For example, you pull the thread from a pool and bind it to the request. As part of this, the thread pulls the locale object from the request and attaches to it. It services that request, and then returns to the pool. Next time it is pulled from the pool it gets a request in a different locale.

Other disciplines are possible. E.g. some operation involving multiple locales, all in one thread, can just do the uselocale calls as needed.

Re:“POSIX advisory locks are broken by design” by int_19h on Wednesday July 25, 2018 @03:11AM (#17607411)
That's not fine, because processing a single request often involves waiting on some I/O, and the thread can service (a part of) another request in the meantime. Which is what modern event loop driven servers do - but this doesn't play well with thread locals, or abstractions implemented on top of them, because now your single physical thread effectively runs multiple cooperatively scheduled logical threads/requests.
Re:“POSIX advisory locks are broken by design” by kazinator on Tuesday July 24, 2018 @08:37PM (#17606076)
That's useful to know; so they finally patched this, some ten years ago now.
Re:“POSIX advisory locks are broken by design” by ecnahc515 on Tuesday July 24, 2018 @09:24PM (#17606284)
haimez said this already, but I wanted to phrase it another way:

This only works if your not executing multiple requests in the same thread, ie: using non-blocking IO.

Re:“POSIX advisory locks are broken by design” by caf on Tuesday July 24, 2018 @09:41PM (#17606334)
Even if you're using non-blocking or asynchronous IO there's always going to be a point at which you switch to the context of a particular request in order to handle it, and that's the point at which you can also switch locales if you want to handle it in a locale-specific way.
Re:“POSIX advisory locks are broken by design” by dfox on Wednesday July 25, 2018 @07:47AM (#17608518)
IEEE Std 1003.1-2017 actually specifies chdir() as thread-safe (obviously in the sense that it is atomic but influences all process' threads).

It strikes me as particularly weird as there are notionally POSIX-conformant OSes that implement it in a way that is not atomic.

Re:“POSIX advisory locks are broken by design” by JdeBP on Wednesday July 25, 2018 @12:06PM (#17610315)
Thread safety is not the issue. The issue is that multiple threads may want multiple different working directories simultaneously, for different work items. But there is one working directory for the process. This problem is partly alleviated by the ...at() family of system calls, of course.
Re:“POSIX advisory locks are broken by design” by mjevans on Wednesday July 25, 2018 @03:08AM (#17607402)
It's comments like this which make me think that the state stack should be on the connection handle rather than on a worker unit... or maybe they're the same thing.
Re:“POSIX advisory locks are broken by design” by tannhaeuser on Wednesday July 25, 2018 @12:12AM (#17606861)
Then you don't use multithreading at all rather than conclude it's broken?
Re:“POSIX advisory locks are broken by design” by mmt on Tuesday July 24, 2018 @08:12PM (#17605974)
> processes give parallelism as well and predate the introduction of threading interfaces

Right, but the reverse isn't true, which is what I was getting at. That is, availability of hardware parallelism was a prerequisite for anyone wanting threading in the first place.

Re:“POSIX advisory locks are broken by design” by MaxBarraclough on Wednesday July 25, 2018 @05:10AM (#17607881)
I don't get you. Are you saying that the only reason people would want threading capabilities is to exploit multicore/multiprocessor architectures?

That isn't true. Having multiple threads for GUI applications makes sense for responsiveness even on a single-core machine.

Re:“POSIX advisory locks are broken by design” by mmt on Wednesday July 25, 2018 @02:28PM (#17611587)
> Are you saying that the only reason people would want threading capabilities is to exploit multicore/multiprocessor architectures?

That's exactly what I was saying, though, specifically, kernel threading support.

> Having multiple threads for GUI applications makes sense for responsiveness even on a single-core machine.

I had never heard this before, but I'm hardly an expert. Is there something you could point to, ideally pre-MP (or pre-MP-popularity), that advocates for kernel thread support being needed for improved GUI responsiveness.

Re:“POSIX advisory locks are broken by design” by MaxBarraclough on Thursday July 26, 2018 @05:08AM (#17616082)
Strictly speaking you're right, it's not really about kernel threads. It's about concurrency.

Proper pre-emptive threads in the kernel aren't the only way to achieve that: Java used to use 'green threads' rather than kernel threads. Its advice on using worker threads to keep your Swing application nice and responsive [0], would still have applied. (I believe this is still the case today for GTK programming in Python, which still uses green threads.)

Most GUI toolkits use C-based languages and compile to native code. Cooperative multithreading isn't a popular model in C-family languages, but I can't see a good technical reason that it wouldn't work (after all, it boils down to something akin to green threads with finer control).

A well-designed Qt app (in C++) will use worker threads to keep the UI thread responsive [1]. It doesn't matter if the worker threads run on a different core, or on the same core through time-division multiplexing. It's not a question of compute-throughput. What matters is that the UI thread doesn't get blocked for long.

[0] https://docs.oracle.com/javase/tutorial/uiswing/concurrency/... [1] https://doc.qt.io/archives/qq/qq27-responsive-guis.html

Re:“POSIX advisory locks are broken by design” by mmt on Thursday July 26, 2018 @02:24PM (#17619645)
> Strictly speaking you're right, it's not really about kernel threads.

I'm not particularly interested in being right or wrong, but, rather, improving understanding.

The reason I made the (strict) distinction was that the whole discussion has been about kernel-supported file locking. Anything called "threading" outside of the kernel seems inapplicable, no matter how closely related conceptually or in practice.

Of course, I may be missing something, such as if userspace/green threads both significantly preceded kernel threading support and motivated its implementation (presumably by benefiting from it).

Re:“POSIX advisory locks are broken by design” by MaxBarraclough on Friday July 27, 2018 @04:49AM (#17624505)
> I'm not particularly interested in being right or wrong, but, rather, improving understanding.

Sure, I hadn't meant to seem combative.

> the whole discussion has been about kernel-supported file locking. Anything called "threading" outside of the kernel seems inapplicable, no matter how closely related conceptually or in practice

Is it necessarily inapplicable? You could write a Python program that uses file-based locking, despite that Python threads don't map to kernel threads, no?

Or in C, you could have 'fake threads' where you effectively bounce control around between a few different execution streams (something akin to coroutines or fibers). We could still reason about which 'thread' holds the file lock, despite that there's only one kernel thread. (Aside: the GNU 'Pth' library does something like this.)

> such as if userspace/green threads both significantly preceded kernel threading support and motivated its implementation

I think interpreters tend to use green threads simply for ease of implementation, more than for anything else. I suppose it helps with cross-platform concerns, but these days, it's quite possible to write multi-platform concurrent C/C++ code. Green threads aren't so popular today, now that multicore is the norm.

Long ago, processes predated (kernel) threads. Before my time, but I believe the general idea then was that if you want the kernel to orchestrate concurrent execution (including executing in parallel on multiple cores if possible) then you'd just have to bite the bullet and go multi-process.

Today we still see some use of process-level concurrency/parallelism, such as in Postgres.

Re:“POSIX advisory locks are broken by design” by mmt on Saturday July 28, 2018 @01:32AM (#17631713)
> Is it necessarily inapplicable? You could write a Python program that uses file-based locking, despite that Python threads don't map to kernel threads, no?

Sure, but what would be the point? I'm looking for legitimate motivations, not contrived examples of what's merely possible. Remember, we're talking about single-core, single-process, multi-threaded for responsiveness (e.g. in a GUI) here. That single process wants to use kernel file locking?

> Long ago, processes predated (kernel) threads. Before my time, but I believe the general idea then was that if you want the kernel to orchestrate concurrent execution (including executing in parallel on multiple cores if possible) then you'd just have to bite the bullet and go multi-process.

By the time Unix-based MP systems became common (all single-core at the time, just multi-socket), those OSes tended to have kernel threads and at least lightweight processes. I can't recall when SunOS 4.1 got it, but I think it was around '91 if not before, but Sun was pushing SunOS 5 aka Solaris 2 for their big SMP boxes https://www.dre.vanderbilt.edu/~schmidt/PDF/beyond_mp.pdf

Before that, there wasn't much biting of any bullets, as I recall, as the challenge was time-sharing the scarce resource that was a CPU, not parallelism. Frequent context-switching, process or even thread, would be the performance-killing worry for something real-time like a GUI on a computer with a single user. Per Moore's Law, today's CPUs have something like 11000x the transistor density of the ones in '91.

Re:“POSIX advisory locks are broken by design” by MaxBarraclough on Monday July 30, 2018 @04:32AM (#17642955)
> what would be the point

The point of my comment there was that I'm not sure what you meant by 'inapplicable'. It would work, right?

As you say, it seems unlikely you'd ever want to use file locks for intra-process concurrency when you could just use the mutex machinery of your language's standard library. The use of green threads doesn't change things at all.

> the challenge was time-sharing the scarce resource that was a CPU, not parallelism

The question there is how pre-emption works, right? They're non-cooperative threads after all. If I understand correctly, Solaris used not to have a direct mapping from userland threads to kernel threads (I'm not sure how the multiplexing worked), but they later moved to a direct mapping (every thread really is backed by a kernel-managed thread).

(I could be completely wrong about that, I just skim-read the 'Solaris Internals' book once.)

Re:“POSIX advisory locks are broken by design” by mmt on Monday July 30, 2018 @06:32PM (#17648763)
> The point of my comment there was that I'm not sure what you meant by 'inapplicable'.

Inapplicable to the context of the over-arching discussion, which is POSIX advisory file locks.

These are not the "threads" you're looking for.

>>> Are you saying that the only reason people would want threading capabilities is to exploit multicore/multiprocessor architectures?

So, to recap, yes, I'm saying the only reason people would want kernel threading capabilities (the only threading capabilities applicable to the topic at hand) is to exploit multicore/multiprocessor architectures.

Re:“POSIX advisory locks are broken by design” by MaxBarraclough on Tuesday July 31, 2018 @04:30AM (#17651490)
> the only threading capabilities applicable to the topic at hand

If we're going to discuss the merits of kernel threads, it doesn't make sense to do so in a vacuum.

> I'm saying the only reason people would want kernel threading capabilities [...] is to exploit multicore/multiprocessor architectures

Didn't we already discuss this? The answer is no.

Preemptive multithreading of native-compiled code, is a popular concurrency strategy. I gave the example of Qt. GUI toolkits based on C/C++ could use userland-based cooperative multithreading to keep things responsive, but they don't. They use kernel threads, for reasons which have nothing to do with parallelism.

Re:“POSIX advisory locks are broken by design” by mmt on Tuesday July 31, 2018 @03:46PM (#17656411)
> Didn't we already discuss this? The answer is no.

We did, but you never made the case for the answer being no, which means the answer is still yes.

> GUI toolkits based on C/C++ could use userland-based cooperative multithreading to keep things responsive, but they don't. They use kernel threads

That's descriptive, not normative. You haven't said why that's the case, or, more importantly, why, in the absence of kernel threads, someone would want them to exist only for GUI responsiveness types of use cases.

Your earlier admission led me to believe they wouldn't:

> Cooperative multithreading isn't a popular model in C-family languages, but I can't see a good technical reason that it wouldn't work (after all, it boils down to something akin to green threads with finer control).

Re:“POSIX advisory locks are broken by design” by MaxBarraclough on Wednesday August 01, 2018 @05:06AM (#17660793)
> you never made the case for the answer being no, which means the answer is still yes

The widespread use of preemptive concurrency for reasons aside from parallelism, demonstrates my point.

> You haven't said why that's the case

If I had to guess, I'd go with this: it's the easiest option for the C/C++ languages and developers, considering how existing functions tend to be blocking, and that C/C++ programmers are more likely to be familiar with preemptive concurrency than cooperative concurrency.

You certainly wouldn't want to use a model that relies on, say, continuations. Intel's TBB library does this. It's a very good library, but the user must write pretty hairy C++ code, despite the benefits of modern C++. It's no help that continuations are trivial in, say, modern C#.

Fibers/coroutines can be done with external C/C++ libraries, but it's seen as a pretty exotic thing to do.

> why, in the absence of kernel threads, someone would want them to exist only for GUI responsiveness types of use cases

Looks like Windows first got 'proper threads' in Windows 98. I suspect it's not purely for GUI, but for other types of application too, such as servers. Again I suspect the reason they're used in preference over the cooperative alternative, is largely just what people are used to.

Looks like Windows provides fibers for C++, so it's not like they've never heard of them. [0]

> Your earlier admission led me to believe they wouldn't:

Sure, I'm not hating on cooperative multithreading. They have advantages: fine-grain user control, no unexpected interleaving, better performance regarding switching. Bit of a pity it's not often used, I suppose.

I google'd the question, but didn't find a decent exploration of why they're so rarely used. This StackOverflow answer thinks the answer is technical, but I'm not convinced. It seems to discount asynchronous IO. https://stackoverflow.com/a/16766549/

[0] https://docs.microsoft.com/en-us/windows/desktop/procthread/...

Re:“POSIX advisory locks are broken by design” by mmt on Wednesday August 01, 2018 @07:02AM (#17661366)
> The widespread use of preemptive concurrency for reasons aside from parallelism, demonstrates my point.

If your point is that kernel threads are more attractive that the alternative, once they already exist, then yes, but that continues to miss refuting my assertion.

Using something ("off label"[1]) once it's already there, isn't the same thing as wanting/needing it in the first place.

[1] consistent with my assertion that the "label" for threading specifies its use for multiprocessor support. After all, how did the early X server or Sun's NeWS manage to be so responsive before all that newfangled threading stuff in the 90s?

Re:“POSIX advisory locks are broken by design” by MaxBarraclough on Thursday August 02, 2018 @05:39AM (#17670269)
> how did the early X server or Sun's NeWS manage to be so responsive before all that newfangled threading stuff in the 90s?

Good question. I have no idea.

If my intuition is accurate, systems like the DOOM engine just crunch through each frame's workload as a sequential queue, tending to things like the audio buffer whenever necessary. I don't know if there's any asynchronous code to speak of in there.

Re:“POSIX advisory locks are broken by design” by anarazel on Friday July 27, 2018 @02:59PM (#17628296)
> Today we still see some use of process-level concurrency/parallelism, such as in Postgres.

FWIW, as somebody working on postgres, there's plenty reasons to regret this. The unshared memory, fd spaces make some things more complicated / heavyweight. We've cross-process shared memory allocators, but then the pointers all have to be relative. It's harder to separate connections from a "query execution context", making connections more heavyweight than necessary. Worker threads for async IO - which we'd love to have - would need to be duplicated in each process. Etc.

There's still quite some benefits, like increased robustness in the face of bugs, reduced contention for memory alloccators etc, ... But I doubt you'd find anything close to a majority inside the postgres team to use threads if we'd need to make the decision again, unencumbered by existing code.