All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_timer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at 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, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <aerospike/as_std.h>
20 #include <stddef.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /*****************************************************************************
27  * TYPES
28  *****************************************************************************/
29 
30 struct as_timer_s;
31 /**
32  * The interface which all timer should implement.
33  */
34 typedef struct as_timer_hooks_s {
35  /**
36  * The destroy should free resources associated with the timer's source.
37  * The destroy should not free the timer itself.
38  */
39  int (* destroy)(struct as_timer_s *);
40  bool (* timedout)(const struct as_timer_s *);
41  uint64_t (* timeslice)(const struct as_timer_s *);
43 
44 /**
45  * Timer handle
46  */
47 typedef struct as_timer_s {
48  bool is_malloc;
49  void * source;
51 } as_timer;
52 
53 /*****************************************************************************
54  * FUNCTIONS
55  *****************************************************************************/
56 
57 /**
58  * Initialize a stack allocated timer
59  */
60 as_timer * as_timer_init(as_timer * timer, void * source, const as_timer_hooks * hooks);
61 
62 /**
63  * Heap allocate and initialize a timer
64  */
65 as_timer * as_timer_new(void * source, const as_timer_hooks * hooks);
66 
67 
68 static inline void * as_timer_source(const as_timer * tt) {
69  return (tt ? tt->source : NULL);
70 }
71 
72 /**
73  * Release resources associated with the timer.
74  * Calls timer->destroy. If success and if this is a heap allocated
75  * timer, then it will be freed.
76  */
77 int as_timer_destroy(as_timer * timer);
78 
79 /**
80  * true if timer has timedout
81  */
82 bool as_timer_timedout(const as_timer * timer);
83 
84 /**
85  * returns timeslice assigned for this timer
86  */
87 uint64_t as_timer_timeslice(const as_timer * timer);
88 
89 #ifdef __cplusplus
90 } // end extern "C"
91 #endif
as_timer * as_timer_init(as_timer *timer, void *source, const as_timer_hooks *hooks)
const as_timer_hooks * hooks
Definition: as_timer.h:50
static void * as_timer_source(const as_timer *tt)
Definition: as_timer.h:68
void * source
Definition: as_timer.h:49
uint64_t as_timer_timeslice(const as_timer *timer)
int as_timer_destroy(as_timer *timer)
as_timer * as_timer_new(void *source, const as_timer_hooks *hooks)
bool is_malloc
Definition: as_timer.h:48
bool as_timer_timedout(const as_timer *timer)