Serializing Tokens - Serializing Tokens in Action

Serializing Tokens in Action

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

Example PseudoCode using Serializing Tokens
Thread A Thread B Behind the Scenes
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.

Read more about this topic:  Serializing Tokens

Famous quotes containing the words tokens and/or action:

    It is the part of men to fear and tremble
    When the most mighty gods by tokens send
    Such dreadful heralds to astonish us.
    William Shakespeare (1564–1616)

    There has never been in history another such culture as the Western civilization M a culture which has practiced the belief that the physical and social environment of man is subject to rational manipulation and that history is subject to the will and action of man; whereas central to the traditional cultures of the rivals of Western civilization, those of Africa and Asia, is a belief that it is environment that dominates man.
    Ishmael Reed (b. 1938)