aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/src/com/yahoo/jrt/Worker.java
blob: 763c9a59c40f5820163923d16d859e0ccabe68d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;


class Worker {

    private static final int WORK_LIMIT = 1024;

    private class Run implements Runnable {
        public void run() {
            try {
                Worker.this.run();
            } catch (Throwable problem) {
                parent.handleFailure(problem, Worker.this);
            }
        }
    }

    private static class CloseSocket implements Runnable {
        Connection connection;
        CloseSocket(Connection c) {
            connection = c;
        }
        public void run() {
            connection.closeSocket();
        }
    }

    private static class DoHandshakeWork implements Runnable {
        private final Connection connection;
        DoHandshakeWork(Connection c) {
            connection = c;
        }
        public void run() {
            connection.doHandshakeWork();
            connection.transportThread().handshakeWorkDone(connection);
        }
    }

    private static void preloadClassRequiredAtShutDown() {
        new CloseSocket(null);
    }
    private final Thread      thread;
    private final Transport   parent;
    private final ThreadQueue workQueue = new ThreadQueue();

    public Worker(Transport parent) {
        preloadClassRequiredAtShutDown();
        thread = new Thread(new Run(), parent.getName() + ".jrt-worker");
        this.parent = parent;
        thread.setDaemon(true);
        thread.start();
    }

    private void doLater(Runnable r) {
        if(!workQueue.enqueue(r, WORK_LIMIT)) {
            r.run();
        }
    }

    public void closeLater(Connection c) {
        doLater(new CloseSocket(c));
    }

    public void doHandshakeWork(Connection c) {
        doLater(new DoHandshakeWork(c));
    }

    private void run() {
        try {
            while (true) {
                ((Runnable) workQueue.dequeue()).run();
            }
        } catch (EndOfQueueException e) {}
    }

    public Worker shutdown() {
        workQueue.close();
        return this;
    }

    public void join() {
        while (true) {
            try {
                thread.join();
                return;
            } catch (InterruptedException e) {}
        }
    }
}