1 | /* |
2 | * This Source Code Form is subject to the terms of the Mozilla Public |
3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
5 | * |
6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
7 | */ |
8 | |
9 | /* |
10 | * @f alarm |
11 | * @a M.L. Kersten, P. Boncz |
12 | * |
13 | * @+ Timers and Timed Interrupts |
14 | * This module handles various signaling/timer functionalities. |
15 | * The Monet interface supports two timer commands: @emph{ alarm} and @emph{ sleep}. |
16 | * Their argument is the number of seconds to wait before the timer goes off. |
17 | * The @emph{ sleep} command blocks till the alarm goes off. |
18 | * The @emph{ alarm} command continues directly, executes off a |
19 | * string when it goes off. |
20 | * The parameterless routines @emph{ time} and @emph{ ctime} provide access to |
21 | * the cpu clock.They return an integer and string, respectively. |
22 | */ |
23 | #include "monetdb_config.h" |
24 | #include "mal.h" |
25 | #include <signal.h> |
26 | #include <time.h> |
27 | |
28 | mal_export str ALARMusec(lng *ret); |
29 | mal_export str ALARMsleep(void *res, int *secs); |
30 | mal_export str ALARMctime(str *res); |
31 | mal_export str ALARMepoch(int *res); |
32 | mal_export str ALARMtime(int *res); |
33 | |
34 | #include "mal.h" |
35 | #include "mal_exception.h" |
36 | |
37 | |
38 | str |
39 | ALARMusec(lng *ret) |
40 | { |
41 | *ret = GDKusec(); |
42 | return MAL_SUCCEED; |
43 | } |
44 | |
45 | str |
46 | ALARMsleep(void *res, int *secs) |
47 | { |
48 | (void) res; /* fool compilers */ |
49 | if (*secs < 0) |
50 | throw(MAL, "alarm.sleep" , "negative delay" ); |
51 | |
52 | MT_sleep_ms(*secs * 1000); |
53 | return MAL_SUCCEED; |
54 | } |
55 | |
56 | str |
57 | ALARMctime(str *res) |
58 | { |
59 | time_t t = time(0); |
60 | char *base; |
61 | |
62 | #ifdef HAVE_CTIME_R3 |
63 | char buf[26]; |
64 | |
65 | base = ctime_r(&t, buf, sizeof(buf)); |
66 | #else |
67 | #ifdef HAVE_CTIME_R |
68 | char buf[26]; |
69 | |
70 | base = ctime_r(&t, buf); |
71 | #else |
72 | base = ctime(&t); |
73 | #endif |
74 | #endif |
75 | if (base == NULL) |
76 | /* very unlikely to happen... */ |
77 | throw(MAL, "alarm.ctime" , "failed to format time" ); |
78 | |
79 | base[24] = 0; /* squash final newline */ |
80 | *res = GDKstrdup(base); |
81 | if (*res == NULL) |
82 | throw(MAL, "alarm.ctime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
83 | return MAL_SUCCEED; |
84 | } |
85 | |
86 | str |
87 | ALARMepoch(int *res) /* XXX should be lng */ |
88 | { |
89 | *res = (int) time(0); |
90 | return MAL_SUCCEED; |
91 | } |
92 | |
93 | str |
94 | ALARMtime(int *res) |
95 | { |
96 | *res = GDKms(); |
97 | return MAL_SUCCEED; |
98 | } |
99 | |
100 | |