summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/ctx.c
blob: 1c707eb5dfdea10d36f8da780bd18424b94a0b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <pthread.h>

#include "globals.h"
#include "glusterfs.h"
#include "timer-wheel.h"

glusterfs_ctx_t *
glusterfs_ctx_new ()
{
        int               ret = 0;
	glusterfs_ctx_t  *ctx = NULL;

	/* no GF_CALLOC here, gf_acct_mem_set_enable is not
	   yet decided at this point */
        ctx = calloc (1, sizeof (*ctx));
        if (!ctx) {
                ret = -1;
                goto out;
        }

        ctx->mem_acct_enable = gf_global_mem_acct_enable_get();

        INIT_LIST_HEAD (&ctx->graphs);
	INIT_LIST_HEAD (&ctx->mempool_list);

	ctx->daemon_pipe[0] = -1;
	ctx->daemon_pipe[1] = -1;

        ctx->log.loglevel = DEFAULT_LOG_LEVEL;

#ifdef RUN_WITH_VALGRIND
        ctx->cmd_args.valgrind = _gf_true;
#endif

        /* lock is never destroyed! */
	ret = LOCK_INIT (&ctx->lock);
	if (ret) {
		free (ctx);
		ctx = NULL;
	}

out:
	return ctx;
}

static void
glusterfs_ctx_tw_destroy (struct gf_ctx_tw *ctx_tw)
{
        if (ctx_tw->timer_wheel)
                gf_tw_cleanup_timers (ctx_tw->timer_wheel);

        GF_FREE (ctx_tw);
}

struct tvec_base*
glusterfs_ctx_tw_get (glusterfs_ctx_t *ctx)
{
        struct gf_ctx_tw *ctx_tw = NULL;

        LOCK (&ctx->lock);
        {
                if (ctx->tw) {
                        ctx_tw = GF_REF_GET (ctx->tw);
                } else {
                        ctx_tw = GF_CALLOC (1, sizeof (struct gf_ctx_tw),
                                            gf_common_mt_tw_ctx);
                        ctx_tw->timer_wheel = gf_tw_init_timers();
                        GF_REF_INIT (ctx_tw, glusterfs_ctx_tw_destroy);
                        ctx->tw = ctx_tw;
                }
        }
        UNLOCK (&ctx->lock);

        return ctx_tw->timer_wheel;
}

void
glusterfs_ctx_tw_put (glusterfs_ctx_t *ctx)
{
        GF_REF_PUT (ctx->tw);
}