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