/[pdpsoft]/trunk/grid-mw-security/ees/src/aos/aos.c
ViewVC logotype

Diff of /trunk/grid-mw-security/ees/src/aos/aos.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1744 by aramv, Mon Jun 7 13:22:26 2010 UTC revision 1745 by aramv, Wed Jun 9 10:09:23 2010 UTC
# Line 4  Line 4 
4    
5  static pthread_key_t           _aos_key;  static pthread_key_t           _aos_key;
6  static pthread_once_t          _aos_key_once = PTHREAD_ONCE_INIT; /* Normally not okay to do initialisation outside of a function, but this way it (hopefully) will only be set one - during compile time */  static pthread_once_t          _aos_key_once = PTHREAD_ONCE_INIT; /* Normally not okay to do initialisation outside of a function, but this way it (hopefully) will only be set one - during compile time */
7  static struct aos_storage_s *  _global_storage;  static struct aos_state_s *  _global_state;
8    
9  /* internal methods */  /* internal methods */
10    
# Line 25  void aos_start_threading(){ Line 25  void aos_start_threading(){
25  /**  /**
26   * returns a pointer to an aos_storage_t struct that corresponds to the current thread context. If the current context is not threaded, a pointer to a global storage struct is returned.   * returns a pointer to an aos_storage_t struct that corresponds to the current thread context. If the current context is not threaded, a pointer to a global storage struct is returned.
27   */   */
28  aos_storage_t * aos_get_storage(){  aos_state_t * aos_get_state(void){
29    pid_t             tid;    pid_t             tid;
30    aos_storage_t  *  storage = NULL;    aos_state_t *     state = NULL;
31    
32    tid = syscall(SYS_gettid);    tid = syscall(SYS_gettid);
33    /*tid = gettid();*/    /*tid = gettid();*/
34    
35    if(!_is_threading){    if(!_is_threading){
36      return _global_storage;      return _global_state;
37    }    }
38    
39    /*  Try to get TLS */    /*  Try to get TLS */
40    storage = pthread_getspecific(_aos_key);    state = pthread_getspecific(_aos_key);
41    if(storage == 0) {    if(state == 0) {
42      /* Create new TLS */      /* Create new TLS */
43      if((storage = calloc(1, sizeof(aos_storage_t)))){      if((state = calloc(1, sizeof(aos_state_t)))){
44        storage->last_context = NULL;        state->last_context = NULL;
45        pthread_setspecific(_aos_key, storage);        if((state->current_storage = calloc(1, sizeof(aos_storage_t)))){
46        return storage;          state->current_storage->list_contexts = NULL;
47            _is_initialized = 1;
48            pthread_setspecific(_aos_key, state);
49            state->saved_storage = state->current_storage;
50            return state;
51          }
52      }      }
53    } else {    } else {
54      return storage;      return state;
55    }    }
56    return NULL;    return NULL;
57  }  }
58    
59    aos_storage_t* aos_get_storage(){
60      aos_state_t* iterator = aos_get_state();
61      return iterator->current_storage;
62    }
63    
64  /**  /**
65   * Frees storage struct pointed to by storage pointer   * Frees storage struct pointed to by storage pointer
66   */   */
# Line 117  aos_context_t * createContext(aos_contex Line 127  aos_context_t * createContext(aos_contex
127   * Adds supplied context to storage attained through aos_get_storage()   * Adds supplied context to storage attained through aos_get_storage()
128   */   */
129  EES_RC addContext(aos_context_t* context){  EES_RC addContext(aos_context_t* context){
130    aos_storage_t* storage = localize_storage(aos_get_storage());    aos_state_t* iterator = aos_get_state();
131      aos_storage_t* storage = iterator->current_storage;
132    aos_context_t* tmp_context = NULL;    aos_context_t* tmp_context = NULL;
133    
134    EEF_log(LOG_DEBUG, "Adding context at %p to storage at %p\n", context, storage);    EEF_log(LOG_DEBUG, "Adding context at %p to storage at %p\n", context, iterator->current_storage);
135    /*printf("Adding context at %p to storage at %p\n", context, storage);*/    /*printf("Adding context at %p to storage at %p\n", context, storage);*/
136      if(!storage){
137        EEF_log(LOG_ERR, "No storage available at %p", storage);
138        return EES_FAILURE;
139      }
140    if(storage->list_contexts != NULL){    if(storage->list_contexts != NULL){
141      if((tmp_context = storage->list_contexts)!=NULL){      if((tmp_context = storage->list_contexts)!=NULL){
142        while(tmp_context->next){        while(tmp_context->next){
# Line 140  EES_RC addContext(aos_context_t* context Line 155  EES_RC addContext(aos_context_t* context
155      EEF_log(LOG_DEBUG, "Created at %p\n", context);      EEF_log(LOG_DEBUG, "Created at %p\n", context);
156      /*printf("List created at %p\n", context);*/      /*printf("List created at %p\n", context);*/
157    }    }
158    aos_set_iterator(context, storage);    aos_set_iterator(context);
159    
160    /*eef_log(LOG_DEBUG, "Copied context to %p\n", storage->last_context);*/    /*eef_log(LOG_DEBUG, "Copied context to %p\n", storage->last_context);*/
161    return EES_SUCCESS;    return EES_SUCCESS;
162  }  }
163    
164  EES_RC rewindContexts(aos_storage_t* storage){  EES_RC rewindContexts(aos_storage_t* storage){
165    aos_storage_t* tmp_storage = localize_storage(storage);    aos_state_t*    state = aos_get_state();
166    if(tmp_storage){    /* not using storage parameter anymore */
     aos_set_iterator(tmp_storage->list_contexts, tmp_storage);  
   }  
   return EES_SUCCESS;  
 }  
167    
168  aos_context_t* aos_get_iterator(aos_storage_t* storage){    if(state){
169    aos_context_t* current_context = NULL;      state->current_storage = state->saved_storage;
   aos_storage_t* tmp_storage = localize_storage(storage);  
170    
171    if(tmp_storage){      if(state->current_storage){
172      current_context = tmp_storage->last_context;        aos_set_iterator(state->current_storage->list_contexts);
173    } else {        return EES_SUCCESS;
174      EEF_log(LOG_ERR, "There was an error getting the storage!\n");      } else {
175          EEF_log(LOG_ERR, "Unable to aquire AOS handle in rewindContexts()\n");
176        }
177    }    }
178      return EES_FAILURE;
   return current_context;  
179  }  }
180    
181  aos_storage_t* localize_storage(aos_storage_t* storage){  EES_RC aos_set_iterator(aos_context_t* context){
182    aos_storage_t* tmp_storage = NULL;    aos_state_t* state = aos_get_state();
183    /* if we are using the global storage and are threading... */    if(state->current_storage){
184    /* we need to use the local iterator */      /*printf("Setting iterator for %p to %p\n", state->current_storage, context);*/
185    if(((storage == _global_storage) && (_is_threading)) || (storage == NULL)){      state->last_context = context;
186      /* get some local storage to save global iterator to */      return EES_SUCCESS;
     tmp_storage = aos_get_storage();  
187    } else {    } else {
188      /* otherwise use the passed storage */      EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", state->current_storage);
     tmp_storage = storage;  
189    }    }
190      return EES_FAILURE;
   return tmp_storage;  
191  }  }
192    
193  EES_RC aos_set_iterator(aos_context_t* context, aos_storage_t* storage){  aos_context_t* aos_get_iterator(void){
194    aos_storage_t* tmp_storage = localize_storage(storage);    aos_state_t* state = aos_get_state();
195    if((storage == _global_storage) && (_is_threading)){    if(state->current_storage){
196      EEF_log(LOG_ERR, "Unable to set iterator in global storage while threading.\n");      /*printf("Getting iterator for %p as %p\n", state->current_storage, state->last_context);*/
197      return EES_FAILURE;      if(state->last_context){
198    }        return state->last_context->next;
199    if(tmp_storage){      }
     tmp_storage->last_context = context;  
     return EES_SUCCESS;  
200    } else {    } else {
201      EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", storage);      EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", state->current_storage);
202    }    }
203    return EES_FAILURE;    return NULL;
204  }  }
205    
206  /**  /**
207   * returns the next aos_context_t pointer with the supplied context_class from the list of contexts in the supplied storage   * returns the next aos_context_t pointer with the supplied context_class from the list of contexts in the supplied storage
208   */   */
209  aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){  aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
210    aos_context_t* current_context = NULL;    aos_state_t*    state           = aos_get_state();
211    aos_storage_t* current_storage = NULL;    aos_context_t*  current_context = aos_get_iterator();
   pid_t             tid;  
   tid = syscall(SYS_gettid);  
   
   /*printf("Looking for context type %i\n", context_class);*/  
   
   current_storage = localize_storage(storage);  
   
   current_context = aos_get_iterator(current_storage);  
212    
213    /* find context type */    /* find context type */
214    if(current_context != NULL){    if(context_class != ANY){
215      if(context_class != ANY){      while(current_context != NULL){
216        while(current_context != NULL){        if((current_context->context_class == context_class)){
217          if((current_context->context_class == context_class)){          /* Found context */
218            /* Found context */          break;
           break;  
         }  
         current_context = current_context->next;  
219        }        }
220          current_context = current_context->next;
221      }      }
222    }    }
223    
224    if((storage != _global_storage) && (storage != NULL) ){    /* TODO -reliably- reset storage to saved pointer */
225    
226    
227      /* save state */
228      if((current_context == NULL) && (storage != _global_state->current_storage) && (state->current_storage != _global_state->current_storage)){
229      /* Retry with global storage */      /* Retry with global storage */
230      rewindContexts(_global_storage);      /*state->current_storage = _global_state->current_storage;*/
231      /*printf("Retrying with global storage %p\n", _global_storage);*/      /*rewindContexts(_global_state->current_storage);*/
232      current_context = getNextContext(context_class, _global_storage);      /*state->saved_storage = state->current_storage;*/
233        state->current_storage = _global_state->current_storage;
234        aos_set_iterator(_global_state->current_storage->list_contexts);
235        /*rewindContexts(_global_state->current_storage);*/
236        current_context = getNextContext(context_class, _global_state->current_storage);
237        /*printf("Should really return %p haha\n", current_context);*/
238    }    }
239    
240    /* save iterator */    if(current_context){
241    if(current_context != NULL){      aos_set_iterator(current_context->next);
     aos_set_iterator(current_context->next, current_storage);  
242    } else {    } else {
243      aos_set_iterator(NULL, current_storage);      aos_set_iterator(NULL);
244    }    }
245    
246      /*if(current_context == NULL){*/
247      /*if((storage == _global_state->current_storage) && (state->current_storage == _global_state->current_storage)){*/
248      /*if(state->saved_storage){*/
249      /*printf("Reset state\n");*/
250      /*state->current_storage = state->saved_storage;*/
251      /*state->last_context = NULL;*/
252      /*}*/
253    
254      /*}*/
255      /*}*/
256    
257    
258    return current_context;    return current_context;
259  }  }
260    
# Line 416  EES_RC AOS_Init (void){ Line 432  EES_RC AOS_Init (void){
432    _is_threading = 0;    _is_threading = 0;
433    pthread_once(&_aos_key_once, aos_make_key);    pthread_once(&_aos_key_once, aos_make_key);
434    
435    if(!(_global_storage = calloc(1, sizeof(aos_storage_t)))){    if((_global_state = calloc(1, sizeof(aos_state_t)))){
436      return EES_FAILURE;      _global_state->last_context = NULL;
437        if((_global_state->current_storage = calloc(1, sizeof(aos_storage_t)))){
438          _global_state->current_storage->list_contexts = NULL;
439          _is_initialized = 1;
440          _global_state->saved_storage = _global_state->current_storage;
441          return EES_SUCCESS;
442        }
443    }    }
444    
445    _is_initialized = 1;    return EES_FAILURE;
   return EES_SUCCESS;  
446  }  }
447    
448  /**  /**
# Line 434  void aos_make_key(void){ Line 455  void aos_make_key(void){
455  /**  /**
456   * Frees thread-local storage key   * Frees thread-local storage key
457   */   */
458  void aos_free_key(void* storage){  void aos_free_key(void* iterator){
459    pid_t             tid;    pid_t             tid;
460      aos_storage_t*    storage;
461      aos_state_t*   tmp_iterator = (aos_state_t*) iterator;
462    /*tid = gettid();*/    /*tid = gettid();*/
463    tid = syscall(SYS_gettid);    tid = syscall(SYS_gettid);
464    EEF_log(LOG_DEBUG, "Killing thread %i\n", tid);    EEF_log(LOG_DEBUG, "Killing thread %i\n", tid);
465    aos_free_storage(storage);    aos_free_storage(tmp_iterator->current_storage);
466  }  }
467    
468  /**  /**
# Line 462  EES_RC AOS_Clean(void){ Line 485  EES_RC AOS_Clean(void){
485   */   */
486  EES_RC AOS_Term (void){  EES_RC AOS_Term (void){
487    _is_threading = 0;    _is_threading = 0;
488    aos_free_storage(_global_storage);    /*aos_free_storage(_global_storage);*/
489    pthread_setspecific(_aos_key, NULL);    pthread_setspecific(_aos_key, NULL);
490    pthread_key_delete(_aos_key);    pthread_key_delete(_aos_key);
491    _is_initialized = 0;    _is_initialized = 0;

Legend:
Removed from v.1744  
changed lines
  Added in v.1745

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28