Replaying a schedule with a lock

A mutex lock makes the increment a critical section: a thread that tries to lock while another holds the lock must wait, so the three steps of two threads can never mix.

What to build

Replay a schedule of steps with a lock around each increment, and count the wasted steps.

Requirements

  • The counter starts at 0. Each of the t threads does k increments. One increment is five steps: lock, load, add, store, unlock. The load, add and store work as in the previous lock-free version.
  • A lock step by a thread while another thread holds the lock is blocked: it does nothing, and the thread tries again next time it is scheduled. Otherwise lock takes the lock. unlock frees it.
  • A step given to a thread that has finished all its increments is idle.
  • Print counter C, blocked B and idle I.

Input

The first line has t (1 ≤ t ≤ 4), k (1 ≤ k ≤ 4) and s (1 ≤ s ≤ 80). The second line has s thread numbers, from 1 to t.

Output

Three lines: counter C, blocked B, idle I.

Constraints

The schedule may end before all threads finish.

Example

Input:

2 1 10
1 2 1 2 1 2 1 2 1 1

Output:

counter 1
blocked 4
idle 1

Why it works

With the lock, the load, add and store of one thread are never mixed with another thread's, so no update is lost: the counter equals the number of stores. The price is the blocked steps: a thread that is scheduled while it cannot enter wastes that time.

Examples

Example 1 · Input
2 1 10
1 2 1 2 1 2 1 2 1 1
Expected output
counter 1
blocked 4
idle 1
Example 2 · Input
2 1 11
1 1 1 1 1 2 2 2 2 2 2
Expected output
counter 2
blocked 0
idle 1

Hints

Hint 1

Keep lock_owner (0 when free), and for each thread a pc and a register. The step is pc % 5: 0 lock, 1 load, 2 add, 3 store, 4 unlock.

Hint 2

At step 0, if the lock is held by another thread, count a blocked step and do not move the pc. Otherwise take the lock and move on.

Hint 3

A thread with pc equal to 5 times k is finished: its steps count as idle.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

int main(void) {
    int t, k, s, who, counter = 0, blocked = 0, idle = 0;
    int owner = 0, pc[5] = {0}, reg[5] = {0};
    scanf("%d %d %d", &t, &k, &s);
    for (int i = 0; i < s; i++) {
        scanf("%d", &who);
        if (pc[who] >= 5 * k) {
            idle++;
            continue;
        }
        int step = pc[who] % 5;
        if (step == 0) {
            if (owner != 0 && owner != who) {
                blocked++;
                continue;
            }
            owner = who;
        } else if (step == 1) {
            reg[who] = counter;
        } else if (step == 2) {
            reg[who] = reg[who] + 1;
        } else if (step == 3) {
            counter = reg[who];
        } else {
            owner = 0;
        }
        pc[who]++;
    }
    (void)t;
    printf("counter %d\n", counter);
    printf("blocked %d\n", blocked);
    printf("idle %d\n", idle);
    return 0;
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Threads and race conditions →