aboutsummaryrefslogtreecommitdiffstats
path: root/plpa-1.3.2/src/libplpa/plpa_runtime.c
blob: 97f1549cc930c5fcbdc1b4560b2d0d3366d0ccf6 (plain) (blame)
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
/*
 * Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.
 */

#include "plpa_config.h"
#include "plpa.h"
#include "plpa_internal.h"

#include <errno.h>
#include <pthread.h>

/* Global variables */
int PLPA_NAME(initialized) = 0;

/* Local variables */
static int refcount = 0;
static pthread_mutex_t mutex;


/* Central clearing point for all parts of PLPA that need to be
   initialized.  It is erroneous to call this function by more than
   one thread simultaneously. */
int PLPA_NAME(init)(void)
{
    int ret;

    /* If we're already initialized, simply increase the refcount */
    if (PLPA_NAME(initialized)) {
        pthread_mutex_lock(&mutex);
        ++refcount;
        pthread_mutex_unlock(&mutex);
        return 0;
    }

    /* Otherwise, initialize all the sybsystems */
    if (0 != (ret = pthread_mutex_init(&mutex, NULL)) ||
        0 != (ret = PLPA_NAME(api_probe_init)()) ||
        0 != (ret = PLPA_NAME(set_cache_behavior)(PLPA_NAME_CAPS(CACHE_USE)))) {
        return ret;
    }

    PLPA_NAME(initialized) = 1;
    refcount = 1;
    return 0;
}


/* Central clearing point for all parts of PLPA that need to be
   shutdown. */
int PLPA_NAME(finalize)(void)
{
    int val;

    /* If we're not initialized, return an error */
    if (!PLPA_NAME(initialized)) {
        return ENOENT;
    }

    /* Decrement and check the refcount.  If it's nonzero, then simply
       return success. */
    pthread_mutex_lock(&mutex);
    val = --refcount;
    pthread_mutex_unlock(&mutex);
    if (0 != val) {
        return 0;
    }

    /* Ok, we're the last one.  Cleanup. */
    PLPA_NAME(set_cache_behavior)(PLPA_NAME_CAPS(CACHE_IGNORE));
    pthread_mutex_destroy(&mutex);
    PLPA_NAME(initialized) = 0;
    return 0;
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy