Serializing tokens

Serializing tokens

In computer science, serializing tokens are a concept in concurrency control arising from the ongoing development of DragonFly BSD. According to Matthew Dillon, they are most akin to SPLs, except a token works across multiple CPUs while SPLs only work within a single CPU's domain.

Serializing tokens allow programmers to write multiprocessor-safe code without themselves or the lower level subsystems needing to be aware of every single entity that may also be holding the same token.


= Tokens vs. Mutexes =

Tokens are similar to mutexes in that they can, if used correctly, prevent multiple threads from accessing a shared resource at the same time. Unlike mutexes, however, they do NOT exclude other threads from accessing the resource "while they are blocked or asleep." In general terms, they're both locks: your thread gets a lock (which prevents other threads from having it), does some work, and then releases it for another thread to use.

It's important here to recall how threads interact with each other when sharing resources. There are a number of ways that a thread can be stopped and another thread to be started:
# Timeslicing: the scheduler tries to ensure that all threads get a fair chance to run, so it runs each thread for a brief period of time (a timeslice) and then switches to another thread.
# Concurrent Execution: In multiprocessor computers, your thread may also be run at "exactly the same time" as another thread on a different CPU.
# Preemption: A thread may be preempted by a higher priority thread, such as a hardware interrupt or LWKT.
# Voluntary Blocking: A thread may voluntarily block (aka "go to sleep") if it has to wait for something, has no work to do, or calls a function that blocks-- note that even the call to acquire a lock can block.

Remember: the purpose of a lock is to keep other threads out while your thread is working on something. This table summarizes the situations in which tokens and mutexes work correctly to keep other threads "out".

So what's the big deal? It seems like mutexes are the clear winner-- and in some cases it's important to be able to block and keep a lock. However, they also cause problems such as Deadlocks and Priority inversions. Dealing with these issues is very difficult and requires coordination at many different levels of the kernel:

Obviously Matt has reason to promote his own solution to deadlocking, but he has a point: serializing tokens do a fine job of locking out other threads "as long as you don't block while holding them". If you do, another thread will steal the lock and possibly change the data you were working on. You will reacquire the token when you are awakened, but you will have to make sure that your data is still consistent.

Serializing Tokens In Action

To show how serializing tokens actually work, let's see some pseudocode and what's going on behind the scenes.

See also

* Lock-free and wait-free algorithms

References

* [http://thread.gmane.org/gmane.os.dragonfly-bsd.kernel/4436 A mailing list thread where Matthew Dillon explains tokens in great detail]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • DragonFly BSD — DragonFly redirects here. For other uses, see Dragonfly (disambiguation). DragonFly Company / developer Matthew Dillon OS family Unix like …   Wikipedia

  • Deadlock — This article is about the computer science concept. For other uses, see Deadlock (disambiguation). A deadlock is a situation where in two or more competing actions are each waiting for the other to finish, and thus neither ever does. It is often… …   Wikipedia

  • Lock (computer science) — In computer science, a lock is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many threads of execution. Locks are one way of enforcing concurrency control policies. Contents 1 Types 2… …   Wikipedia

  • XNU — Infobox Software name = XNU kernel caption = developer = Apple Inc. latest release version = latest release date = operating system = Darwin Mac OS X genre = Kernel kernel type = Hybrid license = Apple Public Source License 2.0 working state = In …   Wikipedia

  • DragonFly BSD — Pour les articles homonymes, voir Dragonfly. DragonFly BSD Famille BSD Type de noyau Hybride État du projet en développement Plates formes …   Wikipédia en Français

  • DragonflyBSD — DragonFly BSD Pour les articles homonymes, voir Dragonfly. DragonFly BSD Famille BSD Type de noyau Hybride …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”