File: store.c

package info (click to toggle)
mutextrace 0.1-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 1,252 kB
  • ctags: 149
  • sloc: sh: 10,127; ansic: 432; makefile: 26
file content (116 lines) | stat: -rw-r--r-- 2,403 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "store.h"
#include "real.h"

#include <stdlib.h>

struct thread *create_thread(void)
{
    static unsigned int num;

    struct thread *new_thread = (struct thread *)malloc(sizeof(struct thread));
    new_thread->left = 0;
    new_thread->right = 0;
    new_thread->num = ++num;
    new_thread->owns = 0;

    return new_thread;
}

struct thread *find_thread(pthread_t thread)
{
    static struct thread *root;

    if(!root)
    {
        root = create_thread();
        root->thread = thread;
        return root;
    }

    struct thread *current = root;

    for(;;)
    {
        if(current->thread == thread)
            return current;

        if(current->thread > thread)
        {
            if(!current->left)
            {
                current->left = create_thread();
                current->left->thread = thread;
            }
            current = current->left;
        }
        else
        {
            if(!current->right)
            {
                current->right = create_thread();
                current->right->thread = thread;
            }
            current = current->right;
        }
    }
}

struct mutex *create_mutex(void)
{
    static unsigned int num;

    struct mutex *new_mutex = (struct mutex *)malloc(sizeof(struct mutex));
    new_mutex->left = 0;
    new_mutex->right = 0;
    real_mutex_init(&new_mutex->lock, 0);
    real_cond_init(&new_mutex->cond, 0);
    new_mutex->num = ++num;
    new_mutex->state = uninitialized;
    new_mutex->owner = 0;
    new_mutex->owns_next = 0;

    return new_mutex;
}

struct mutex *find_mutex(pthread_mutex_t *mutex)
{
    static struct mutex *root;

    if(!root)
    {
        root = create_mutex();
        root->mutex = mutex;
        return root;
    }

    struct mutex *current = root;

    for(;;)
    {
        if(current->mutex == mutex)
            return current;

        if(current->mutex > mutex)
        {
            if(!current->left)
            {
                current->left = create_mutex();
                current->left->mutex = mutex;
            }
            current = current->left;
        }
        else
        {
            if(!current->right)
            {
                current->right = create_mutex();
                current->right->mutex = mutex;
            }
            current = current->right;
        }
    }
}