Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-sleep/src/sleep.c

raw
1 /*
2 ** mrb_sleep - sleep methods for mruby
3 **
4 ** Copyright (c) mod_mruby developers 2012-
5 ** Copyright (c) mruby developers 2018
6 **
7 ** Permission is hereby granted, free of charge, to any person obtaining
8 ** a copy of this software and associated documentation files (the
9 ** "Software"), to deal in the Software without restriction, including
10 ** without limitation the rights to use, copy, modify, merge, publish,
11 ** distribute, sublicense, and/or sell copies of the Software, and to
12 ** permit persons to whom the Software is furnished to do so, subject to
13 ** the following conditions:
14 **
15 ** The above copyright notice and this permission notice shall be
16 ** included in all copies or substantial portions of the Software.
17 **
18 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **
26 ** [ MIT license: https://www.opensource.org/licenses/mit-license.php ]
27 */
28
29 #include <time.h>
30 #ifdef _WIN32
31 #include <windows.h>
32 #define sleep(x) Sleep(x * 1000)
33 #define usleep(x) Sleep((DWORD)(((x)<1000) ? 1 : ((x)/1000)))
34 #else
35 #include <unistd.h>
36 #include <sys/time.h>
37 #endif
38
39 #include "mruby.h"
40
41 /* not implemented forever sleep (called without an argument)*/
42 static mrb_value
43 f_sleep(mrb_state *mrb, mrb_value self)
44 {
45 time_t beg = time(0);
46 time_t end;
47 #ifndef MRB_NO_FLOAT
48 mrb_float sec;
49
50 mrb_get_args(mrb, "f", &sec);
51 if (sec >= 0) {
52 usleep(sec * 1000000);
53 }
54 else {
55 mrb_raise(mrb, E_ARGUMENT_ERROR, "time interval must not be negative");
56 }
57 #else
58 mrb_int sec;
59
60 mrb_get_args(mrb, "i", &sec);
61 if (sec >= 0) {
62 sleep(sec);
63 }
64 else {
65 mrb_raise(mrb, E_ARGUMENT_ERROR, "time interval must not be negative");
66 }
67 #endif
68 end = time(0) - beg;
69
70 return mrb_fixnum_value((mrb_int)end);
71 }
72
73 /* mruby special; needed for mruby without float numbers */
74 static mrb_value
75 f_usleep(mrb_state *mrb, mrb_value self)
76 {
77 mrb_int usec;
78 #ifdef _WIN32
79 FILETIME st_ft,ed_ft;
80 unsigned __int64 st_time = 0;
81 unsigned __int64 ed_time = 0;
82 #else
83 struct timeval st_tm,ed_tm;
84 #endif
85 time_t slp_tm;
86
87 #ifdef _WIN32
88 GetSystemTimeAsFileTime(&st_ft);
89 #else
90 gettimeofday(&st_tm, NULL);
91 #endif
92
93 /* not implemented forever sleep (called without an argument)*/
94 mrb_get_args(mrb, "i", &usec);
95
96 if (usec >= 0) {
97 usleep(usec);
98 }
99 else {
100 mrb_raise(mrb, E_ARGUMENT_ERROR, "time interval must not be negative");
101 }
102
103 #ifdef _WIN32
104 GetSystemTimeAsFileTime(&ed_ft);
105
106 st_time |= st_ft.dwHighDateTime;
107 st_time <<=32;
108 st_time |= st_ft.dwLowDateTime;
109 ed_time |= ed_ft.dwHighDateTime;
110 ed_time <<=32;
111 ed_time |= ed_ft.dwLowDateTime;
112
113 slp_tm = (ed_time - st_time) / 10;
114 #else
115 gettimeofday(&ed_tm, NULL);
116
117 if (st_tm.tv_usec > ed_tm.tv_usec) {
118 slp_tm = 1000000 + ed_tm.tv_usec - st_tm.tv_usec;
119 }
120 else {
121 slp_tm = ed_tm.tv_usec - st_tm.tv_usec;
122 }
123 #endif
124
125 return mrb_fixnum_value((mrb_int)slp_tm);
126 }
127
128 void
129 mrb_mruby_sleep_gem_init(mrb_state *mrb)
130 {
131 mrb_define_method(mrb, mrb->kernel_module, "sleep", f_sleep, MRB_ARGS_REQ(1));
132 mrb_define_method(mrb, mrb->kernel_module, "usleep", f_usleep, MRB_ARGS_REQ(1));
133 }
134
135 void
136 mrb_mruby_sleep_gem_final(mrb_state *mrb)
137 {
138 }