diff options
-rw-r--r-- | glusterfsd/src/glusterfsd.c | 8 | ||||
-rw-r--r-- | libglusterfs/src/Makefile.am | 4 | ||||
-rw-r--r-- | libglusterfs/src/globals.c | 166 | ||||
-rw-r--r-- | libglusterfs/src/globals.h | 41 |
4 files changed, 215 insertions, 4 deletions
diff --git a/glusterfsd/src/glusterfsd.c b/glusterfsd/src/glusterfsd.c index 3a0c30f98c7..720d8a775ee 100644 --- a/glusterfsd/src/glusterfsd.c +++ b/glusterfsd/src/glusterfsd.c @@ -61,6 +61,7 @@ #include "revision.h" #include "common-utils.h" #include "event.h" +#include "globals.h" #include <fnmatch.h> @@ -980,9 +981,12 @@ main (int argc, char *argv[]) int xl_count = 0; uint8_t process_mode = 0; + ret = glusterfs_globals_init (); + if (ret) + return ret; + utime = time (NULL); - ctx = CALLOC (1, sizeof (glusterfs_ctx_t)); - ERR_ABORT (ctx); + ctx = glusterfs_ctx_get (); process_mode = gf_get_process_mode (argv[0]); set_global_ctx_ptr (ctx); ctx->process_uuid = zr_build_process_uuid (); diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am index 8ff35fbb27a..7698661fa42 100644 --- a/libglusterfs/src/Makefile.am +++ b/libglusterfs/src/Makefile.am @@ -6,9 +6,9 @@ libglusterfs_la_LIBADD = @LEXLIB@ lib_LTLIBRARIES = libglusterfs.la -libglusterfs_la_SOURCES = dict.c spec.lex.c y.tab.c xlator.c logging.c hashfn.c defaults.c scheduler.c common-utils.c transport.c timer.c inode.c call-stub.c compat.c authenticate.c fd.c compat-errno.c event.c mem-pool.c gf-dirent.c syscall.c iobuf.c +libglusterfs_la_SOURCES = dict.c spec.lex.c y.tab.c xlator.c logging.c hashfn.c defaults.c scheduler.c common-utils.c transport.c timer.c inode.c call-stub.c compat.c authenticate.c fd.c compat-errno.c event.c mem-pool.c gf-dirent.c syscall.c iobuf.c globals.c -noinst_HEADERS = common-utils.h defaults.h dict.h glusterfs.h hashfn.h logging.h protocol.h scheduler.h xlator.h transport.h stack.h timer.h list.h inode.h call-stub.h compat.h authenticate.h fd.h revision.h compat-errno.h event.h mem-pool.h byte-order.h gf-dirent.h locking.h syscall.h iobuf.h +noinst_HEADERS = common-utils.h defaults.h dict.h glusterfs.h hashfn.h logging.h protocol.h scheduler.h xlator.h transport.h stack.h timer.h list.h inode.h call-stub.h compat.h authenticate.h fd.h revision.h compat-errno.h event.h mem-pool.h byte-order.h gf-dirent.h locking.h syscall.h iobuf.h globals.h EXTRA_DIST = spec.l spec.y diff --git a/libglusterfs/src/globals.c b/libglusterfs/src/globals.c new file mode 100644 index 00000000000..38727a25afe --- /dev/null +++ b/libglusterfs/src/globals.c @@ -0,0 +1,166 @@ +/* + Copyright (c) 2009 Z RESEARCH, Inc. <http://www.zresearch.com> + This file is part of GlusterFS. + + GlusterFS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + GlusterFS is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see + <http://www.gnu.org/licenses/>. +*/ + +#ifndef _CONFIG_H +#define _CONFIG_H +#include "config.h" +#endif /* !_CONFIG_H */ + +#include <pthread.h> + +#include "globals.h" +#include "glusterfs.h" +#include "xlator.h" + + +/* CTX */ +static glusterfs_ctx_t *glusterfs_ctx; + + +int +glusterfs_ctx_init () +{ + int ret = 0; + + if (glusterfs_ctx) + goto out; + + glusterfs_ctx = CALLOC (1, sizeof (*glusterfs_ctx)); + if (!glusterfs_ctx) { + ret = -1; + goto out; + } + + ret = pthread_mutex_init (&glusterfs_ctx->lock, NULL); + +out: + return ret; +} + + +glusterfs_ctx_t * +glusterfs_ctx_get () +{ + return glusterfs_ctx; + +} + + +/* THIS */ + +xlator_t global_xlator; +static pthread_key_t this_xlator_key; + +void +glusterfs_this_destroy (void *ptr) +{ + if (ptr) + FREE (ptr); +} + + +int +glusterfs_this_init () +{ + int ret = 0; + + ret = pthread_key_create (&this_xlator_key, glusterfs_this_destroy); + if (ret != 0) { + return ret; + } + + global_xlator.name = "glusterfs"; + global_xlator.type = "global"; + + return ret; +} + + +xlator_t ** +__glusterfs_this_location () +{ + xlator_t **this_location = NULL; + int ret = 0; + + this_location = pthread_getspecific (this_xlator_key); + + if (!this_location) { + this_location = CALLOC (1, sizeof (*this_location)); + if (!this_location) + goto out; + + ret = pthread_setspecific (this_xlator_key, this_location); + if (ret != 0) { + FREE (this_location); + this_location = NULL; + goto out; + } + } +out: + if (this_location) { + if (!*this_location) + *this_location = &global_xlator; + } + return this_location; +} + + +xlator_t * +glusterfs_this_get () +{ + xlator_t **this_location = NULL; + + this_location = __glusterfs_this_location (); + if (!this_location) + return &global_xlator; + + return *this_location; +} + + +int +glusterfs_this_set (xlator_t *this) +{ + xlator_t **this_location = NULL; + + this_location = __glusterfs_this_location (); + if (!this_location) + return -ENOMEM; + + *this_location = this; + + return 0; +} + + +int +glusterfs_globals_init () +{ + int ret = 0; + + ret = glusterfs_ctx_init (); + if (ret) + goto out; + + ret = glusterfs_this_init (); + if (ret) + goto out; +out: + return ret; +} diff --git a/libglusterfs/src/globals.h b/libglusterfs/src/globals.h new file mode 100644 index 00000000000..f4d252dd9b5 --- /dev/null +++ b/libglusterfs/src/globals.h @@ -0,0 +1,41 @@ +/* + Copyright (c) 2009 Z RESEARCH, Inc. <http://www.zresearch.com> + This file is part of GlusterFS. + + GlusterFS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + GlusterFS is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see + <http://www.gnu.org/licenses/>. +*/ + +#ifndef _GLOBALS_H +#define _GLOBALS_H + +#include "glusterfs.h" +#include "xlator.h" + +/* CTX */ +#define CTX (glusterfs_ctx_get()) + +glusterfs_ctx_t *glusterfs_ctx_get (); + +/* THIS */ +#define THIS (*__glusterfs_this_location()) + +xlator_t **__glusterfs_this_location (); +xlator_t *glusterfs_this_get (); +int glusterfs_this_set (xlator_t *); + +/* init */ +int glusterfs_globals_init (void); + +#endif /* !_GLOBALS_H */ |