-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_tree.c
142 lines (112 loc) · 3.88 KB
/
thread_tree.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright 2009 Raphael Kubo da Costa <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <malloc.h>
#include <pthread.h>
#include <stdlib.h>
#include "futex.h"
#include "mem.h"
#include "thread_tree.h"
void thread_level_free(ThreadLevel *level)
{
if (level) {
free(level->interested);
free(level->turn);
free(level);
}
}
size_t thread_level_get_turn_pos(size_t thread_id)
{
return thread_id/2;
}
ThreadLevel *thread_level_new(size_t numthreads)
{
ThreadLevel *level;
level = MEM_ALLOC(ThreadLevel);
/* Always pad numthread to the next even number */
level->interested = MEM_ALLOC_N(size_t, (numthreads % 2 ? numthreads + 1 : numthreads));
level->n_elem = (numthreads % 2 ? numthreads + 1 : numthreads);
level->turn = MEM_ALLOC_N(size_t, level->n_elem / 2);
return level;
}
void thread_tree_enter_critical_region(ThreadTree *tree, size_t level, size_t thread_id)
{
size_t other, turn_pos;
assert(tree);
assert(level < thread_tree_get_height(tree));
assert(thread_id < tree->tree[level]->n_elem);
turn_pos = thread_level_get_turn_pos(thread_id);
tree->tree[level]->interested[thread_id] = 1;
tree->tree[level]->turn[turn_pos] = thread_id;
other = (thread_id % 2 ? thread_id - 1 : thread_id + 1);
futex_wake(&(tree->tree[level]->turn[turn_pos]), 1);
while ((tree->tree[level]->interested[other]) &&
(!futex_wait(&(tree->tree[level]->turn[turn_pos]), thread_id)));
}
void thread_tree_free(ThreadTree *tree)
{
size_t i;
if (tree) {
for (i = 0; i < tree->height; i++)
thread_level_free(tree->tree[i]);
free(tree->thread_list);
free(tree->tree);
free(tree);
}
}
size_t thread_tree_get_height(ThreadTree *tree)
{
assert(tree);
return tree->height;
}
void thread_tree_leave_critical_region(ThreadTree *tree, size_t level, size_t thread_id)
{
size_t turn_pos;
assert(tree);
assert(level < thread_tree_get_height(tree));
assert(thread_id < tree->tree[level]->n_elem);
turn_pos = thread_level_get_turn_pos(thread_id);
tree->tree[level]->interested[thread_id] = 0;
futex_wake(&(tree->tree[level]->turn[turn_pos]), 1);
}
ThreadTree *thread_tree_new(size_t numthreads)
{
size_t height = 0;
size_t threads_left;
ThreadLevel *level;
ThreadTree *tree;
assert(numthreads > 0);
tree = MEM_ALLOC(ThreadTree);
tree->tree = NULL;
tree->thread_list = MEM_ALLOC_N(pthread_t, numthreads);
for (threads_left = numthreads; threads_left > 0; threads_left /= 2) {
level = thread_level_new(numthreads);
tree->tree = realloc(tree->tree, (height * sizeof(tree->tree)) + sizeof(ThreadLevel*));
tree->tree[height] = level;
height++;
}
tree->height = height;
return tree;
}