/
callback_groups.py
112 lines (86 loc) · 3.33 KB
/
callback_groups.py
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
# Copyright 2017 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from threading import Lock
import weakref
class CallbackGroup:
"""Control when callbacks are allowed to be executed."""
def __init__(self):
super().__init__()
self.entities = set()
def add_entity(self, entity):
"""
Add an entity to the callback group.
:param entity: a subscription, timer, client, or service instance
:rtype: None
"""
self.entities.add(weakref.ref(entity))
def has_entity(self, entity):
"""
Determine if an entity has been added to this group.
:param entity: a subscription, timer, client, or service instance
:rtype: bool
"""
return weakref.ref(entity) in self.entities
def can_execute(self, entity):
"""
Return true if an entity can be executed.
:param entity: a subscription, timer, client, or service instance
:rtype: bool
"""
raise NotImplementedError()
def beginning_execution(self, entity):
"""
Get permission from the callback from the group to begin executing an entity.
Return true if the callback can be executed, false otherwise. If this returns True then
:func:`CallbackGroup.ending_execution` must be called after the callback has been executed.
:param entity: a subscription, timer, client, or service instance
:rtype: bool
"""
raise NotImplementedError()
def ending_execution(self, entity):
"""
Notify group that a callback has finished executing.
:param entity: a subscription, timer, client, or service instance
:rtype: None
"""
raise NotImplementedError()
class ReentrantCallbackGroup(CallbackGroup):
"""Allow callbacks to be executed in parallel without restriction."""
def can_execute(self, entity):
return True
def beginning_execution(self, entity):
return True
def ending_execution(self, entity):
pass
class MutuallyExclusiveCallbackGroup(CallbackGroup):
"""Allow only one callback to be executing at a time."""
def __init__(self):
super().__init__()
self._active_entity = None
self._lock = Lock()
def can_execute(self, entity):
with self._lock:
assert weakref.ref(entity) in self.entities
return self._active_entity is None
def beginning_execution(self, entity):
with self._lock:
assert weakref.ref(entity) in self.entities
if self._active_entity is None:
self._active_entity = entity
return True
return False
def ending_execution(self, entity):
with self._lock:
assert self._active_entity == entity
self._active_entity = None