summaryrefslogtreecommitdiff
path: root/ot_mutex.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_mutex.c')
-rw-r--r--ot_mutex.c308
1 files changed, 150 insertions, 158 deletions
diff --git a/ot_mutex.c b/ot_mutex.c
index f46e3e2..be3ddf9 100644
--- a/ot_mutex.c
+++ b/ot_mutex.c
@@ -16,42 +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 pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT]; 29static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT];
29static size_t g_torrent_count; 30static size_t g_torrent_count;
30 31
31/* Self pipe from opentracker.c */ 32/* Self pipe from opentracker.c */
32extern int g_self_pipe[2]; 33extern int g_self_pipe[2];
33 34
34ot_vector *mutex_bucket_lock( int bucket ) { 35ot_vector *mutex_bucket_lock(int bucket) {
35 pthread_mutex_lock(bucket_mutex + bucket ); 36 pthread_mutex_lock(bucket_mutex + bucket);
36 return all_torrents + bucket; 37 return all_torrents + bucket;
37} 38}
38 39
39ot_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); }
40 return mutex_bucket_lock( uint32_read_big( (char*)hash ) >> OT_BUCKET_COUNT_SHIFT );
41}
42 41
43void mutex_bucket_unlock( int bucket, int delta_torrentcount ) { 42void mutex_bucket_unlock(int bucket, int delta_torrentcount) {
44 pthread_mutex_unlock(bucket_mutex + bucket); 43 pthread_mutex_unlock(bucket_mutex + bucket);
45 g_torrent_count += delta_torrentcount; 44 g_torrent_count += delta_torrentcount;
46} 45}
47 46
48void mutex_bucket_unlock_by_hash( ot_hash hash, int delta_torrentcount ) { 47void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) {
49 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);
50} 49}
51 50
52size_t mutex_get_torrent_count( ) { 51size_t mutex_get_torrent_count(void) { return g_torrent_count; }
53 return g_torrent_count;
54}
55 52
56/* TaskQueue Magic */ 53/* TaskQueue Magic */
57 54
@@ -59,37 +56,22 @@ struct ot_task {
59 ot_taskid taskid; 56 ot_taskid taskid;
60 ot_tasktype tasktype; 57 ot_tasktype tasktype;
61 int64 sock; 58 int64 sock;
62 int iovec_entries; 59 size_t iovec_entries;
63 struct iovec *iovec; 60 struct iovec *iovec;
64 struct ot_task *next; 61 struct ot_task *next;
65}; 62};
66 63
67static ot_taskid next_free_taskid = 1; 64static ot_taskid next_free_taskid = 1;
68static struct ot_task *tasklist; 65static struct ot_task *tasklist;
69static pthread_mutex_t tasklist_mutex; 66static pthread_mutex_t tasklist_mutex;
70static pthread_cond_t tasklist_being_filled; 67static pthread_cond_t tasklist_being_filled;
71
72int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) {
73 struct ot_task ** tmptask, * task;
74 68
75 /* Want exclusive access to tasklist */ 69int mutex_workqueue_pushtask(int64 sock, ot_tasktype tasktype) {
76 MTX_DBG( "pushtask locks.\n" ); 70 struct ot_task **tmptask, *task;
77 pthread_mutex_lock( &tasklist_mutex );
78 MTX_DBG( "pushtask locked.\n" );
79 71
80 task = malloc(sizeof( struct ot_task)); 72 task = malloc(sizeof(struct ot_task));
81 if( !task ) { 73 if (!task)
82 MTX_DBG( "pushtask fail unlocks.\n" );
83 pthread_mutex_unlock( &tasklist_mutex );
84 MTX_DBG( "pushtask fail unlocked.\n" );
85 return -1; 74 return -1;
86 }
87
88 /* Skip to end of list */
89 tmptask = &tasklist;
90 while( *tmptask )
91 tmptask = &(*tmptask)->next;
92 *tmptask = task;
93 75
94 task->taskid = 0; 76 task->taskid = 0;
95 task->tasktype = tasktype; 77 task->tasktype = tasktype;
@@ -98,183 +80,193 @@ int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) {
98 task->iovec = NULL; 80 task->iovec = NULL;
99 task->next = 0; 81 task->next = 0;
100 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
101 /* Inform waiting workers and release lock */ 92 /* Inform waiting workers and release lock */
102 MTX_DBG( "pushtask broadcasts.\n" ); 93 pthread_cond_broadcast(&tasklist_being_filled);
103 pthread_cond_broadcast( &tasklist_being_filled ); 94 pthread_mutex_unlock(&tasklist_mutex);
104 MTX_DBG( "pushtask broadcasted, mutex unlocks.\n" );
105 pthread_mutex_unlock( &tasklist_mutex );
106 MTX_DBG( "pushtask end mutex unlocked.\n" );
107 return 0; 95 return 0;
108} 96}
109 97
110void mutex_workqueue_canceltask( int64 sock ) { 98void mutex_workqueue_canceltask(int64 sock) {
111 struct ot_task ** task; 99 struct ot_task **task;
112 100
113 /* Want exclusive access to tasklist */ 101 /* Want exclusive access to tasklist */
114 MTX_DBG( "canceltask locks.\n" ); 102 pthread_mutex_lock(&tasklist_mutex);
115 pthread_mutex_lock( &tasklist_mutex );
116 MTX_DBG( "canceltask locked.\n" );
117
118 task = &tasklist;
119 while( *task && ( (*task)->sock != sock ) )
120 *task = (*task)->next;
121 103
122 if( *task && ( (*task)->sock == sock ) ) { 104 for (task = &tasklist; *task; task = &((*task)->next))
123 struct iovec *iovec = (*task)->iovec; 105 if ((*task)->sock == sock) {
124 struct ot_task *ptask = *task; 106 struct iovec *iovec = (*task)->iovec;
125 int i; 107 struct ot_task *ptask = *task;
108 size_t i;
126 109
127 /* Free task's iovec */ 110 /* Free task's iovec */
128 for( i=0; i<(*task)->iovec_entries; ++i ) 111 for (i = 0; i < (*task)->iovec_entries; ++i)
129 free( iovec[i].iov_base ); 112 free(iovec[i].iov_base);
130 113
131 *task = (*task)->next; 114 *task = (*task)->next;
132 free( ptask ); 115 free(ptask);
133 } 116 break;
117 }
134 118
135 /* Release lock */ 119 /* Release lock */
136 MTX_DBG( "canceltask unlocks.\n" ); 120 pthread_mutex_unlock(&tasklist_mutex);
137 pthread_mutex_unlock( &tasklist_mutex );
138 MTX_DBG( "canceltask unlocked.\n" );
139} 121}
140 122
141ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) { 123ot_taskid mutex_workqueue_poptask(ot_tasktype *tasktype) {
142 struct ot_task * task; 124 struct ot_task *task;
143 ot_taskid taskid = 0; 125 ot_taskid taskid = 0;
144 126
145 /* Want exclusive access to tasklist */ 127 /* Want exclusive access to tasklist */
146 MTX_DBG( "poptask mutex locks.\n" ); 128 pthread_mutex_lock(&tasklist_mutex);
147 pthread_mutex_lock( &tasklist_mutex );
148 MTX_DBG( "poptask mutex locked.\n" );
149 129
150 while( !taskid ) { 130 while (!taskid) {
151 /* Skip to the first unassigned task this worker wants to do */ 131 /* Skip to the first unassigned task this worker wants to do */
152 task = tasklist; 132 for (task = tasklist; task; task = task->next)
153 while( task && ( ( ( TASK_CLASS_MASK & task->tasktype ) != *tasktype ) || task->taskid ) ) 133 if (!task->taskid && (TASK_CLASS_MASK & task->tasktype) == *tasktype) {
154 task = task->next; 134 /* If we found an outstanding task, assign a taskid to it
135 and leave the loop */
136 task->taskid = taskid = ++next_free_taskid;
137 *tasktype = task->tasktype;
138 break;
139 }
155 140
156 /* If we found an outstanding task, assign a taskid to it 141 /* Wait until the next task is being fed */
157 and leave the loop */ 142 if (!taskid)
158 if( task ) { 143 pthread_cond_wait(&tasklist_being_filled, &tasklist_mutex);
159 task->taskid = taskid = ++next_free_taskid;
160 *tasktype = task->tasktype;
161 } else {
162 /* Wait until the next task is being fed */
163 MTX_DBG( "poptask cond waits.\n" );
164 pthread_cond_wait( &tasklist_being_filled, &tasklist_mutex );
165 MTX_DBG( "poptask cond waited.\n" );
166 }
167 } 144 }
168 145
169 /* Release lock */ 146 /* Release lock */
170 MTX_DBG( "poptask end mutex unlocks.\n" ); 147 pthread_mutex_unlock(&tasklist_mutex);
171 pthread_mutex_unlock( &tasklist_mutex );
172 MTX_DBG( "poptask end mutex unlocked.\n" );
173 148
174 return taskid; 149 return taskid;
175} 150}
176 151
177void mutex_workqueue_pushsuccess( ot_taskid taskid ) { 152void mutex_workqueue_pushsuccess(ot_taskid taskid) {
178 struct ot_task ** task; 153 struct ot_task **task;
179 154
180 /* Want exclusive access to tasklist */ 155 /* Want exclusive access to tasklist */
181 MTX_DBG( "pushsuccess locks.\n" ); 156 pthread_mutex_lock(&tasklist_mutex);
182 pthread_mutex_lock( &tasklist_mutex );
183 MTX_DBG( "pushsuccess locked.\n" );
184
185 task = &tasklist;
186 while( *task && ( (*task)->taskid != taskid ) )
187 *task = (*task)->next;
188 157
189 if( *task && ( (*task)->taskid == taskid ) ) { 158 for (task = &tasklist; *task; task = &((*task)->next))
190 struct ot_task *ptask = *task; 159 if ((*task)->taskid == taskid) {
191 *task = (*task)->next; 160 struct ot_task *ptask = *task;
192 free( ptask ); 161 *task = (*task)->next;
193 } 162 free(ptask);
163 break;
164 }
194 165
195 /* Release lock */ 166 /* Release lock */
196 MTX_DBG( "pushsuccess unlocks.\n" ); 167 pthread_mutex_unlock(&tasklist_mutex);
197 pthread_mutex_unlock( &tasklist_mutex );
198 MTX_DBG( "pushsuccess unlocked.\n" );
199} 168}
200 169
201int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) { 170int mutex_workqueue_pushresult(ot_taskid taskid, size_t iovec_entries, struct iovec *iovec) {
202 struct ot_task * task; 171 struct ot_task *task;
203 const char byte = 'o'; 172 const char byte = 'o';
204 173
205 /* Want exclusive access to tasklist */ 174 /* Want exclusive access to tasklist */
206 MTX_DBG( "pushresult locks.\n" ); 175 pthread_mutex_lock(&tasklist_mutex);
207 pthread_mutex_lock( &tasklist_mutex );
208 MTX_DBG( "pushresult locked.\n" );
209
210 task = tasklist;
211 while( task && ( task->taskid != taskid ) )
212 task = task->next;
213 176
214 if( task ) { 177 for (task = tasklist; task; task = task->next)
215 task->iovec_entries = iovec_entries; 178 if (task->taskid == taskid) {
216 task->iovec = iovec; 179 task->iovec_entries = iovec_entries;
217 task->tasktype = TASK_DONE; 180 task->iovec = iovec;
218 } 181 task->tasktype = TASK_DONE;
182 break;
183 }
219 184
220 /* Release lock */ 185 /* Release lock */
221 MTX_DBG( "pushresult unlocks.\n" ); 186 pthread_mutex_unlock(&tasklist_mutex);
222 pthread_mutex_unlock( &tasklist_mutex );
223 MTX_DBG( "pushresult unlocked.\n" );
224 187
225 io_trywrite( g_self_pipe[1], &byte, 1 ); 188 io_trywrite(g_self_pipe[1], &byte, 1);
226 189
227 /* Indicate whether the worker has to throw away results */ 190 /* Indicate whether the worker has to throw away results */
228 return task ? 0 : -1; 191 return task ? 0 : -1;
229} 192}
230 193
231int64 mutex_workqueue_popresult( int *iovec_entries, struct iovec ** iovec ) { 194int mutex_workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec) {
232 struct ot_task ** task; 195 struct ot_task *task;
233 int64 sock = -1; 196 const char byte = 'o';
234 197
235 /* Want exclusive access to tasklist */ 198 /* Want exclusive access to tasklist */
236 MTX_DBG( "popresult locks.\n" ); 199 pthread_mutex_lock(&tasklist_mutex);
237 pthread_mutex_lock( &tasklist_mutex );
238 MTX_DBG( "popresult locked.\n" );
239 200
240 task = &tasklist; 201 for (task = tasklist; task; task = task->next)
241 while( *task && ( (*task)->tasktype != TASK_DONE ) ) 202 if (task->taskid == taskid) {
242 task = &(*task)->next; 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 }
243 212
244 if( *task && ( (*task)->tasktype == TASK_DONE ) ) { 213 /* Release lock */
245 struct ot_task *ptask = *task; 214 pthread_mutex_unlock(&tasklist_mutex);
246 215
247 *iovec_entries = (*task)->iovec_entries; 216 io_trywrite(g_self_pipe[1], &byte, 1);
248 *iovec = (*task)->iovec;
249 sock = (*task)->sock;
250 217
251 *task = (*task)->next; 218 /* Indicate whether the worker has to throw away results */
252 free( ptask ); 219 return task ? 0 : -1;
253 } 220}
221
222int64 mutex_workqueue_popresult(size_t *iovec_entries, struct iovec **iovec, int *is_partial) {
223 struct ot_task **task;
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 }
254 250
255 /* Release lock */ 251 /* Release lock */
256 MTX_DBG( "popresult unlocks.\n" ); 252 pthread_mutex_unlock(&tasklist_mutex);
257 pthread_mutex_unlock( &tasklist_mutex );
258 MTX_DBG( "popresult unlocked.\n" );
259 return sock; 253 return sock;
260} 254}
261 255
262void mutex_init( ) { 256void mutex_init(void) {
263 int i; 257 size_t i;
264 pthread_mutex_init(&tasklist_mutex, NULL); 258 pthread_mutex_init(&tasklist_mutex, NULL);
265 pthread_cond_init (&tasklist_being_filled, NULL); 259 pthread_cond_init(&tasklist_being_filled, NULL);
266 for (i=0; i < OT_BUCKET_COUNT; ++i) 260 for (i = 0; i < OT_BUCKET_COUNT; ++i)
267 pthread_mutex_init(bucket_mutex + i, NULL); 261 pthread_mutex_init(bucket_mutex + i, NULL);
268 byte_zero( all_torrents, sizeof( all_torrents ) ); 262 byte_zero(all_torrents, sizeof(all_torrents));
269} 263}
270 264
271void mutex_deinit( ) { 265void mutex_deinit(void) {
272 int i; 266 size_t i;
273 for (i=0; i < OT_BUCKET_COUNT; ++i) 267 for (i = 0; i < OT_BUCKET_COUNT; ++i)
274 pthread_mutex_destroy(bucket_mutex + i); 268 pthread_mutex_destroy(bucket_mutex + i);
275 pthread_mutex_destroy(&tasklist_mutex); 269 pthread_mutex_destroy(&tasklist_mutex);
276 pthread_cond_destroy(&tasklist_being_filled); 270 pthread_cond_destroy(&tasklist_being_filled);
277 byte_zero( all_torrents, sizeof( all_torrents ) ); 271 byte_zero(all_torrents, sizeof(all_torrents));
278} 272}
279
280const char *g_version_mutex_c = "$Source$: $Revision$\n";