blob: 0f647c2c7d6414adaf250f0b6a2e0f5703930afe [file] [log] [blame]
Pau Espin Pedrol927344b2017-05-22 16:38:49 +02001# osmo_gsm_tester: Event loop
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Pau Espin Pedrol927344b2017-05-22 16:38:49 +02009# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Pau Espin Pedrol927344b2017-05-22 16:38:49 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Pau Espin Pedrol927344b2017-05-22 16:38:49 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Pau Espin Pedrol927344b2017-05-22 16:38:49 +020020import time
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020021from gi.repository import GLib, GObject
22
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020023from . import log
24
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020025class DeferredHandling:
26 defer_queue = []
27
28 def handle_queue(self):
29 while DeferredHandling.defer_queue:
30 handler, args, kwargs = self.defer_queue.pop(0)
31 handler(*args, **kwargs)
32
33 def defer(self, handler, *args, **kwargs):
34 self.defer_queue.append((handler, args, kwargs))
35
36class WaitRequest:
37 timeout_ack = False
38 condition_ack = False
39
40 def __init__(self, condition, condition_args, condition_kwargs, timeout, timestep):
41 self.timeout_started = time.time()
42 self.timeout = timeout
43 self.condition = condition
44 self.condition_args = condition_args
45 self.condition_kwargs = condition_kwargs
46
47 def condition_check(self):
48 #print("_wait_condition_check")
49 waited = time.time() - self.timeout_started
50 if self.condition(*self.condition_args, **self.condition_kwargs):
51 self.condition_ack = True
52 elif waited > self.timeout:
53 self.timeout_ack = True
54
55class EventLoop:
56 poll_funcs = []
57 gloop = None
58 gctx = None
59 deferred_handling = None
60
61 def __init__(self):
62 self.gloop = GLib.MainLoop()
63 self.gctx = self.gloop.get_context()
64 self.deferred_handling = DeferredHandling()
65
66 def _trigger_cb_func(self, user_data):
67 self.defer(user_data)
68 return True #to retrigger the timeout
69
70 def defer(self, handler, *args, **kwargs):
71 self.deferred_handling.defer(handler, *args, **kwargs)
72
73 def register_poll_func(self, func, timestep=1):
74 id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, func) # in 1/1000th of a sec
75 self.poll_funcs.append((func, id))
76
77 def unregister_poll_func(self, func):
78 for pair in self.poll_funcs:
79 f, id = pair
80 if f == func:
81 GObject.source_remove(id)
82 self.poll_funcs.remove(pair)
83 return
84
85 def poll(self, may_block=False):
86 self.gctx.iteration(may_block)
87 self.deferred_handling.handle_queue()
88
89 def wait_no_raise(self, log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
90 if not timeout or timeout < 0:
91 self = log_obj
92 raise log.Error('wait() *must* time out at some point.', timeout=timeout)
93 if timestep < 0.1:
94 timestep = 0.1
95
96 wait_req = WaitRequest(condition, condition_args, condition_kwargs, timeout, timestep)
97 wait_id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, wait_req.condition_check)
98 while True:
Pau Espin Pedrolb6ab1da2018-04-07 17:26:43 +020099 try:
100 self.poll(may_block=True)
101 except Exception: # cleanup of temporary resources in the wait scope
102 GObject.source_remove(wait_id)
103 raise
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200104 if wait_req.condition_ack or wait_req.timeout_ack:
105 GObject.source_remove(wait_id)
106 success = wait_req.condition_ack
107 return success
108
109 def wait(self, log_obj, condition, *condition_args, timeout=300, timestep=1, **condition_kwargs):
110 if not self.wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
111 log.ctx(log_obj)
Pau Espin Pedrole5236652018-05-28 12:46:42 +0200112 raise log.Error('Wait timeout', condition=condition, timeout=timeout, timestep=timestep)
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200113
114 def sleep(self, log_obj, seconds):
115 assert seconds > 0.
116 self.wait_no_raise(log_obj, lambda: False, [], {}, timeout=seconds, timestep=seconds)
117
118
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200119MainLoop = EventLoop()
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200120
121
122# vim: expandtab tabstop=4 shiftwidth=4