apache-ignite

Форк
0
90 строк · 2.5 Кб
1
# Licensed to the Apache Software Foundation (ASF) under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#    http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
"""
17
This module contains concurrent utils.
18
"""
19

20
import threading
21

22

23
class CountDownLatch:
24
    """
25
    A count-down latch.
26
    """
27
    def __init__(self, count=1):
28
        self.count = count
29
        self.cond_var = threading.Condition()
30

31
    def count_down(self):
32
        """
33
        Decreases the latch counter.
34
        """
35
        with self.cond_var:
36
            if self.count > 0:
37
                self.count -= 1
38
            if self.count == 0:
39
                self.cond_var.notifyAll()
40

41
    def wait(self):
42
        """
43
        Blocks current thread if the latch is not free.
44
        """
45
        with self.cond_var:
46
            while self.count > 0:
47
                self.cond_var.wait()
48

49

50
class AtomicValue:
51
    """
52
    An atomic reference holder.
53
    """
54
    def __init__(self, value=None):
55
        self.value = value
56
        self.lock = threading.Lock()
57

58
    def set(self, value):
59
        """
60
        Sets new value to hold.
61
        :param value: New value to hold.
62
        """
63
        with self.lock:
64
            self.value = value
65

66
    def get(self):
67
        """
68
        Gives current value.
69
        """
70
        with self.lock:
71
            return self.value
72

73
    def compare_and_set(self, expected, value):
74
        """
75
        Sets new value to hold if current one equals expected.
76
        :param expected: The value to compare with.
77
        :param value: New value to hold.
78
        """
79
        return self.check_and_set(lambda: self.value == expected, value)
80

81
    def check_and_set(self, condition, value):
82
        """
83
        Sets new value to hold by condition.
84
        :param condition: The condition to check.
85
        :param value: New value to hold.
86
        """
87
        with self.lock:
88
            if condition():
89
                self.value = value
90
            return self.value
91

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.