~ubuntu-branches/ubuntu/saucy/mutextrace/saucy-proposed

« back to all changes in this revision

Viewing changes to store.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Richter
  • Date: 2009-11-30 15:26:21 UTC
  • Revision ID: james.westby@ubuntu.com-20091130152621-kxucetq7b43lx9fu
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef HAVE_CONFIG_H
 
2
#include <config.h>
 
3
#endif
 
4
 
 
5
#include "store.h"
 
6
#include "real.h"
 
7
 
 
8
#include <stdlib.h>
 
9
 
 
10
struct thread *create_thread(void)
 
11
{
 
12
    static unsigned int num;
 
13
 
 
14
    struct thread *new_thread = (struct thread *)malloc(sizeof(struct thread));
 
15
    new_thread->left = 0;
 
16
    new_thread->right = 0;
 
17
    new_thread->num = ++num;
 
18
    new_thread->owns = 0;
 
19
 
 
20
    return new_thread;
 
21
}
 
22
 
 
23
struct thread *find_thread(pthread_t thread)
 
24
{
 
25
    static struct thread *root;
 
26
 
 
27
    if(!root)
 
28
    {
 
29
        root = create_thread();
 
30
        root->thread = thread;
 
31
        return root;
 
32
    }
 
33
 
 
34
    struct thread *current = root;
 
35
 
 
36
    for(;;)
 
37
    {
 
38
        if(current->thread == thread)
 
39
            return current;
 
40
 
 
41
        if(current->thread > thread)
 
42
        {
 
43
            if(!current->left)
 
44
            {
 
45
                current->left = create_thread();
 
46
                current->left->thread = thread;
 
47
            }
 
48
            current = current->left;
 
49
        }
 
50
        else
 
51
        {
 
52
            if(!current->right)
 
53
            {
 
54
                current->right = create_thread();
 
55
                current->right->thread = thread;
 
56
            }
 
57
            current = current->right;
 
58
        }
 
59
    }
 
60
}
 
61
 
 
62
struct mutex *create_mutex(void)
 
63
{
 
64
    static unsigned int num;
 
65
 
 
66
    struct mutex *new_mutex = (struct mutex *)malloc(sizeof(struct mutex));
 
67
    new_mutex->left = 0;
 
68
    new_mutex->right = 0;
 
69
    real_mutex_init(&new_mutex->lock, 0);
 
70
    real_cond_init(&new_mutex->cond, 0);
 
71
    new_mutex->num = ++num;
 
72
    new_mutex->state = uninitialized;
 
73
    new_mutex->owner = 0;
 
74
    new_mutex->owns_next = 0;
 
75
 
 
76
    return new_mutex;
 
77
}
 
78
 
 
79
struct mutex *find_mutex(pthread_mutex_t *mutex)
 
80
{
 
81
    static struct mutex *root;
 
82
 
 
83
    if(!root)
 
84
    {
 
85
        root = create_mutex();
 
86
        root->mutex = mutex;
 
87
        return root;
 
88
    }
 
89
 
 
90
    struct mutex *current = root;
 
91
 
 
92
    for(;;)
 
93
    {
 
94
        if(current->mutex == mutex)
 
95
            return current;
 
96
 
 
97
        if(current->mutex > mutex)
 
98
        {
 
99
            if(!current->left)
 
100
            {
 
101
                current->left = create_mutex();
 
102
                current->left->mutex = mutex;
 
103
            }
 
104
            current = current->left;
 
105
        }
 
106
        else
 
107
        {
 
108
            if(!current->right)
 
109
            {
 
110
                current->right = create_mutex();
 
111
                current->right->mutex = mutex;
 
112
            }
 
113
            current = current->right;
 
114
        }
 
115
    }
 
116
}