File: rwait.py

package info (click to toggle)
linux-show-player 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,896 kB
  • sloc: python: 12,408; sh: 154; makefile: 17; xml: 8
file content (122 lines) | stat: -rw-r--r-- 3,854 bytes parent folder | download | duplicates (4)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
#
# This file is part of Linux Show Player
#
# Copyright 2012-2016 Francesco Ceruti <ceppofrancy@gmail.com>
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player.  If not, see <http://www.gnu.org/licenses/>.

import time
from threading import Event

from lisp.core.signal import Signal


class RWait:
    """Provide a resumeable-wait mechanism."""

    def __init__(self):
        self._elapsed = 0
        self._start_time = 0
        self._ended = False

        self._waiting = Event()
        self._waiting.set()
        self._is_waiting = Event()
        self._is_waiting.set()

        self.start = Signal()
        self.ended = Signal()
        self.paused = Signal()
        self.stopped = Signal()

    def wait(self, timeout, lock=None):
        """Block until the timeout is elapsed or `pause` or `stop` are called.

        If the wait is paused, the next time this function is called the timeout
        will be `total_timeout - elapsed_time`.

        :param timeout: time to wait
        :param lock: lock to release before and re-acquired after the wait
        :return: True if the wait has not been interrupted by `pause` or `stop`
        :rtype: bool
        """
        if self._is_waiting.is_set():
            # Clear the events
            self._waiting.clear()
            self._is_waiting.clear()
            # Set the start-time
            self._start_time = time.monotonic() - self._elapsed

            self.start.emit()

            if lock is not None:
                lock.release()

            # Wait for the "remaining" time
            self._ended = not self._waiting.wait(timeout - self._elapsed)

            # If the wait is ended by timeout
            if self._ended:
                self._elapsed = 0
                self.ended.emit()

            # Notify that we are not waiting anymore
            self._is_waiting.set()

            if lock is not None:
                lock.acquire()

            return self._ended

        return False

    def stop(self):
        """Stop the wait."""
        if self.current_time() > 0:
            self._waiting.set()
            self._is_waiting.wait()

            # Check self._ended to ensure that the wait is not ended by timeout
            # before `self._waiting.set()` is called in this method.
            if not self._ended:
                self._elapsed = 0
                self.stopped.emit()

    def pause(self):
        """Pause the wait."""
        if not self._is_waiting.is_set():
            # Calculate elapsed time ASAP
            elapsed = time.monotonic() - self._start_time
            self._waiting.set()
            self._is_waiting.wait()

            # Check self._ended to ensure that the wait is not ended by timeout
            # before `self._waiting.set()` is called in this method.
            if not self._ended:
                self._elapsed = elapsed
                self.paused.emit()

    def current_time(self):
        """Return the currently elapsed time."""
        if self._is_waiting.is_set():
            return self._elapsed

        return time.monotonic() - self._start_time

    def is_waiting(self):
        return not self._is_waiting.is_set()

    def is_paused(self):
        return self._is_waiting.is_set() and self.current_time() > 0