Commit 91664352 by Serge Hallyn

Revert "remove static_lock()/static_unlock() and start to use thread local storage (v2)"

This reverts commit 95b422fc. Conflicts: src/lxc/utils.c
parent 5cad2f04
...@@ -44,6 +44,7 @@ lxc_log_define(lxc_lock, lxc); ...@@ -44,6 +44,7 @@ lxc_log_define(lxc_lock, lxc);
#ifdef MUTEX_DEBUGGING #ifdef MUTEX_DEBUGGING
pthread_mutex_t thread_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; pthread_mutex_t thread_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
pthread_mutex_t static_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
inline void dump_stacktrace(void) inline void dump_stacktrace(void)
{ {
...@@ -65,6 +66,7 @@ inline void dump_stacktrace(void) ...@@ -65,6 +66,7 @@ inline void dump_stacktrace(void)
} }
#else #else
pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
inline void dump_stacktrace(void) {;} inline void dump_stacktrace(void) {;}
#endif #endif
...@@ -322,6 +324,17 @@ void process_unlock(void) ...@@ -322,6 +324,17 @@ void process_unlock(void)
unlock_mutex(&thread_mutex); unlock_mutex(&thread_mutex);
} }
/* Protects static const values inside the lxc_global_config_value funtion */
void static_lock(void)
{
lock_mutex(&static_mutex);
}
void static_unlock(void)
{
unlock_mutex(&static_mutex);
}
int container_mem_lock(struct lxc_container *c) int container_mem_lock(struct lxc_container *c)
{ {
return lxclock(c->privlock, 0); return lxclock(c->privlock, 0);
......
...@@ -123,6 +123,16 @@ extern void process_lock(void); ...@@ -123,6 +123,16 @@ extern void process_lock(void);
*/ */
extern void process_unlock(void); extern void process_unlock(void);
/*!
* \brief Lock global data.
*/
extern void static_lock(void);
/*!
* \brief Unlock global data.
*/
extern void static_unlock(void);
struct lxc_container; struct lxc_container;
/*! /*!
......
...@@ -254,6 +254,9 @@ const char *lxc_global_config_value(const char *option_name) ...@@ -254,6 +254,9 @@ const char *lxc_global_config_value(const char *option_name)
{ NULL, NULL }, { NULL, NULL },
}; };
/* Protected by a mutex to eliminate conflicting load and store operations */
static const char *values[sizeof(options) / sizeof(options[0])] = { 0 };
char *user_config_path = NULL; char *user_config_path = NULL;
char *user_lxc_path = NULL; char *user_lxc_path = NULL;
char *user_home = NULL; char *user_home = NULL;
...@@ -278,8 +281,6 @@ const char *lxc_global_config_value(const char *option_name) ...@@ -278,8 +281,6 @@ const char *lxc_global_config_value(const char *option_name)
user_lxc_path = strdup(LXCPATH); user_lxc_path = strdup(LXCPATH);
} }
/* placed in the thread local storage pool */
static __thread const char *values[sizeof(options) / sizeof(options[0])] = { 0 };
const char *(*ptr)[2]; const char *(*ptr)[2];
const char *value; const char *value;
size_t i; size_t i;
...@@ -297,13 +298,15 @@ const char *lxc_global_config_value(const char *option_name) ...@@ -297,13 +298,15 @@ const char *lxc_global_config_value(const char *option_name)
return NULL; return NULL;
} }
static_lock();
if (values[i]) { if (values[i]) {
free(user_config_path); free(user_config_path);
free(user_lxc_path); free(user_lxc_path);
value = values[i]; value = values[i];
static_unlock();
return value; return value;
} }
static_unlock();
process_lock(); process_lock();
fin = fopen_cloexec(user_config_path, "r"); fin = fopen_cloexec(user_config_path, "r");
...@@ -341,12 +344,15 @@ const char *lxc_global_config_value(const char *option_name) ...@@ -341,12 +344,15 @@ const char *lxc_global_config_value(const char *option_name)
while (*p && (*p == ' ' || *p == '\t')) p++; while (*p && (*p == ' ' || *p == '\t')) p++;
if (!*p) if (!*p)
continue; continue;
static_lock();
values[i] = copy_global_config_value(p); values[i] = copy_global_config_value(p);
static_unlock();
free(user_lxc_path); free(user_lxc_path);
goto out; goto out;
} }
} }
/* could not find value, use default */ /* could not find value, use default */
static_lock();
if (strcmp(option_name, "lxcpath") == 0) if (strcmp(option_name, "lxcpath") == 0)
values[i] = user_lxc_path; values[i] = user_lxc_path;
else { else {
...@@ -358,6 +364,7 @@ const char *lxc_global_config_value(const char *option_name) ...@@ -358,6 +364,7 @@ const char *lxc_global_config_value(const char *option_name)
* as an error... */ * as an error... */
if (!values[i]) if (!values[i])
errno = 0; errno = 0;
static_unlock();
out: out:
process_lock(); process_lock();
...@@ -365,7 +372,10 @@ out: ...@@ -365,7 +372,10 @@ out:
fclose(fin); fclose(fin);
process_unlock(); process_unlock();
return values[i]; static_lock();
value = values[i];
static_unlock();
return value;
} }
const char *default_lvm_vg(void) const char *default_lvm_vg(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment