Hacker News new | comments | show | ask | jobs | submit login
Look ma, no locks (lucteo.ro)
165 points by ingve 4 days ago | hide | past | web | favorite | 32 comments





This is a confusing article. The main content is a description of how to implement a simple operating system scheduler which effectively is implementing locks using instruction level primitives.

The author has completely neglected the most common cases where either "tasks" require multiple resources (e.g. when copying between two files) or where the ordering of tasks is important. It is these two conditions that lead to deadlocks and livelocks.


Most (though not all, see Midori) OSes use preemptive multitasking and threads. These tasks are cooperating coroutines - a different beast. Of course you can use tasks to implement a thread scheduler, but this machinery would probably live in a user-mode runtime.

As for the neglected cases, you can build those out of these primitives: canonical ordering of tasks for multiple resource acquisition; work stealing and/or a DAG executor where ordering is important.

My only criticism is that it's a little redundant to implement both mutexes and semaphores - a mutex is just a semaphore of 1. But the rest is wonderful.


Mutex is for resource locking, semaphore is for signaling. This means they have some differences in implementation extending beyond simply limiting semaphore to get a mutex.

AFAIK, the only difference between "semaphore of 1" and "mutex" is that a semaphore is definitely able to be signaled by someone who isn't holding a lock, whereas a mutex is sometimes only able to be signaled/unlocked by whoever acquired the lock.

As long as you trust the code running on your machine (if you don't, you have bigger issues to worry about), you'll be fine - nobody signals a semaphore before waiting on it if they're hoping to achieve mutual exclusion


>to be able to do extra work at the end of the task, we wrap our task into a task that call the given tasks and performs the extra work

Yea, a bit confusing. He surely knew what he meant but I didn’t.

Given the prevalence of mutexes and semaphores in embedded I was hoping to take something useful away, but yea the issue is I have no other work to do. I can’t do anything really until this read or write or access is done. I can’t write here until this read is finished not because of access but logically I need input to determine output, etc. A bit different from your comment, but similar lines i think.


It is a scheduler, but it is different from a typical OS scheduler. On most OSs, when a thread locks a mutex it keeps running on its original CPU, it is not suspended and resumed in the CPU that last locked that mutex (which would be an approximation for the behaviour of this architecture).

I'm expecting someone to come denigrate your approach in the comments, but the fact you made this readable and kept me with you to the end is a real feat of communication. Thanks for putting in all the effort to re-explain, color-code, and consider objections.

I believe a number of video game engines use this approach.

There’s a talk about this approach in the game Destiny: https://www.gdcvault.com/play/1022164/Multithreading-the-Ent...

And also in the Uncharted engine: http://twvideo01.ubm-us.net/o1/vault/gdc2015/presentations/G...

They go a step further by allowing threads to pause and resume their state by changing the stack pointer.

These sort of systems are useful in games where you basically want to be using all of the cpu all of the time and there’s no reason to ever give control back to the operating system’s scheduler (with mutexes).


I'm not entirely sure that I agree with the section comparing mutexes and task dispatch. It appears that the comparison is actually between a spinlock and task dispatch -- most other kinds of mutexes put a process into an interruptible state that allow the scheduler to run a different process. So I would contend that they act in a very similar fashion in practice. At least, that's how locking works within the Linux kernel (and I think pthreads locks work the same way on Linux).

However, I do quite like this way of thinking about synchronisation. I wonder how many other kinds of synchronisation primitives (I'm thinking about RCU and seqlocks) can be re-thought using this method.


> Here, instead of blocking on the second thread when executing the blue task, the scheduler picks up a yellow task;

This only works if the second thread's yellow task is independent of its blue task's results. Which is, unfortunately, only 1% of the cases.


Nice article.

A few high performance C++ systems use variants of the dispatcher approach. For example ScyllaDB and I know of a few HFT systems like that. Boost.asio is similar (the serializer is, or used to be, called strand) and so is the dispatcher proposal in C++.

A few systems I have seen strictly map each dispatcher to a thread (which is in turn exclusively pinned to an hardware core), so the serialization is implicit. "Active" objects expose their home dispatcher and to interact with the object one would post a lambda to that object dispatcher. In practice this is a form of async message passing. To get the answer back either a response callback is posted to the original thread or some future/promise abstraction is used.


I think the author eventually came up with what libdispatch already does using queues.

Interesting that a 101 style explanation wouldn't tell you that mutex is derived from "mutually exclusive".

Otherwise, a pretty nice overview. The bar charts are helpful.


Likely because the target audience is programmers whom use mutexes rather than programmers whom do not write threaded code.

Please don't be mad as I'm not trying to provoke, but I believe 'who' goes here, not 'whom', as from what I understand, whom is used when something is being done/given to something. Perhaps I'm simplifying, but it's just like nouns etc. in the accusative case work in languages which have grammatical cases.

TL;DR: 'whom' is to 'him' as 'who' is to 'he'.

For whom does the bell toll? It tolls for him (not 'he').

Who is there? He (not 'him') is there.


Ooh, now do the predicate nominative!

This is kinda the type of model I use my little lock-free SPSC workqueues for: https://github.com/djcapelis/atomic-ring

You just have things do stuff and then toss pointers to threads on the other side of the workqueue. Most things I do this with I basically keep each thread in a different part of the pipeline and they pass the task to the next one. Sometimes this means each thread has a warm cache for the part of the task it's working on, assuming the majority of the working set of each task is more correlated with the stage of the pipeline than the specific data moving through it.


This sounds a lot like going from the Lock/Mutexes/Semaphores paradigm into an Actor system.

OP - i.e. @ingve - it might be interesting to take a look into some strategies - like the pull one - on Actor systems like AKKA


Sounds like libdispatch

Yep, working in iOS/OS X for 9 years now. The primitives are queues not mutexes.

The skeleton of the approach suggested reminds me of how the BEAM runs processes: one OS thread per CPU, scheduling many lightweight tasks listening to message queues.

If you want to see a practical working implementation of what this article describes: https://github.com/tinspin/rupy (Java HTTP Server)

What does he mean by "static world", "static task"?

They mean that, if you know upfront how long every task takes and the order they need to run in, it's straightforward to lay out a schedule that has no resource contention.

I'm not sure that that's true in general but it's true that it's easier than when you don't know those things.


Enqueuing tasks per resource is a very old concept in computer science; this is nothing new and anyone with a computer science degree should have come out of the university with having written such a task scheduler under their belt.

Yet, most people with a computer science degree have not come out of the university with having written such a task scheduler under their belt.

That says more about how bad the curriculum is than about them. Still, anyone doing any serious programming has either had to or will need to write such an enqueuing scheduler at least once in their career, because such a solution is really useful.

Also, not all of us have computer science degrees.

Then it's high time to get one, otherwise those people have no business being in the IT industry.

I would like to compare this with Haskell's STM.

Looks like a reinvention of async/await.



Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: