blob: 2580e91353d2e90b273df8cf24760211009bc075 (
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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* Delayed deallocation of port_info objects.
Copyright (C) 2015 Free Software Foundation, Inc.
Written by Justus Winter <4winter@informatik.uni-hamburg.de>
This file is part of the GNU Hurd.
The GNU Hurd is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
The GNU Hurd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#include <assert.h>
#include <pthread.h>
#include "ports.h"
/*
* A threadpool has a color indicating which threads belong to the old
* generation.
*/
#define COLOR_BLACK 0
#define COLOR_WHITE 1
#define COLOR_INVALID ~0U
static inline int
valid_color (unsigned int c)
{
return c == COLOR_BLACK || c == COLOR_WHITE;
}
static inline unsigned int
flip_color (unsigned int c)
{
assert (valid_color (c));
return ! c;
}
/* Initialize the thread pool. */
void
_ports_threadpool_init (struct ports_threadpool *pool)
{
pthread_spin_init (&pool->lock, PTHREAD_PROCESS_PRIVATE);
pool->color = COLOR_BLACK;
pool->old_threads = 0;
pool->old_objects = NULL;
pool->young_threads = 0;
pool->young_objects = NULL;
}
/* Turn all young objects and threads into old ones. */
static inline void
flip_generations (struct ports_threadpool *pool)
{
assert (pool->old_threads == 0);
pool->old_threads = pool->young_threads;
pool->old_objects = pool->young_objects;
pool->young_threads = 0;
pool->young_objects = NULL;
pool->color = flip_color (pool->color);
}
/* Called by a thread to join a thread pool. */
void
_ports_thread_online (struct ports_threadpool *pool,
struct ports_thread *thread)
{
pthread_spin_lock (&pool->lock);
thread->color = flip_color (pool->color);
pool->young_threads += 1;
pthread_spin_unlock (&pool->lock);
}
struct pi_list
{
struct pi_list *next;
struct port_info *pi;
};
/* Called by a thread that enters its quiescent period. */
void
_ports_thread_quiescent (struct ports_threadpool *pool,
struct ports_thread *thread)
{
struct pi_list *free_list = NULL, *p;
assert (valid_color (thread->color));
pthread_spin_lock (&pool->lock);
if (thread->color == pool->color)
{
pool->old_threads -= 1;
pool->young_threads += 1;
thread->color = flip_color (thread->color);
if (pool->old_threads == 0)
{
free_list = pool->old_objects;
flip_generations (pool);
}
}
pthread_spin_unlock (&pool->lock);
for (p = free_list; p;)
{
struct pi_list *old = p;
p = p->next;
ports_port_deref (old->pi);
free (old);
}
}
/* Called by a thread to leave a thread pool. */
void
_ports_thread_offline (struct ports_threadpool *pool,
struct ports_thread *thread)
{
assert (valid_color (thread->color));
retry:
pthread_spin_lock (&pool->lock);
if (thread->color == pool->color)
{
pthread_spin_unlock (&pool->lock);
_ports_thread_quiescent (pool, thread);
goto retry;
}
thread->color = COLOR_INVALID;
pool->young_threads -= 1;
pthread_spin_unlock (&pool->lock);
}
/* Schedule an object for deallocation. */
void
_ports_port_deref_deferred (struct port_info *pi)
{
struct ports_threadpool *pool = &pi->bucket->threadpool;
struct pi_list *pl = malloc (sizeof *pl);
if (pl == NULL)
return;
pl->pi = pi;
pthread_spin_lock (&pool->lock);
pl->next = pool->young_objects;
pool->young_objects = pl;
if (pool->old_threads == 0)
{
assert (pool->old_objects == NULL);
flip_generations (pool);
}
pthread_spin_unlock (&pool->lock);
}
|