EM-ODP  3.7.0
Event Machine on ODP
em_hooks.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, Nokia Solutions and Networks
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef EM_HOOKS_H_
32 #define EM_HOOKS_H_
33 
34 /**
35  * @file
36  * EM internal API callback hook functions
37  */
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * EM API-callback and idle hooks init function (called once at startup).
45  */
47 hooks_init(const em_api_hooks_t *api_hooks, const em_idle_hooks_t *idle_hooks);
48 
49 /**
50  * Helper function for registering callback hook functions.
51  *
52  * @return EM_OK if there was room left to register a new callback
53  */
55 hook_register(uint8_t type, hook_fn_t hook_fn);
56 
57 /**
58  * Helper function for unregistering dispatcher callback functions.
59  *
60  * @return EM_OK if there was room left to register a new callback
61  */
63 hook_unregister(uint8_t type, hook_fn_t hook_fn);
64 
65 static inline void
66 call_api_hooks_alloc(const em_event_t events[], const int num_act,
67  const int num_req, uint32_t size, em_event_type_t type,
68  em_pool_t pool)
69 {
70  const hook_tbl_t *alloc_hook_tbl = em_shm->alloc_hook_tbl;
71  em_api_hook_alloc_t alloc_hook_fn;
72 
73  for (int i = 0; i < EM_CALLBACKS_MAX; i++) {
74  alloc_hook_fn = alloc_hook_tbl->tbl[i].alloc;
75  if (alloc_hook_fn == NULL)
76  return;
77  alloc_hook_fn(events, num_act, num_req, size, type, pool);
78  }
79 }
80 
81 static inline void
82 call_api_hooks_free(const em_event_t events[], const int num)
83 {
84  const hook_tbl_t *free_hook_tbl = em_shm->free_hook_tbl;
85  em_api_hook_free_t free_hook_fn;
86 
87  for (int i = 0; i < EM_CALLBACKS_MAX; i++) {
88  free_hook_fn = free_hook_tbl->tbl[i].free;
89  if (free_hook_fn == NULL)
90  return;
91  free_hook_fn(events, num);
92  }
93 }
94 
95 static inline void
96 call_api_hooks_send(const em_event_t events[], const int num,
97  em_queue_t queue, em_event_group_t event_group)
98 {
99  const hook_tbl_t *send_hook_tbl = em_shm->send_hook_tbl;
100  em_api_hook_send_t send_hook_fn;
101 
102  for (int i = 0; i < EM_CALLBACKS_MAX; i++) {
103  send_hook_fn = send_hook_tbl->tbl[i].send;
104  if (send_hook_fn == NULL)
105  return;
106  send_hook_fn(events, num, queue, event_group);
107  }
108 }
109 
110 static inline void call_idle_hooks_to_idle(uint64_t to_idle_delay_ns)
111 {
112  const hook_tbl_t *to_idle_hook_tbl = em_shm->to_idle_hook_tbl;
113  em_idle_hook_to_idle_t to_idle_hook_fn;
114 
115  for (int i = 0; i < EM_CALLBACKS_MAX; i++) {
116  to_idle_hook_fn = to_idle_hook_tbl->tbl[i].to_idle;
117  if (to_idle_hook_fn == NULL)
118  return;
119  to_idle_hook_fn(to_idle_delay_ns);
120  }
121 }
122 
123 static inline void call_idle_hooks_to_active(void)
124 {
125  const hook_tbl_t *to_active_hook_tbl = em_shm->to_active_hook_tbl;
126  em_idle_hook_to_active_t to_active_hook_fn;
127 
128  for (int i = 0; i < EM_CALLBACKS_MAX; i++) {
129  to_active_hook_fn = to_active_hook_tbl->tbl[i].to_active;
130  if (to_active_hook_fn == NULL)
131  return;
132  to_active_hook_fn();
133  }
134 }
135 
136 static inline void call_idle_hooks_while_idle(void)
137 {
138  const hook_tbl_t *while_idle_hook_tbl = em_shm->while_idle_hook_tbl;
139  em_idle_hook_while_idle_t while_idle_hook_fn;
140 
141  for (int i = 0; i < EM_CALLBACKS_MAX; i++) {
142  while_idle_hook_fn = while_idle_hook_tbl->tbl[i].while_idle;
143  if (while_idle_hook_fn == NULL)
144  return;
145  while_idle_hook_fn();
146  }
147 }
148 
149 #ifdef __cplusplus
150 }
151 #endif
152 
153 #endif /* EM_HOOKS_H_ */
em_shm_t::while_idle_hook_tbl
hook_tbl_t * while_idle_hook_tbl
Definition: em_mem.h:112
hook_register
em_status_t hook_register(uint8_t type, hook_fn_t hook_fn)
Definition: em_hooks.c:140
em_api_hooks_t
Definition: event_machine_hooks.h:214
em_idle_hook_to_active_t
void(* em_idle_hook_to_active_t)(void)
Definition: event_machine_hooks.h:186
em_shm_t::send_hook_tbl
hook_tbl_t * send_hook_tbl
Definition: em_mem.h:106
em_idle_hooks_t
Definition: event_machine_hooks.h:254
em_api_hook_send_t
void(* em_api_hook_send_t)(const em_event_t events[], int num, em_queue_t queue, em_event_group_t event_group)
Definition: event_machine_hooks.h:158
em_idle_hook_while_idle_t
void(* em_idle_hook_while_idle_t)(void)
Definition: event_machine_hooks.h:196
em_idle_hook_to_idle_t
void(* em_idle_hook_to_idle_t)(uint64_t to_idle_delay_ns)
Definition: event_machine_hooks.h:175
em_shm_t::free_hook_tbl
hook_tbl_t * free_hook_tbl
Definition: em_mem.h:104
em_shm_t::to_active_hook_tbl
hook_tbl_t * to_active_hook_tbl
Definition: em_mem.h:110
hook_unregister
em_status_t hook_unregister(uint8_t type, hook_fn_t hook_fn)
Definition: em_hooks.c:194
em_status_t
uint32_t em_status_t
Definition: event_machine_types.h:321
em_api_hook_alloc_t
void(* em_api_hook_alloc_t)(const em_event_t events[], int num_act, int num_req, uint32_t size, em_event_type_t type, em_pool_t pool)
Definition: event_machine_hooks.h:113
em_event_type_t
uint32_t em_event_type_t
Definition: event_machine_types.h:85
em_shm
em_shm_t * em_shm
Definition: event_machine_init.c:41
hooks_init
em_status_t hooks_init(const em_api_hooks_t *api_hooks, const em_idle_hooks_t *idle_hooks)
Definition: em_hooks.c:46
hook_tbl_t
Definition: em_hook_types.h:76
em_shm_t::alloc_hook_tbl
hook_tbl_t * alloc_hook_tbl
Definition: em_mem.h:102
hook_fn_t
Definition: em_hook_types.h:61
EM_CALLBACKS_MAX
#define EM_CALLBACKS_MAX
Definition: event_machine_config.h:234
em_api_hook_free_t
void(* em_api_hook_free_t)(const em_event_t events[], int num)
Definition: event_machine_hooks.h:138
em_shm_t::to_idle_hook_tbl
hook_tbl_t * to_idle_hook_tbl
Definition: em_mem.h:108