Computer Laboratory

tesla_store.c File Reference

Detailed Description

Implementation of tesla_store.

Definition in file tesla_store.c.

#include "tesla_internal.h"
#include <errno.h>
+ Include dependency graph for tesla_store.c:

Go to the source code of this file.

Functions

pthread_key_t pthread_key (void)
 The pthreads key used to identify TESLA data. More...
 
void tesla_pthread_destructor (void *)
 
int32_t tesla_store_get (enum tesla_context context, uint32_t classes, uint32_t instances, tesla_store **storep)
 Retrieve the tesla_store for a context (e.g., a thread). More...
 
int32_t tesla_store_init (tesla_store *store, enum tesla_context context, uint32_t classes, uint32_t instances)
 Initialise tesla_store internals. More...
 
void tesla_store_free (tesla_store *store)
 Clean up a tesla_store. More...
 
int32_t tesla_class_get (tesla_store *store, uint32_t id, tesla_class **tclassp, const char *name, const char *description)
 Retrieve (or create) a tesla_class from a tesla_store. More...
 

Function Documentation

pthread_key_t pthread_key ( void  )

The pthreads key used to identify TESLA data.

Definition at line 195 of file tesla_store.c.

References __debug, and tesla_pthread_destructor().

Referenced by tesla_store_get().

196 {
197  // This function is just a singleton accessor.
198  static pthread_key_t key;
199  static int key_initialised = 0;
200  static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
201 
202  // The key, once initialised, is immutable, so it is safe to check and
203  // return it without locking. Multiple initialisations are prevented by
204  // the critical section below.
205  if (key_initialised) return key;
206 
207  int error __debug = pthread_mutex_lock(&lock);
208  assert(error == 0 && "failed to lock pthread key mutex");
209 
210  // Now that we're in the critical section, check again to make sure we
211  // initialise the key twice.
212  if (key_initialised) return key;
213 
214  error = pthread_key_create(&key, tesla_pthread_destructor);
215  assert(error == 0 && "failed to create pthread_key_t");
216 
217  key_initialised = 1;
218 
219  error = pthread_mutex_unlock(&lock);
220  assert(error == 0 && "failed to unlock pthread key mutex");
221 
222  return key;
223 }

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void tesla_pthread_destructor ( void *  x)

Definition at line 226 of file tesla_store.c.

References tesla_store_free().

Referenced by pthread_key().

227 {
228  tesla_store *store = (tesla_store*) x;
229  tesla_store_free(store);
230 }

+ Here is the call graph for this function:

+ Here is the caller graph for this function: