EM-ODP  3.7.0
Event Machine on ODP
em_atomic_group.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 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 /**
32  * @file
33  * EM internal atomic group functions
34  *
35  */
36 
37 #ifndef EM_ATOMIC_GROUP_H_
38 #define EM_ATOMIC_GROUP_H_
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 #define invalid_atomic_group(atomic_group) \
45  ((unsigned int)agrp_hdl2idx((atomic_group)) >= EM_MAX_ATOMIC_GROUPS)
46 
48 atomic_group_init(atomic_group_tbl_t *const atomic_group_tbl,
49  atomic_group_pool_t *const atomic_group_pool);
50 
51 em_atomic_group_t
52 atomic_group_alloc(void);
53 
55 atomic_group_free(em_atomic_group_t atomic_group);
56 
57 void atomic_group_remove_queue(queue_elem_t *const q_elem);
58 
59 void atomic_group_dispatch(odp_event_t odp_evtbl[], const int num_events,
60  const queue_elem_t *q_elem);
61 
62 static inline int
63 atomic_group_allocated(const atomic_group_elem_t *agrp_elem)
64 {
65  return !objpool_in_pool(&agrp_elem->atomic_group_pool_elem);
66 }
67 
68 static inline int
69 agrp_hdl2idx(const em_atomic_group_t atomic_group)
70 {
71  return (int)((uintptr_t)atomic_group - 1);
72 }
73 
74 static inline em_atomic_group_t
75 agrp_idx2hdl(const int atomic_group_index)
76 {
77  return (em_atomic_group_t)(uintptr_t)(atomic_group_index + 1);
78 }
79 
80 static inline atomic_group_elem_t *
81 atomic_group_elem_get(const em_atomic_group_t atomic_group)
82 {
83  const int ag_idx = agrp_hdl2idx(atomic_group);
84  atomic_group_elem_t *ag_elem;
85 
86  if (unlikely((unsigned int)ag_idx > EM_MAX_ATOMIC_GROUPS - 1))
87  return NULL;
88 
89  ag_elem = &em_shm->atomic_group_tbl.ag_elem[ag_idx];
90 
91  return ag_elem;
92 }
93 
94 static inline void
95 atomic_group_add_queue_list(atomic_group_elem_t *const ag_elem,
96  queue_elem_t *const q_elem)
97 {
98  env_spinlock_lock(&ag_elem->lock);
99  list_add(&ag_elem->qlist_head, &q_elem->agrp.agrp_node);
100  env_atomic32_inc(&ag_elem->num_queues);
101  env_spinlock_unlock(&ag_elem->lock);
102 }
103 
104 static inline void
105 atomic_group_rem_queue_list(atomic_group_elem_t *const ag_elem,
106  queue_elem_t *const q_elem)
107 {
108  env_spinlock_lock(&ag_elem->lock);
109  if (!list_is_empty(&ag_elem->qlist_head)) {
110  list_rem(&ag_elem->qlist_head, &q_elem->agrp.agrp_node);
111  env_atomic32_dec(&ag_elem->num_queues);
112  }
113  env_spinlock_unlock(&ag_elem->lock);
114 }
115 
116 static inline void
117 atomic_group_release(void)
118 {
119  em_locm_t *const locm = &em_locm;
120  const queue_elem_t *q_elem = locm->current.sched_q_elem;
121  em_atomic_group_t atomic_group = q_elem->agrp.atomic_group;
122  atomic_group_elem_t *const agrp_elem = atomic_group_elem_get(atomic_group);
123 
124  locm->atomic_group_released = true;
125  env_spinlock_unlock(&agrp_elem->lock);
126 }
127 
128 unsigned int
129 atomic_group_count(void);
130 
131 /** Print information about all atomic groups */
132 void print_atomic_group_info(void);
133 
134 /** Print information about all queues of the given atomic group */
135 void print_atomic_group_queues(em_atomic_group_t ag);
136 
137 void print_ag_elem_info(void);
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif /* EM_ATOMIC_GROUP_H_ */
atomic_group_elem_t::qlist_head
list_node_t qlist_head
Definition: em_atomic_group_types.h:67
atomic_group_init
em_status_t atomic_group_init(atomic_group_tbl_t *const atomic_group_tbl, atomic_group_pool_t *const atomic_group_pool)
Definition: em_atomic_group.c:38
atomic_group_elem_t::num_queues
env_atomic32_t num_queues
Definition: em_atomic_group_types.h:64
atomic_group_alloc
em_atomic_group_t atomic_group_alloc(void)
Definition: em_atomic_group.c:89
em_locm
ENV_LOCAL em_locm_t em_locm
ODP_PACKED::sched_q_elem
queue_elem_t * sched_q_elem
Definition: em_mem.h:176
em_locm_t::current
em_locm_current_t current
Definition: em_mem.h:190
EM_MAX_ATOMIC_GROUPS
#define EM_MAX_ATOMIC_GROUPS
Definition: event_machine_config.h:137
print_atomic_group_queues
void print_atomic_group_queues(em_atomic_group_t ag)
Definition: em_atomic_group.c:401
print_atomic_group_info
void print_atomic_group_info(void)
Definition: em_atomic_group.c:330
atomic_group_remove_queue
void atomic_group_remove_queue(queue_elem_t *const q_elem)
Definition: em_atomic_group.c:126
atomic_group_elem_t
Definition: em_atomic_group_types.h:46
atomic_group_pool_t
Definition: em_atomic_group_types.h:85
em_status_t
uint32_t em_status_t
Definition: event_machine_types.h:321
em_locm_t::atomic_group_released
bool atomic_group_released
Definition: em_mem.h:201
em_shm
em_shm_t * em_shm
Definition: event_machine_init.c:41
q_elem_atomic_group_::agrp_node
list_node_t agrp_node
Definition: em_queue_types.h:160
atomic_group_tbl_t
Definition: em_atomic_group_types.h:77
q_elem_atomic_group_::atomic_group
em_atomic_group_t atomic_group
Definition: em_queue_types.h:158
em_locm_t
Definition: em_mem.h:188
atomic_group_elem_t::atomic_group_pool_elem
objpool_elem_t atomic_group_pool_elem
Definition: em_atomic_group_types.h:52
queue_elem_t
Definition: em_queue_types.h:180