Multi-ApplicationOnlineProfiling  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Key_Entry.c
Go to the documentation of this file.
1 /* ############################ MALP License ############################## */
2 /* # Fri Jan 18 14:00:00 CET 2013 # */
3 /* # Copyright or (C) or Copr. Commissariat a l'Energie Atomique # */
4 /* # # */
5 /* # This software is governed by the CeCILL-C license under French law # */
6 /* # and abiding by the rules of distribution of free software. You can # */
7 /* # use, modify and/ or redistribute the software under the terms of # */
8 /* # the CeCILL-C license as circulated by CEA, CNRS and INRIA at the # */
9 /* # following URL http://www.cecill.info. # */
10 /* # # */
11 /* # The fact that you are presently reading this means that you have # */
12 /* # had knowledge of the CeCILL-C license and that you accept its # */
13 /* # terms. # */
14 /* # # */
15 /* # Authors: # */
16 /* # - BESNARD Jean-Baptiste jean-baptiste.besnard@cea.fr # */
17 /* # # */
18 /* ######################################################################## */
19 #include "Key_Entry.h"
20 #include "Knowledge_System.h"
21 
22 #define KEY_ENTRY_CL_COLL_AVOID 5
23 
24 
25 /* Init a key entry with givent key */
26 void Key_entry_init( struct Key_entry *ke, uint64_t key )
27 {
28  ke->freed = 0;
29  ke->lock = 0;
30  ke->key = key;
32 }
33 
34 /* Clears a key entry */
35 void Key_entry_release( struct Key_entry *ke )
36 {
37  ke->freed = 1;
38  ke->lock = 1;
39  ke->key = 0;
41 }
42 
43 
45 {
46  struct chained_list_view clv;
47  chained_list_view_init( &clv );
48 
49  struct Knowledge_system ** tmp = NULL;
50  int i = 0;
51 
52  while( ( tmp = (struct Knowledge_system **)Chained_List_get_nth_seq(&(ke->target_ks), &clv, i) ) )
53  {
54  if( tmp )
55  {
56  if( *tmp == NULL )
57  {
58  return tmp;
59  }
60  }
61  i++;
62  }
63 
64  return NULL;
65 }
66 
67 struct Knowledge_system ** Key_entry_get_ks( struct Key_entry *ke, struct Knowledge_system *ks )
68 {
69  struct chained_list_view clv;
70  chained_list_view_init( &clv );
71 
72  struct Knowledge_system ** tmp = NULL;
73  int i = 0;
74 
75 
76  while( ( tmp = (struct Knowledge_system **)Chained_List_get_nth_seq(&(ke->target_ks), &clv, i) ) )
77  {
78  if( tmp )
79  {
80  if( *tmp == ks )
81  {
82  return tmp;
83  }
84  }
85  i++;
86  }
87 
88  return NULL;
89 }
90 
91 
92 /* Add this Ks if not already present */
93 void Key_entry_push_ks( struct Key_entry *ke, struct Knowledge_system *ks )
94 {
96  {
97 
98  if( Key_entry_get_ks( ke, ks ) )
99  {
100  /* This ks is already registered */
102  return;
103  }
104 
105  struct Knowledge_system **empty_ks = NULL;
106 
107  if( (empty_ks = Key_entry_find_empty( ke ) ) != NULL )
108  {
109  /* Found an empty slot */
110  *empty_ks = ks;
111  }
112  else
113  {
114  /* Creating a new slot */
115  Chained_List_push(&ke->target_ks, (void*)&ks);
116  }
117 
118  }
120 }
121 
122 /* Remove all references to this Ks */
123 void Key_entry_pop_ks( struct Key_entry *ke, struct Knowledge_system *ks )
124 {
125  MALP_Spinlock_lock(&ke->lock);
126  {
127  struct Knowledge_system **to_delete = NULL;
128 
129  if( ( to_delete = Key_entry_get_ks( ke, ks ) ) != NULL )
130  {
131  *to_delete = NULL;
132  }
133  }
135 }
136 
137 /* Clear all references to Ks in this entry */
138 void Key_entry_clear_ks( struct Key_entry *ke )
139 {
140  MALP_Spinlock_lock(&ke->lock);
141  {
142  if( ke->target_ks.count != 0 )
143  {
145  /* A bit hacky but the simplest way to clear all nulled entries */
147  }
148  }
150 }
151 
152 /* Get the number of Ks in this entry */
154 {
155  int ret = 0;
156  MALP_Spinlock_lock(&ke->lock);
157  {
158  struct chained_list_view clv;
159  chained_list_view_init( &clv );
160 
161  struct Knowledge_system ** tmp = NULL;
162  int i = 0;
163 
164  while( ( tmp = (struct Knowledge_system **)Chained_List_get_nth_seq(&ke->target_ks, &clv, i) ) )
165  {
166  if( tmp )
167  {
168  if( *tmp )
169  {
170  ret++;
171  }
172  }
173  i++;
174  }
175  }
177 
178  return ret;
179 }
180 
181 int Key_entry_has_KS_type( struct Key_entry *ke, uint64_t type )
182 {
183  int ret = 0;
184  MALP_Spinlock_lock(&ke->lock);
185  {
186  struct chained_list_view clv;
187  chained_list_view_init( &clv );
188 
189  struct Knowledge_system ** tmp = NULL;
190  int i = 0;
191 
192  while( ( tmp = (struct Knowledge_system **)Chained_List_get_nth_seq(&ke->target_ks, &clv, i) ) )
193  {
194  if( tmp )
195  {
196  if( (*tmp)->type == type )
197  {
198  ret = 1;
199  break;
200  }
201  }
202  i++;
203  }
204 
205  }
207  return ret;
208 }
209 
210 
211 
212 /* Call a handler on every Ks >>> if handler returns 1 walk is interupted <<<*/
213 void Key_entry_walk( struct Key_entry *ke, int (*handler)( struct Knowledge_system *, void *), void *arg )
214 {
215  if( !handler )
216  return;
217 
218  MALP_Spinlock_lock(&ke->lock);
219  {
220  struct chained_list_view clv;
221  chained_list_view_init( &clv );
222 
223  struct Knowledge_system ** tmp = NULL;
224  int i = 0;
225 
226  while( ( tmp = (struct Knowledge_system **)Chained_List_get_nth_seq(&ke->target_ks, &clv, i) ) )
227  {
228  if( tmp )
229  {
230  if( (handler)( *tmp, arg ) )
231  break;
232  }
233  i++;
234  }
235 
236  }
238 }
struct Chained_List target_ks
the list of Knowledge_system identified by this key entry
Definition: Key_Entry.h:37
This struct is used to improve performance when sequentially walking through.
Definition: Chained_List.h:133
static void chained_list_view_init(struct chained_list_view *clv)
Initializes a chained_list_view.
Definition: Chained_List.h:142
#define KEY_ENTRY_CL_COLL_AVOID
Definition: Key_Entry.c:22
The Knowledge_system structure.
void Key_entry_push_ks(struct Key_entry *ke, struct Knowledge_system *ks)
associates a Knowledge_system to a key entry
Definition: Key_Entry.c:93
uint64_t count
the number of elements in the list
Definition: Chained_List.h:275
void Key_entry_clear_ks(struct Key_entry *ke)
Clear all references to Ks in this entry.
Definition: Key_Entry.c:138
void Key_entry_pop_ks(struct Key_entry *ke, struct Knowledge_system *ks)
Removes an association between a Knowledge_system and its key.
Definition: Key_Entry.c:123
static void Chained_List_init(struct Chained_List *list, uint64_t chunk_size, size_t payload_size)
Initializes a Chained_List.
Definition: Chained_List.h:286
uint64_t key
the key identifier
Definition: Key_Entry.h:39
int MALP_Spinlock_unlock(MALP_Spinlock *mutex)
Unlocks the given MALP_Spinlock.
Definition: Spinlock.c:41
MALP_Spinlock lock
a lock for concurrent access
Definition: Key_Entry.h:38
void Key_entry_init(struct Key_entry *ke, uint64_t key)
Init a key entry with given key.
Definition: Key_Entry.c:26
void Key_entry_walk(struct Key_entry *ke, int(*handler)(struct Knowledge_system *, void *), void *arg)
Call a handler on every Ks of a key entry.
Definition: Key_Entry.c:213
static void * Chained_List_push(struct Chained_List *list, void *payload)
Adds an element to the list.
Definition: Chained_List.h:312
int MALP_Spinlock_lock(MALP_Spinlock *mutex)
Locks the given MALP_Spinlock.
Definition: Spinlock.c:29
int Key_entry_has_KS_type(struct Key_entry *ke, uint64_t type)
Check if a KS type is present in a key entry.
Definition: Key_Entry.c:181
struct Knowledge_system ** Key_entry_get_ks(struct Key_entry *ke, struct Knowledge_system *ks)
Search in the key entry if a Knowledge_system is associated with it.
Definition: Key_Entry.c:67
static void Chained_List_release(struct Chained_List *list)
Clears a Chained_List.
Definition: Chained_List.h:298
struct Knowledge_system ** Key_entry_find_empty(struct Key_entry *ke)
Definition: Key_Entry.c:44
The Key_entry struct.
Definition: Key_Entry.h:35
void Key_entry_release(struct Key_entry *ke)
Clears a key entry.
Definition: Key_Entry.c:35
uint8_t freed
set to one when the entry is released, 0 otherwise
Definition: Key_Entry.h:40
static void * Chained_List_get_nth_seq(struct Chained_List *cl, struct chained_list_view *view, uint64_t n)
Get a reference to the nth element of the chained list (optimised for sequential) ...
Definition: Chained_List.h:401
int Key_entry_get_count(struct Key_entry *ke)
Get the number of Ks in this entry.
Definition: Key_Entry.c:153