timeval.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
24 #include "timeval.h"
25 
26 #include <ext/spl/spl_exceptions.h>
27 #include <zend_exceptions.h>
28 
29 zend_class_entry *grpc_ce_timeval;
30 PHP_GRPC_DECLARE_OBJECT_HANDLER(timeval_ce_handlers)
31 
32 /* Frees and destroys an instance of wrapped_grpc_call */
33 PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_timeval)
35 
36 /* Initializes an instance of wrapped_grpc_timeval to be associated with an
37  * object of a class specified by class_type */
39  TSRMLS_DC) {
40  PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_timeval);
41  zend_object_std_init(&intern->std, class_type TSRMLS_CC);
42  object_properties_init(&intern->std, class_type);
43  PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_timeval, timeval_ce_handlers);
44 }
45 
47  zval *timeval_object;
48  PHP_GRPC_MAKE_STD_ZVAL(timeval_object);
49  object_init_ex(timeval_object, grpc_ce_timeval);
50  wrapped_grpc_timeval *timeval =
51  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, timeval_object);
52  memcpy(&timeval->wrapped, &wrapped, sizeof(gpr_timespec));
53  return timeval_object;
54 }
55 
60 PHP_METHOD(Timeval, __construct) {
61  wrapped_grpc_timeval *timeval =
62  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis());
63  int64_t microseconds = 0;
64 
65  /* parse $microseconds as long */
66  if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
67  ZEND_NUM_ARGS() TSRMLS_CC, "l",
68  &microseconds) == FAILURE) {
69  double microsecondsDouble = 0.0;
70  /* parse $microseconds as double */
71  if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
72  ZEND_NUM_ARGS() TSRMLS_CC, "d",
73  &microsecondsDouble) == FAILURE) {
74  zend_throw_exception(spl_ce_InvalidArgumentException,
75  "Timeval expects a long or double", 1 TSRMLS_CC);
76  return;
77  }
78  microseconds = (int64_t)microsecondsDouble;
79  }
80  gpr_timespec time = gpr_time_from_micros(microseconds, GPR_TIMESPAN);
81  memcpy(&timeval->wrapped, &time, sizeof(gpr_timespec));
82 }
83 
90 PHP_METHOD(Timeval, add) {
91  zval *other_obj;
92 
93  /* "O" == 1 Object */
94  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &other_obj,
95  grpc_ce_timeval) == FAILURE) {
96  zend_throw_exception(spl_ce_InvalidArgumentException,
97  "add expects a Timeval", 1 TSRMLS_CC);
98  return;
99  }
100  wrapped_grpc_timeval *self =
101  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis());
102  wrapped_grpc_timeval *other =
103  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, other_obj);
104  zval *sum =
105  grpc_php_wrap_timeval(gpr_time_add(self->wrapped, other->wrapped)
106  TSRMLS_CC);
108 }
109 
116 PHP_METHOD(Timeval, subtract) {
117  zval *other_obj;
118 
119  /* "O" == 1 Object */
120  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &other_obj,
121  grpc_ce_timeval) == FAILURE) {
122  zend_throw_exception(spl_ce_InvalidArgumentException,
123  "subtract expects a Timeval", 1 TSRMLS_CC);
124  return;
125  }
126  wrapped_grpc_timeval *self =
127  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis());
128  wrapped_grpc_timeval *other =
129  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, other_obj);
130  zval *diff =
131  grpc_php_wrap_timeval(gpr_time_sub(self->wrapped, other->wrapped)
132  TSRMLS_CC);
134 }
135 
143 PHP_METHOD(Timeval, compare) {
144  zval *a_obj;
145  zval *b_obj;
146 
147  /* "OO" == 2 Objects */
148  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OO", &a_obj,
149  grpc_ce_timeval, &b_obj,
150  grpc_ce_timeval) == FAILURE) {
151  zend_throw_exception(spl_ce_InvalidArgumentException,
152  "compare expects two Timevals", 1 TSRMLS_CC);
153  return;
154  }
155  wrapped_grpc_timeval *a =
156  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, a_obj);
157  wrapped_grpc_timeval *b =
158  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, b_obj);
159  long result = gpr_time_cmp(a->wrapped, b->wrapped);
160  RETURN_LONG(result);
161 }
162 
170 PHP_METHOD(Timeval, similar) {
171  zval *a_obj;
172  zval *b_obj;
173  zval *thresh_obj;
174 
175  /* "OOO" == 3 Objects */
176  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OOO", &a_obj,
178  &thresh_obj, grpc_ce_timeval) == FAILURE) {
179  zend_throw_exception(spl_ce_InvalidArgumentException,
180  "compare expects three Timevals", 1 TSRMLS_CC);
181  return;
182  }
183  wrapped_grpc_timeval *a =
184  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, a_obj);
185  wrapped_grpc_timeval *b =
186  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, b_obj);
187  wrapped_grpc_timeval *thresh =
188  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, thresh_obj);
189  int result = gpr_time_similar(a->wrapped, b->wrapped, thresh->wrapped);
190  RETURN_BOOL(result);
191 }
192 
197 PHP_METHOD(Timeval, now) {
200 }
201 
206 PHP_METHOD(Timeval, zero) {
207  zval *grpc_php_timeval_zero =
209  RETURN_DESTROY_ZVAL(grpc_php_timeval_zero);
210 }
211 
216 PHP_METHOD(Timeval, infFuture) {
217  zval *grpc_php_timeval_inf_future =
219  RETURN_DESTROY_ZVAL(grpc_php_timeval_inf_future);
220 }
221 
226 PHP_METHOD(Timeval, infPast) {
227  zval *grpc_php_timeval_inf_past =
229  RETURN_DESTROY_ZVAL(grpc_php_timeval_inf_past);
230 }
231 
236 PHP_METHOD(Timeval, sleepUntil) {
237  wrapped_grpc_timeval *this =
238  PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis());
239  gpr_sleep_until(this->wrapped);
240 }
241 
242 ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 1)
243  ZEND_ARG_INFO(0, microseconds)
244 ZEND_END_ARG_INFO()
245 
246 ZEND_BEGIN_ARG_INFO_EX(arginfo_add, 0, 0, 1)
247  ZEND_ARG_INFO(0, timeval)
248 ZEND_END_ARG_INFO()
249 
250 ZEND_BEGIN_ARG_INFO_EX(arginfo_compare, 0, 0, 2)
251  ZEND_ARG_INFO(0, a_timeval)
252  ZEND_ARG_INFO(0, b_timeval)
253 ZEND_END_ARG_INFO()
254 
255 ZEND_BEGIN_ARG_INFO_EX(arginfo_infFuture, 0, 0, 0)
256 ZEND_END_ARG_INFO()
257 
258 ZEND_BEGIN_ARG_INFO_EX(arginfo_infPast, 0, 0, 0)
259 ZEND_END_ARG_INFO()
260 
261 ZEND_BEGIN_ARG_INFO_EX(arginfo_now, 0, 0, 0)
262 ZEND_END_ARG_INFO()
263 
264 ZEND_BEGIN_ARG_INFO_EX(arginfo_similar, 0, 0, 3)
265  ZEND_ARG_INFO(0, a_timeval)
266  ZEND_ARG_INFO(0, b_timeval)
267  ZEND_ARG_INFO(0, threshold_timeval)
268 ZEND_END_ARG_INFO()
269 
270 ZEND_BEGIN_ARG_INFO_EX(arginfo_sleepUntil, 0, 0, 0)
271 ZEND_END_ARG_INFO()
272 
273 ZEND_BEGIN_ARG_INFO_EX(arginfo_subtract, 0, 0, 1)
274  ZEND_ARG_INFO(0, timeval)
275 ZEND_END_ARG_INFO()
276 
277 ZEND_BEGIN_ARG_INFO_EX(arginfo_zero, 0, 0, 0)
278 ZEND_END_ARG_INFO()
279 
280 static zend_function_entry timeval_methods[] = {
281  PHP_ME(Timeval, __construct, arginfo_construct,
282  ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
283  PHP_ME(Timeval, add, arginfo_add,
284  ZEND_ACC_PUBLIC)
285  PHP_ME(Timeval, compare, arginfo_compare,
286  ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
287  PHP_ME(Timeval, infFuture, arginfo_infFuture,
288  ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
289  PHP_ME(Timeval, infPast, arginfo_infPast,
290  ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
291  PHP_ME(Timeval, now, arginfo_now,
292  ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
293  PHP_ME(Timeval, similar, arginfo_similar,
294  ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
295  PHP_ME(Timeval, sleepUntil, arginfo_sleepUntil,
296  ZEND_ACC_PUBLIC)
297  PHP_ME(Timeval, subtract, arginfo_subtract,
298  ZEND_ACC_PUBLIC)
299  PHP_ME(Timeval, zero, arginfo_zero,
300  ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
301  PHP_FE_END
302 };
303 
304 void grpc_init_timeval(TSRMLS_D) {
305  zend_class_entry ce;
306  INIT_CLASS_ENTRY(ce, "Grpc\\Timeval", timeval_methods);
307  ce.create_object = create_wrapped_grpc_timeval;
308  grpc_ce_timeval = zend_register_internal_class(&ce TSRMLS_CC);
309  PHP_GRPC_INIT_HANDLER(wrapped_grpc_timeval, timeval_ce_handlers);
310 }
311 
312 void grpc_shutdown_timeval(TSRMLS_D) {}
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
grpc_init_timeval
void grpc_init_timeval(TSRMLS_D)
Definition: timeval.c:304
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
now
static double now(void)
Definition: test/core/fling/client.cc:130
gpr_time_similar
GPRAPI int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold)
Definition: src/core/lib/gpr/time.cc:206
php_grpc_zend_object
#define php_grpc_zend_object
Definition: php7_wrapper.h:27
gpr_time_0
GPRAPI gpr_timespec gpr_time_0(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:47
grpc::testing::sum
double sum(const T &container, F functor)
Definition: test/cpp/qps/stats.h:30
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
PHP_GRPC_INIT_HANDLER
#define PHP_GRPC_INIT_HANDLER(class_object, handler_name)
Definition: php7_wrapper.h:140
PHP_GRPC_FREE_WRAPPED_FUNC_END
#define PHP_GRPC_FREE_WRAPPED_FUNC_END()
Definition: php7_wrapper.h:73
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
timeval_methods
static zend_function_entry timeval_methods[]
Definition: timeval.c:280
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
PHP_GRPC_DECLARE_OBJECT_HANDLER
#define PHP_GRPC_DECLARE_OBJECT_HANDLER(handler_name)
Definition: php7_wrapper.h:146
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
PHP_GRPC_GET_WRAPPED_OBJECT
#define PHP_GRPC_GET_WRAPPED_OBJECT(class_object, zv)
Definition: php7_wrapper.h:149
gpr_time_sub
GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:168
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
bm_diff.diff
diff
Definition: bm_diff.py:274
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
timeval.h
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc_php_wrap_timeval
zval * grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC)
Definition: timeval.c:46
grpc_shutdown_timeval
void grpc_shutdown_timeval(TSRMLS_D)
Definition: timeval.c:312
PHP_METHOD
PHP_METHOD(Timeval, __construct)
Definition: timeval.c:60
add
static void add(const char *beg, const char *end, char ***ss, size_t *ns)
Definition: debug/trace.cc:96
gpr_inf_past
GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:63
wrapped
grpc_call * wrapped
Definition: src/php/ext/grpc/call.h:32
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
intern
upb_strtable_uninit & intern
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:222
timeval
Definition: setup_once.h:113
PHP_GRPC_MAKE_STD_ZVAL
#define PHP_GRPC_MAKE_STD_ZVAL(pzv)
Definition: php7_wrapper.h:42
PHP_GRPC_ALLOC_CLASS_OBJECT
#define PHP_GRPC_ALLOC_CLASS_OBJECT(class_object)
Definition: php7_wrapper.h:77
gpr_time_from_micros
GPRAPI gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:115
PHP_GRPC_FREE_CLASS_OBJECT
#define PHP_GRPC_FREE_CLASS_OBJECT(class_object, handler)
Definition: php7_wrapper.h:82
grpc_ce_timeval
zend_class_entry * grpc_ce_timeval
Definition: timeval.c:29
gpr_timespec
Definition: gpr_types.h:50
compare
Definition: benchmark/tools/compare.py:1
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
PHP_GRPC_FREE_WRAPPED_FUNC_START
#define PHP_GRPC_FREE_WRAPPED_FUNC_START(class_object)
Definition: php7_wrapper.h:70
RETURN_DESTROY_ZVAL
#define RETURN_DESTROY_ZVAL(val)
Definition: php7_wrapper.h:55
create_wrapped_grpc_timeval
php_grpc_zend_object create_wrapped_grpc_timeval(zend_class_entry *class_type TSRMLS_DC)
Definition: timeval.c:38


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:39