_time.pxd 2.3 KB
Newer Older
Kirill Smelkov's avatar
Kirill Smelkov committed
1
# cython: language_level=2
2
3
# Copyright (C) 2019-2020  Nexedi SA and Contributors.
#                          Kirill Smelkov <[email protected]>
Kirill Smelkov's avatar
Kirill Smelkov committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
"""Package time mirrors Go package time.

22
23
24
25
26
 - `now` returns current time.
 - `sleep` pauses current task.
 - `Ticker` and `Timer` provide timers integrated with channels.
 - `tick`, `after` and `after_func` are convenience wrappers to use
   tickers and timers easily.
Kirill Smelkov's avatar
Kirill Smelkov committed
27

28
See also https://golang.org/pkg/time for Go time package documentation.
Kirill Smelkov's avatar
Kirill Smelkov committed
29
30
"""

31
32
from golang cimport chan, cbool, refptr

33
cdef extern from "golang/time.h" namespace "golang::time" nogil:
34
35
36
37
38
39
40
    const double second
    const double nanosecond
    const double microsecond
    const double millisecond
    const double minute
    const double hour

Kirill Smelkov's avatar
Kirill Smelkov committed
41
42
    void   sleep(double dt)
    double now()
43
44
45

    chan[double] tick(double dt)
    chan[double] after(double dt)
46
    Timer        after_func(double dt, ...)    # ... = func<void()>
47

48
    cppclass _Ticker:
49
50
51
        chan[double] c
        void stop()

52
    cppclass Ticker (refptr[_Ticker]):
53
54
55
56
57
58
59
        # Ticker.X = Ticker->X in C++.
        chan[double] c      "_ptr()->c"
        void         stop   "_ptr()->stop" ()

    Ticker new_ticker(double dt)


60
    cppclass _Timer:
61
62
63
64
        chan[double] c
        cbool stop()
        void  reset(double dt)

65
    cppclass Timer (refptr[_Timer]):
66
67
68
69
70
71
        # Timer.X = Timer->X in C++.
        chan[double] c      "_ptr()->c"
        cbool        stop   "_ptr()->stop"  ()
        void         reset  "_ptr()->reset" (double dt)

    Timer new_timer(double dt)