Serializing tokens
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, serializing tokens are a concept in concurrency control arising from the ongoing development of
DragonFly BSD DragonFly BSD is a free and open-source Unix-like operating system forked from FreeBSD 4.8. Matthew Dillon, an Amiga developer in the late 1980s and early 1990s and FreeBSD developer between 1994 and 2003, began working on 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 Multiprocessing is the use of two or more central processing units (CPUs) within a single computer system. The term also refers to the ability of a system to support more than one processor or the ability to allocate tasks between them. There ar ...
-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.


Comparison with mutual exclusion (mutex)

Tokens and
mutual exclusion In computer science, mutual exclusion is a property of concurrency control, which is instituted for the purpose of preventing race conditions. It is the requirement that one thread of execution never enters a critical section while a concurren ...
(mutex) mechanisms are locks. Unlike mutexes, tokens do not exclude other threads from accessing the resource while they are blocked or asleep. A thread sharing resources with other threads can be stopped and started for a variety of reasons: # Timeslicing: the user space (US) 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, a thread may be run at exactly the same time as another thread on a different CPU. # Preemption: a thread may preempt a lower-priority thread, such as a hardware interrupt or Light Weight Kernel Threads. # Voluntary blocking: a thread may sleep if it has to wait for something, has no work to do, or calls a function that blocks. Even the call to acquire a lock can block. The following table summarizes the properties of tokens and mutexes. Issues such as
deadlock In concurrent computing, deadlock is any situation in which no member of some group of entities can proceed because each waits for another member, including itself, to take action, such as sending a message or, more commonly, releasing a loc ...
and
priority inversion In computer science, priority inversion is a scenario in scheduling in which a high priority task is indirectly superseded by a lower priority task effectively inverting the assigned priorities of the tasks. This violates the priority model that ...
can be very difficult to avoid, and require coordination at many levels of the kernel. Because locking with tokens does not deadlock and acquired tokens need not be atomic when later operations block, it allow much simpler code than mutexes.


Example

The following
pseudocode In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
and explanations illustrate how serializing tokens work. {, class="wikitable" style="background-color:white" , + Example PseudoCode using serializing tokens , - !width="35%" , Thread A !width="35%" , Thread B !Action , - ,
lwkt_gettoken(T1);
iter = list1.head;
,
...
lwkt_gettoken(T1); // blocks
// waiting for token T1
, A acquires token T1 and uses it to get synchronized access to list1, which is shared by both threads. , - ,
lwkt_gettoken(T2); // blocks
,
// waiting for token T1
, A's call to lwkt_gettoken(T2) is a blocking function, so A goes to sleep and temporarily loses its tokens. It will be awakened when the scheduler sees that both T1 and T2 are available. , - ,
// waiting for T1 and T2
,
list1.head = list1.head.next;
lwkt_releasetoken(T1);
, B acquires T1 and modifies list1. Note that A's "iter" still points to the old head of the list. , - ,
// get the new version of the head:
iter = list1.head; 
// make new list:
while (iter != null) {
    list2.tail = iter;
    iter = iter.next;
}
lwkt_releasetoken(T1);
lwkt_releasetoken(T2);
,   , The scheduler sees that both T1 and T2 are available, so it wakes up thread A. Since A was coded correctly, it refreshes its iterator with the new head of list1, and does some nonblocking operations on it. Note that it would have been better form for A to simply ask for both tokens at the start.


Prior art in the Darwin kernel

Mac OS X macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lap ...
's Darwin kernel uses a similar technique (called a
funnel A funnel is a tube or pipe that is wide at the top and narrow at the bottom, used for guiding liquid or powder into a small opening. Funnels are usually made of stainless steel, aluminium, glass, or plastic. The material used in its construct ...
) to serialize access to the BSD portion of the kernel.


See also

* Lock-free and wait-free algorithms


References


A mailing list thread where Matthew Dillon explains tokens in great detail


Concurrency control