summaryrefslogtreecommitdiff
path: root/ot_mutex.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_mutex.c')
-rw-r--r--ot_mutex.c372
1 files changed, 154 insertions, 218 deletions
diff --git a/ot_mutex.c b/ot_mutex.c
index 772d936..3011987 100644
--- a/ot_mutex.c
+++ b/ot_mutex.c
@@ -16,100 +16,39 @@
16#include "uint32.h" 16#include "uint32.h"
17 17
18/* Opentracker */ 18/* Opentracker */
19#include "trackerlogic.h" 19#include "ot_iovec.h"
20#include "ot_mutex.h" 20#include "ot_mutex.h"
21#include "ot_stats.h" 21#include "ot_stats.h"
22#include "trackerlogic.h"
22 23
23/* #define MTX_DBG( STRING ) fprintf( stderr, STRING ) */ 24/* #define MTX_DBG( STRING ) fprintf( stderr, STRING ) */
24#define MTX_DBG( STRING ) 25#define MTX_DBG(STRING)
25 26
26/* Our global all torrents list */ 27/* Our global all torrents list */
27static ot_vector all_torrents[OT_BUCKET_COUNT]; 28static ot_vector all_torrents[OT_BUCKET_COUNT];
28static size_t g_torrent_count; 29static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT];
29 30static size_t g_torrent_count;
30/* Bucket Magic */
31static int bucket_locklist[ OT_MAX_THREADS ];
32static int bucket_locklist_count = 0;
33static pthread_mutex_t bucket_mutex;
34static pthread_cond_t bucket_being_unlocked;
35 31
36/* Self pipe from opentracker.c */ 32/* Self pipe from opentracker.c */
37extern int g_self_pipe[2]; 33extern int g_self_pipe[2];
38
39static int bucket_check( int bucket ) {
40 /* C should come with auto-i ;) */
41 int i;
42
43 /* No more space to acquire lock to bucket -- should not happen */
44 if( bucket_locklist_count == OT_MAX_THREADS ) {
45 fprintf( stderr, "More lock requests than mutexes. Consult source code.\n" );
46 return -1;
47 }
48
49 /* See, if bucket is already locked */
50 for( i=0; i<bucket_locklist_count; ++i )
51 if( bucket_locklist[ i ] == bucket ) {
52 stats_issue_event( EVENT_BUCKET_LOCKED, 0, 0 );
53 return -1;
54 }
55
56 return 0;
57}
58
59static void bucket_push( int bucket ) {
60 bucket_locklist[ bucket_locklist_count++ ] = bucket;
61}
62 34
63static void bucket_remove( int bucket ) { 35ot_vector *mutex_bucket_lock(int bucket) {
64 int i = 0; 36 pthread_mutex_lock(bucket_mutex + bucket);
65
66 while( ( i < bucket_locklist_count ) && ( bucket_locklist[ i ] != bucket ) )
67 ++i;
68
69 if( i == bucket_locklist_count ) {
70 fprintf( stderr, "Request to unlock bucket that was never locked. Consult source code.\n" );
71 return;
72 }
73
74 for( ; i < bucket_locklist_count - 1; ++i )
75 bucket_locklist[ i ] = bucket_locklist[ i + 1 ];
76
77 --bucket_locklist_count;
78}
79
80/* Can block */
81ot_vector *mutex_bucket_lock( int bucket ) {
82 pthread_mutex_lock( &bucket_mutex );
83 while( bucket_check( bucket ) )
84 pthread_cond_wait( &bucket_being_unlocked, &bucket_mutex );
85 bucket_push( bucket );
86 pthread_mutex_unlock( &bucket_mutex );
87 return all_torrents + bucket; 37 return all_torrents + bucket;
88} 38}
89 39
90ot_vector *mutex_bucket_lock_by_hash( ot_hash hash ) { 40ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(uint32_read_big((const char *)hash) >> OT_BUCKET_COUNT_SHIFT); }
91 return mutex_bucket_lock( uint32_read_big( (char*)hash ) >> OT_BUCKET_COUNT_SHIFT );
92}
93 41
94void mutex_bucket_unlock( int bucket, int delta_torrentcount ) { 42void mutex_bucket_unlock(int bucket, int delta_torrentcount) {
95 pthread_mutex_lock( &bucket_mutex ); 43 pthread_mutex_unlock(bucket_mutex + bucket);
96 bucket_remove( bucket );
97 g_torrent_count += delta_torrentcount; 44 g_torrent_count += delta_torrentcount;
98 pthread_cond_broadcast( &bucket_being_unlocked );
99 pthread_mutex_unlock( &bucket_mutex );
100} 45}
101 46
102void mutex_bucket_unlock_by_hash( ot_hash hash, int delta_torrentcount ) { 47void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) {
103 mutex_bucket_unlock( uint32_read_big( (char*)hash ) >> OT_BUCKET_COUNT_SHIFT, delta_torrentcount ); 48 mutex_bucket_unlock(uint32_read_big((char *)hash) >> OT_BUCKET_COUNT_SHIFT, delta_torrentcount);
104} 49}
105 50
106size_t mutex_get_torrent_count( ) { 51size_t mutex_get_torrent_count() { return g_torrent_count; }
107 size_t torrent_count;
108 pthread_mutex_lock( &bucket_mutex );
109 torrent_count = g_torrent_count;
110 pthread_mutex_unlock( &bucket_mutex );
111 return torrent_count;
112}
113 52
114/* TaskQueue Magic */ 53/* TaskQueue Magic */
115 54
@@ -122,32 +61,17 @@ struct ot_task {
122 struct ot_task *next; 61 struct ot_task *next;
123}; 62};
124 63
125static ot_taskid next_free_taskid = 1; 64static ot_taskid next_free_taskid = 1;
126static struct ot_task *tasklist; 65static struct ot_task *tasklist;
127static pthread_mutex_t tasklist_mutex; 66static pthread_mutex_t tasklist_mutex;
128static pthread_cond_t tasklist_being_filled; 67static pthread_cond_t tasklist_being_filled;
129 68
130int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) { 69int mutex_workqueue_pushtask(int64 sock, ot_tasktype tasktype) {
131 struct ot_task ** tmptask, * task; 70 struct ot_task **tmptask, *task;
132 71
133 /* Want exclusive access to tasklist */ 72 task = malloc(sizeof(struct ot_task));
134 MTX_DBG( "pushtask locks.\n" ); 73 if (!task)
135 pthread_mutex_lock( &tasklist_mutex );
136 MTX_DBG( "pushtask locked.\n" );
137
138 task = malloc(sizeof( struct ot_task));
139 if( !task ) {
140 MTX_DBG( "pushtask fail unlocks.\n" );
141 pthread_mutex_unlock( &tasklist_mutex );
142 MTX_DBG( "pushtask fail unlocked.\n" );
143 return -1; 74 return -1;
144 }
145
146 /* Skip to end of list */
147 tmptask = &tasklist;
148 while( *tmptask )
149 tmptask = &(*tmptask)->next;
150 *tmptask = task;
151 75
152 task->taskid = 0; 76 task->taskid = 0;
153 task->tasktype = tasktype; 77 task->tasktype = tasktype;
@@ -156,181 +80,193 @@ int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) {
156 task->iovec = NULL; 80 task->iovec = NULL;
157 task->next = 0; 81 task->next = 0;
158 82
83 /* Want exclusive access to tasklist */
84 pthread_mutex_lock(&tasklist_mutex);
85
86 /* Skip to end of list */
87 tmptask = &tasklist;
88 while (*tmptask)
89 tmptask = &(*tmptask)->next;
90 *tmptask = task;
91
159 /* Inform waiting workers and release lock */ 92 /* Inform waiting workers and release lock */
160 MTX_DBG( "pushtask broadcasts.\n" ); 93 pthread_cond_broadcast(&tasklist_being_filled);
161 pthread_cond_broadcast( &tasklist_being_filled ); 94 pthread_mutex_unlock(&tasklist_mutex);
162 MTX_DBG( "pushtask broadcasted, mutex unlocks.\n" );
163 pthread_mutex_unlock( &tasklist_mutex );
164 MTX_DBG( "pushtask end mutex unlocked.\n" );
165 return 0; 95 return 0;
166} 96}
167 97
168void mutex_workqueue_canceltask( int64 sock ) { 98void mutex_workqueue_canceltask(int64 sock) {
169 struct ot_task ** task; 99 struct ot_task **task;
170 100
171 /* Want exclusive access to tasklist */ 101 /* Want exclusive access to tasklist */
172 MTX_DBG( "canceltask locks.\n" ); 102 pthread_mutex_lock(&tasklist_mutex);
173 pthread_mutex_lock( &tasklist_mutex );
174 MTX_DBG( "canceltask locked.\n" );
175 103
176 task = &tasklist; 104 for (task = &tasklist; *task; task = &((*task)->next))
177 while( *task && ( (*task)->sock != sock ) ) 105 if ((*task)->sock == sock) {
178 *task = (*task)->next; 106 struct iovec *iovec = (*task)->iovec;
107 struct ot_task *ptask = *task;
108 int i;
179 109
180 if( *task && ( (*task)->sock == sock ) ) { 110 /* Free task's iovec */
181 struct iovec *iovec = (*task)->iovec; 111 for (i = 0; i < (*task)->iovec_entries; ++i)
182 struct ot_task *ptask = *task; 112 free(iovec[i].iov_base);
183 int i;
184 113
185 /* Free task's iovec */ 114 *task = (*task)->next;
186 for( i=0; i<(*task)->iovec_entries; ++i ) 115 free(ptask);
187 munmap( iovec[i].iov_base, iovec[i].iov_len ); 116 break;
188 117 }
189 *task = (*task)->next;
190 free( ptask );
191 }
192 118
193 /* Release lock */ 119 /* Release lock */
194 MTX_DBG( "canceltask unlocks.\n" ); 120 pthread_mutex_unlock(&tasklist_mutex);
195 pthread_mutex_unlock( &tasklist_mutex );
196 MTX_DBG( "canceltask unlocked.\n" );
197} 121}
198 122
199ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) { 123ot_taskid mutex_workqueue_poptask(ot_tasktype *tasktype) {
200 struct ot_task * task; 124 struct ot_task *task;
201 ot_taskid taskid = 0; 125 ot_taskid taskid = 0;
202 126
203 /* Want exclusive access to tasklist */ 127 /* Want exclusive access to tasklist */
204 MTX_DBG( "poptask mutex locks.\n" ); 128 pthread_mutex_lock(&tasklist_mutex);
205 pthread_mutex_lock( &tasklist_mutex );
206 MTX_DBG( "poptask mutex locked.\n" );
207 129
208 while( !taskid ) { 130 while (!taskid) {
209 /* Skip to the first unassigned task this worker wants to do */ 131 /* Skip to the first unassigned task this worker wants to do */
210 task = tasklist; 132 for (task = tasklist; task; task = task->next)
211 while( task && ( ( ( TASK_CLASS_MASK & task->tasktype ) != *tasktype ) || task->taskid ) ) 133 if (!task->taskid && (TASK_CLASS_MASK & task->tasktype) == *tasktype) {
212 task = task->next; 134 /* If we found an outstanding task, assign a taskid to it
213 135 and leave the loop */
214 /* If we found an outstanding task, assign a taskid to it 136 task->taskid = taskid = ++next_free_taskid;
215 and leave the loop */ 137 *tasktype = task->tasktype;
216 if( task ) { 138 break;
217 task->taskid = taskid = ++next_free_taskid; 139 }
218 *tasktype = task->tasktype; 140
219 } else { 141 /* Wait until the next task is being fed */
220 /* Wait until the next task is being fed */ 142 if (!taskid)
221 MTX_DBG( "poptask cond waits.\n" ); 143 pthread_cond_wait(&tasklist_being_filled, &tasklist_mutex);
222 pthread_cond_wait( &tasklist_being_filled, &tasklist_mutex );
223 MTX_DBG( "poptask cond waited.\n" );
224 }
225 } 144 }
226 145
227 /* Release lock */ 146 /* Release lock */
228 MTX_DBG( "poptask end mutex unlocks.\n" ); 147 pthread_mutex_unlock(&tasklist_mutex);
229 pthread_mutex_unlock( &tasklist_mutex );
230 MTX_DBG( "poptask end mutex unlocked.\n" );
231 148
232 return taskid; 149 return taskid;
233} 150}
234 151
235void mutex_workqueue_pushsuccess( ot_taskid taskid ) { 152void mutex_workqueue_pushsuccess(ot_taskid taskid) {
236 struct ot_task ** task; 153 struct ot_task **task;
237 154
238 /* Want exclusive access to tasklist */ 155 /* Want exclusive access to tasklist */
239 MTX_DBG( "pushsuccess locks.\n" ); 156 pthread_mutex_lock(&tasklist_mutex);
240 pthread_mutex_lock( &tasklist_mutex ); 157
241 MTX_DBG( "pushsuccess locked.\n" ); 158 for (task = &tasklist; *task; task = &((*task)->next))
242 159 if ((*task)->taskid == taskid) {
243 task = &tasklist; 160 struct ot_task *ptask = *task;
244 while( *task && ( (*task)->taskid != taskid ) ) 161 *task = (*task)->next;
245 *task = (*task)->next; 162 free(ptask);
246 163 break;
247 if( *task && ( (*task)->taskid == taskid ) ) { 164 }
248 struct ot_task *ptask = *task;
249 *task = (*task)->next;
250 free( ptask );
251 }
252 165
253 /* Release lock */ 166 /* Release lock */
254 MTX_DBG( "pushsuccess unlocks.\n" ); 167 pthread_mutex_unlock(&tasklist_mutex);
255 pthread_mutex_unlock( &tasklist_mutex );
256 MTX_DBG( "pushsuccess unlocked.\n" );
257} 168}
258 169
259int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) { 170int mutex_workqueue_pushresult(ot_taskid taskid, int iovec_entries, struct iovec *iovec) {
260 struct ot_task * task; 171 struct ot_task *task;
261 const char byte = 'o'; 172 const char byte = 'o';
262 173
263 /* Want exclusive access to tasklist */ 174 /* Want exclusive access to tasklist */
264 MTX_DBG( "pushresult locks.\n" ); 175 pthread_mutex_lock(&tasklist_mutex);
265 pthread_mutex_lock( &tasklist_mutex ); 176
266 MTX_DBG( "pushresult locked.\n" ); 177 for (task = tasklist; task; task = task->next)
267 178 if (task->taskid == taskid) {
268 task = tasklist; 179 task->iovec_entries = iovec_entries;
269 while( task && ( task->taskid != taskid ) ) 180 task->iovec = iovec;
270 task = task->next; 181 task->tasktype = TASK_DONE;
271 182 break;
272 if( task ) { 183 }
273 task->iovec_entries = iovec_entries;
274 task->iovec = iovec;
275 task->tasktype = TASK_DONE;
276 }
277 184
278 /* Release lock */ 185 /* Release lock */
279 MTX_DBG( "pushresult unlocks.\n" ); 186 pthread_mutex_unlock(&tasklist_mutex);
280 pthread_mutex_unlock( &tasklist_mutex );
281 MTX_DBG( "pushresult unlocked.\n" );
282 187
283 io_trywrite( g_self_pipe[1], &byte, 1 ); 188 io_trywrite(g_self_pipe[1], &byte, 1);
284 189
285 /* Indicate whether the worker has to throw away results */ 190 /* Indicate whether the worker has to throw away results */
286 return task ? 0 : -1; 191 return task ? 0 : -1;
287} 192}
288 193
289int64 mutex_workqueue_popresult( int *iovec_entries, struct iovec ** iovec ) { 194int mutex_workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec) {
290 struct ot_task ** task; 195 struct ot_task *task;
291 int64 sock = -1; 196 const char byte = 'o';
292 197
293 /* Want exclusive access to tasklist */ 198 /* Want exclusive access to tasklist */
294 MTX_DBG( "popresult locks.\n" ); 199 pthread_mutex_lock(&tasklist_mutex);
295 pthread_mutex_lock( &tasklist_mutex ); 200
296 MTX_DBG( "popresult locked.\n" ); 201 for (task = tasklist; task; task = task->next)
202 if (task->taskid == taskid) {
203 if (iovec) {
204 if (iovec_append(&task->iovec_entries, &task->iovec, iovec))
205 task->tasktype = TASK_DONE_PARTIAL;
206 else
207 task = NULL;
208 } else
209 task->tasktype = TASK_DONE;
210 break;
211 }
297 212
298 task = &tasklist; 213 /* Release lock */
299 while( *task && ( (*task)->tasktype != TASK_DONE ) ) 214 pthread_mutex_unlock(&tasklist_mutex);
300 task = &(*task)->next;
301 215
302 if( *task && ( (*task)->tasktype == TASK_DONE ) ) { 216 io_trywrite(g_self_pipe[1], &byte, 1);
303 struct ot_task *ptask = *task;
304 217
305 *iovec_entries = (*task)->iovec_entries; 218 /* Indicate whether the worker has to throw away results */
306 *iovec = (*task)->iovec; 219 return task ? 0 : -1;
307 sock = (*task)->sock; 220}
308 221
309 *task = (*task)->next; 222int64 mutex_workqueue_popresult(int *iovec_entries, struct iovec **iovec, int *is_partial) {
310 free( ptask ); 223 struct ot_task **task;
311 } 224 int64 sock = -1;
225
226 *is_partial = 0;
227
228 /* Want exclusive access to tasklist */
229 pthread_mutex_lock(&tasklist_mutex);
230
231 for (task = &tasklist; *task; task = &((*task)->next))
232 if (((*task)->tasktype & TASK_CLASS_MASK) == TASK_DONE) {
233 struct ot_task *ptask = *task;
234 *iovec_entries = ptask->iovec_entries;
235 *iovec = ptask->iovec;
236 sock = ptask->sock;
237
238 if ((*task)->tasktype == TASK_DONE) {
239 *task = ptask->next;
240 free(ptask);
241 } else {
242 ptask->iovec_entries = 0;
243 ptask->iovec = NULL;
244 *is_partial = 1;
245 /* Prevent task from showing up immediately again unless new data was added */
246 (*task)->tasktype = TASK_FULLSCRAPE;
247 }
248 break;
249 }
312 250
313 /* Release lock */ 251 /* Release lock */
314 MTX_DBG( "popresult unlocks.\n" ); 252 pthread_mutex_unlock(&tasklist_mutex);
315 pthread_mutex_unlock( &tasklist_mutex );
316 MTX_DBG( "popresult unlocked.\n" );
317 return sock; 253 return sock;
318} 254}
319 255
320void mutex_init( ) { 256void mutex_init() {
257 int i;
321 pthread_mutex_init(&tasklist_mutex, NULL); 258 pthread_mutex_init(&tasklist_mutex, NULL);
322 pthread_cond_init (&tasklist_being_filled, NULL); 259 pthread_cond_init(&tasklist_being_filled, NULL);
323 pthread_mutex_init(&bucket_mutex, NULL); 260 for (i = 0; i < OT_BUCKET_COUNT; ++i)
324 pthread_cond_init (&bucket_being_unlocked, NULL); 261 pthread_mutex_init(bucket_mutex + i, NULL);
325 byte_zero( all_torrents, sizeof( all_torrents ) ); 262 byte_zero(all_torrents, sizeof(all_torrents));
326} 263}
327 264
328void mutex_deinit( ) { 265void mutex_deinit() {
329 pthread_mutex_destroy(&bucket_mutex); 266 int i;
330 pthread_cond_destroy(&bucket_being_unlocked); 267 for (i = 0; i < OT_BUCKET_COUNT; ++i)
268 pthread_mutex_destroy(bucket_mutex + i);
331 pthread_mutex_destroy(&tasklist_mutex); 269 pthread_mutex_destroy(&tasklist_mutex);
332 pthread_cond_destroy(&tasklist_being_filled); 270 pthread_cond_destroy(&tasklist_being_filled);
333 byte_zero( all_torrents, sizeof( all_torrents ) ); 271 byte_zero(all_torrents, sizeof(all_torrents));
334} 272}
335
336const char *g_version_mutex_c = "$Source$: $Revision$\n";