Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 90b1188977e2449070f252c9939852443563a8d1 (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
# *****************************************************************************
# * Copyright (c) 2011, 2013-2014, 2016 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *****************************************************************************

import threading


class EventQueue(object):

    def __init__(self, on_shutdown=None):
        self.__thread = threading.Thread(target=self,
                                         name="TCF Event Dispatcher")
        self.__thread.daemon = True
        self.__is_waiting = False
        self.__is_shutdown = False
        self.__on_shutdown = on_shutdown
        self.__lock = threading.Condition()
        self.__queue = []

    def start(self):
        self.__thread.start()

    def shutdown(self):
        try:
            if self.__on_shutdown:
                self.__on_shutdown()
            with self.__lock:
                self.__is_shutdown = True
                if self.__is_waiting:
                    self.__is_waiting = False
                    self.__lock.notifyAll()
            self.__thread.join()
        except Exception as e:
            from . import protocol  # as protocol import this module too
            protocol.log("Failed to shutdown TCF event dispatch thread", e)

    def isShutdown(self):
        with self.__lock:
            return self.__is_shutdown

    def __error(self, x):
        if not self.isShutdown():
            from . import protocol  # as protocol import this module too
            protocol.log("Unhandled exception in TCF event dispatch", x)

    def __call__(self):
        while True:
            try:
                with self.__lock:
                    while not self.__queue:
                        if self.__is_shutdown:
                            return
                        self.__is_waiting = True
                        self.__lock.wait()
                    r, args, kwargs = self.__queue.pop(0)
                r(*args, **kwargs)
            except Exception as x:
                self.__error(x)

    def invokeLater(self, r, *args, **kwargs):
        assert r
        with self.__lock:
            if self.__is_shutdown:
                raise RuntimeError("TCF event dispatcher has shut down")
            self.__queue.append((r, args, kwargs))
            if self.__is_waiting:
                self.__is_waiting = False
                self.__lock.notifyAll()

    def isDispatchThread(self):
        return threading.currentThread() is self.__thread

    def getCongestion(self):
        with self.__lock:
            job_cnt = 0
            l0 = int(job_cnt / 10) - 100
            l1 = int(len(self.__queue) / 10) - 100
            if l1 > l0:
                l0 = l1
            if l0 > 100:
                l0 = 100
            return l0

Back to the top