diff options
Diffstat (limited to 'ot_mutex.c')
| -rw-r--r-- | ot_mutex.c | 308 |
1 files changed, 150 insertions, 158 deletions
| @@ -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 */ |
| 27 | static ot_vector all_torrents[OT_BUCKET_COUNT]; | 28 | static ot_vector all_torrents[OT_BUCKET_COUNT]; |
| 28 | static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT]; | 29 | static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT]; |
| 29 | static size_t g_torrent_count; | 30 | static size_t g_torrent_count; |
| 30 | 31 | ||
| 31 | /* Self pipe from opentracker.c */ | 32 | /* Self pipe from opentracker.c */ |
| 32 | extern int g_self_pipe[2]; | 33 | extern int g_self_pipe[2]; |
| 33 | 34 | ||
| 34 | ot_vector *mutex_bucket_lock( int bucket ) { | 35 | ot_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 | ||
| 39 | ot_vector *mutex_bucket_lock_by_hash( ot_hash hash ) { | 40 | ot_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 | ||
| 43 | void mutex_bucket_unlock( int bucket, int delta_torrentcount ) { | 42 | void 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 | ||
| 48 | void mutex_bucket_unlock_by_hash( ot_hash hash, int delta_torrentcount ) { | 47 | void 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 | ||
| 52 | size_t mutex_get_torrent_count( ) { | 51 | size_t mutex_get_torrent_count() { return g_torrent_count; } |
| 53 | return g_torrent_count; | ||
| 54 | } | ||
| 55 | 52 | ||
| 56 | /* TaskQueue Magic */ | 53 | /* TaskQueue Magic */ |
| 57 | 54 | ||
| @@ -64,32 +61,17 @@ struct ot_task { | |||
| 64 | struct ot_task *next; | 61 | struct ot_task *next; |
| 65 | }; | 62 | }; |
| 66 | 63 | ||
| 67 | static ot_taskid next_free_taskid = 1; | 64 | static ot_taskid next_free_taskid = 1; |
| 68 | static struct ot_task *tasklist; | 65 | static struct ot_task *tasklist; |
| 69 | static pthread_mutex_t tasklist_mutex; | 66 | static pthread_mutex_t tasklist_mutex; |
| 70 | static pthread_cond_t tasklist_being_filled; | 67 | static pthread_cond_t tasklist_being_filled; |
| 71 | 68 | ||
| 72 | int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) { | 69 | int mutex_workqueue_pushtask(int64 sock, ot_tasktype tasktype) { |
| 73 | struct ot_task ** tmptask, * task; | 70 | struct ot_task **tmptask, *task; |
| 74 | 71 | ||
| 75 | /* Want exclusive access to tasklist */ | 72 | task = malloc(sizeof(struct ot_task)); |
| 76 | MTX_DBG( "pushtask locks.\n" ); | 73 | if (!task) |
| 77 | pthread_mutex_lock( &tasklist_mutex ); | ||
| 78 | MTX_DBG( "pushtask locked.\n" ); | ||
| 79 | |||
| 80 | task = malloc(sizeof( struct ot_task)); | ||
| 81 | 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 | ||
| 110 | void mutex_workqueue_canceltask( int64 sock ) { | 98 | void 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 | int 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 | ||
| 141 | ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) { | 123 | ot_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 |
| 155 | 135 | and leave the loop */ | |
| 156 | /* If we found an outstanding task, assign a taskid to it | 136 | task->taskid = taskid = ++next_free_taskid; |
| 157 | and leave the loop */ | 137 | *tasktype = task->tasktype; |
| 158 | if( task ) { | 138 | break; |
| 159 | task->taskid = taskid = ++next_free_taskid; | 139 | } |
| 160 | *tasktype = task->tasktype; | 140 | |
| 161 | } else { | 141 | /* Wait until the next task is being fed */ |
| 162 | /* Wait until the next task is being fed */ | 142 | if (!taskid) |
| 163 | MTX_DBG( "poptask cond waits.\n" ); | 143 | pthread_cond_wait(&tasklist_being_filled, &tasklist_mutex); |
| 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 | ||
| 177 | void mutex_workqueue_pushsuccess( ot_taskid taskid ) { | 152 | void 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 ); | 157 | |
| 183 | MTX_DBG( "pushsuccess locked.\n" ); | 158 | for (task = &tasklist; *task; task = &((*task)->next)) |
| 184 | 159 | if ((*task)->taskid == taskid) { | |
| 185 | task = &tasklist; | 160 | struct ot_task *ptask = *task; |
| 186 | while( *task && ( (*task)->taskid != taskid ) ) | 161 | *task = (*task)->next; |
| 187 | *task = (*task)->next; | 162 | free(ptask); |
| 188 | 163 | break; | |
| 189 | if( *task && ( (*task)->taskid == taskid ) ) { | 164 | } |
| 190 | struct ot_task *ptask = *task; | ||
| 191 | *task = (*task)->next; | ||
| 192 | free( ptask ); | ||
| 193 | } | ||
| 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 | ||
| 201 | int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) { | 170 | int mutex_workqueue_pushresult(ot_taskid taskid, int 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 ); | 176 | |
| 208 | MTX_DBG( "pushresult locked.\n" ); | 177 | for (task = tasklist; task; task = task->next) |
| 209 | 178 | if (task->taskid == taskid) { | |
| 210 | task = tasklist; | 179 | task->iovec_entries = iovec_entries; |
| 211 | while( task && ( task->taskid != taskid ) ) | 180 | task->iovec = iovec; |
| 212 | task = task->next; | 181 | task->tasktype = TASK_DONE; |
| 213 | 182 | break; | |
| 214 | if( task ) { | 183 | } |
| 215 | task->iovec_entries = iovec_entries; | ||
| 216 | task->iovec = iovec; | ||
| 217 | task->tasktype = TASK_DONE; | ||
| 218 | } | ||
| 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 | ||
| 231 | int64 mutex_workqueue_popresult( int *iovec_entries, struct iovec ** iovec ) { | 194 | int 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 ); | 200 | |
| 238 | 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 | } | ||
| 212 | |||
| 213 | /* Release lock */ | ||
| 214 | pthread_mutex_unlock(&tasklist_mutex); | ||
| 239 | 215 | ||
| 240 | task = &tasklist; | 216 | io_trywrite(g_self_pipe[1], &byte, 1); |
| 241 | while( *task && ( (*task)->tasktype != TASK_DONE ) ) | ||
| 242 | task = &(*task)->next; | ||
| 243 | 217 | ||
| 244 | if( *task && ( (*task)->tasktype == TASK_DONE ) ) { | 218 | /* Indicate whether the worker has to throw away results */ |
| 245 | struct ot_task *ptask = *task; | 219 | return task ? 0 : -1; |
| 220 | } | ||
| 246 | 221 | ||
| 247 | *iovec_entries = (*task)->iovec_entries; | 222 | int64 mutex_workqueue_popresult(int *iovec_entries, struct iovec **iovec, int *is_partial) { |
| 248 | *iovec = (*task)->iovec; | 223 | struct ot_task **task; |
| 249 | sock = (*task)->sock; | 224 | int64 sock = -1; |
| 250 | 225 | ||
| 251 | *task = (*task)->next; | 226 | *is_partial = 0; |
| 252 | free( ptask ); | 227 | |
| 253 | } | 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 | ||
| 262 | void mutex_init( ) { | 256 | void mutex_init() { |
| 263 | int i; | 257 | int 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 | ||
| 271 | void mutex_deinit( ) { | 265 | void mutex_deinit() { |
| 272 | int i; | 266 | int 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 | |||
| 280 | const char *g_version_mutex_c = "$Source$: $Revision$\n"; | ||
