An open and free bittorrent tracker https://erdgeist.org/gitweb/opentracker
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
9.0 KiB

13 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. /* This software was written by Dirk Engling <erdgeist@erdgeist.org>
  2. It is considered beerware. Prost. Skol. Cheers or whatever.
  3. $id$ */
  4. /* System */
  5. #include <pthread.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/mman.h>
  9. #include <sys/uio.h>
  10. /* Libowfat */
  11. #include "byte.h"
  12. #include "io.h"
  13. #include "uint32.h"
  14. /* Opentracker */
  15. #include "trackerlogic.h"
  16. #include "ot_mutex.h"
  17. #include "ot_stats.h"
  18. /* #define MTX_DBG( STRING ) fprintf( stderr, STRING ) */
  19. #define MTX_DBG( STRING )
  20. /* Our global all torrents list */
  21. static ot_vector all_torrents[OT_BUCKET_COUNT];
  22. static size_t g_torrent_count;
  23. /* Bucket Magic */
  24. static int bucket_locklist[ OT_MAX_THREADS ];
  25. static int bucket_locklist_count = 0;
  26. static pthread_mutex_t bucket_mutex;
  27. static pthread_cond_t bucket_being_unlocked;
  28. /* Self pipe from opentracker.c */
  29. extern int g_self_pipe[2];
  30. static int bucket_check( int bucket ) {
  31. /* C should come with auto-i ;) */
  32. int i;
  33. /* No more space to acquire lock to bucket -- should not happen */
  34. if( bucket_locklist_count == OT_MAX_THREADS ) {
  35. fprintf( stderr, "More lock requests than mutexes. Consult source code.\n" );
  36. return -1;
  37. }
  38. /* See, if bucket is already locked */
  39. for( i=0; i<bucket_locklist_count; ++i )
  40. if( bucket_locklist[ i ] == bucket ) {
  41. stats_issue_event( EVENT_BUCKET_LOCKED, 0, 0 );
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. static void bucket_push( int bucket ) {
  47. bucket_locklist[ bucket_locklist_count++ ] = bucket;
  48. }
  49. static void bucket_remove( int bucket ) {
  50. int i = 0;
  51. while( ( i < bucket_locklist_count ) && ( bucket_locklist[ i ] != bucket ) )
  52. ++i;
  53. if( i == bucket_locklist_count ) {
  54. fprintf( stderr, "Request to unlock bucket that was never locked. Consult source code.\n" );
  55. return;
  56. }
  57. for( ; i < bucket_locklist_count - 1; ++i )
  58. bucket_locklist[ i ] = bucket_locklist[ i + 1 ];
  59. --bucket_locklist_count;
  60. }
  61. /* Can block */
  62. ot_vector *mutex_bucket_lock( int bucket ) {
  63. pthread_mutex_lock( &bucket_mutex );
  64. while( bucket_check( bucket ) )
  65. pthread_cond_wait( &bucket_being_unlocked, &bucket_mutex );
  66. bucket_push( bucket );
  67. pthread_mutex_unlock( &bucket_mutex );
  68. return all_torrents + bucket;
  69. }
  70. ot_vector *mutex_bucket_lock_by_hash( ot_hash hash ) {
  71. return mutex_bucket_lock( uint32_read_big( (char*)hash ) >> OT_BUCKET_COUNT_SHIFT );
  72. }
  73. void mutex_bucket_unlock( int bucket, int delta_torrentcount ) {
  74. pthread_mutex_lock( &bucket_mutex );
  75. bucket_remove( bucket );
  76. g_torrent_count += delta_torrentcount;
  77. pthread_cond_broadcast( &bucket_being_unlocked );
  78. pthread_mutex_unlock( &bucket_mutex );
  79. }
  80. void mutex_bucket_unlock_by_hash( ot_hash hash, int delta_torrentcount ) {
  81. mutex_bucket_unlock( uint32_read_big( (char*)hash ) >> OT_BUCKET_COUNT_SHIFT, delta_torrentcount );
  82. }
  83. size_t mutex_get_torrent_count( ) {
  84. size_t torrent_count;
  85. pthread_mutex_lock( &bucket_mutex );
  86. torrent_count = g_torrent_count;
  87. pthread_mutex_unlock( &bucket_mutex );
  88. return torrent_count;
  89. }
  90. /* TaskQueue Magic */
  91. struct ot_task {
  92. ot_taskid taskid;
  93. ot_tasktype tasktype;
  94. int64 sock;
  95. int iovec_entries;
  96. struct iovec *iovec;
  97. struct ot_task *next;
  98. };
  99. static ot_taskid next_free_taskid = 1;
  100. static struct ot_task *tasklist;
  101. static pthread_mutex_t tasklist_mutex;
  102. static pthread_cond_t tasklist_being_filled;
  103. int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) {
  104. struct ot_task ** tmptask, * task;
  105. /* Want exclusive access to tasklist */
  106. MTX_DBG( "pushtask locks.\n" );
  107. pthread_mutex_lock( &tasklist_mutex );
  108. MTX_DBG( "pushtask locked.\n" );
  109. task = malloc(sizeof( struct ot_task));
  110. if( !task ) {
  111. MTX_DBG( "pushtask fail unlocks.\n" );
  112. pthread_mutex_unlock( &tasklist_mutex );
  113. MTX_DBG( "pushtask fail unlocked.\n" );
  114. return -1;
  115. }
  116. /* Skip to end of list */
  117. tmptask = &tasklist;
  118. while( *tmptask )
  119. tmptask = &(*tmptask)->next;
  120. *tmptask = task;
  121. task->taskid = 0;
  122. task->tasktype = tasktype;
  123. task->sock = sock;
  124. task->iovec_entries = 0;
  125. task->iovec = NULL;
  126. task->next = 0;
  127. /* Inform waiting workers and release lock */
  128. MTX_DBG( "pushtask broadcasts.\n" );
  129. pthread_cond_broadcast( &tasklist_being_filled );
  130. MTX_DBG( "pushtask broadcasted, mutex unlocks.\n" );
  131. pthread_mutex_unlock( &tasklist_mutex );
  132. MTX_DBG( "pushtask end mutex unlocked.\n" );
  133. return 0;
  134. }
  135. void mutex_workqueue_canceltask( int64 sock ) {
  136. struct ot_task ** task;
  137. /* Want exclusive access to tasklist */
  138. MTX_DBG( "canceltask locks.\n" );
  139. pthread_mutex_lock( &tasklist_mutex );
  140. MTX_DBG( "canceltask locked.\n" );
  141. task = &tasklist;
  142. while( *task && ( (*task)->sock != sock ) )
  143. *task = (*task)->next;
  144. if( *task && ( (*task)->sock == sock ) ) {
  145. struct iovec *iovec = (*task)->iovec;
  146. struct ot_task *ptask = *task;
  147. int i;
  148. /* Free task's iovec */
  149. for( i=0; i<(*task)->iovec_entries; ++i )
  150. munmap( iovec[i].iov_base, iovec[i].iov_len );
  151. *task = (*task)->next;
  152. free( ptask );
  153. }
  154. /* Release lock */
  155. MTX_DBG( "canceltask unlocks.\n" );
  156. pthread_mutex_unlock( &tasklist_mutex );
  157. MTX_DBG( "canceltask unlocked.\n" );
  158. }
  159. ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) {
  160. struct ot_task * task;
  161. ot_taskid taskid = 0;
  162. /* Want exclusive access to tasklist */
  163. MTX_DBG( "poptask mutex locks.\n" );
  164. pthread_mutex_lock( &tasklist_mutex );
  165. MTX_DBG( "poptask mutex locked.\n" );
  166. while( !taskid ) {
  167. /* Skip to the first unassigned task this worker wants to do */
  168. task = tasklist;
  169. while( task && ( ( ( TASK_CLASS_MASK & task->tasktype ) != *tasktype ) || task->taskid ) )
  170. task = task->next;
  171. /* If we found an outstanding task, assign a taskid to it
  172. and leave the loop */
  173. if( task ) {
  174. task->taskid = taskid = ++next_free_taskid;
  175. *tasktype = task->tasktype;
  176. } else {
  177. /* Wait until the next task is being fed */
  178. MTX_DBG( "poptask cond waits.\n" );
  179. pthread_cond_wait( &tasklist_being_filled, &tasklist_mutex );
  180. MTX_DBG( "poptask cond waited.\n" );
  181. }
  182. }
  183. /* Release lock */
  184. MTX_DBG( "poptask end mutex unlocks.\n" );
  185. pthread_mutex_unlock( &tasklist_mutex );
  186. MTX_DBG( "poptask end mutex unlocked.\n" );
  187. return taskid;
  188. }
  189. void mutex_workqueue_pushsuccess( ot_taskid taskid ) {
  190. struct ot_task ** task;
  191. /* Want exclusive access to tasklist */
  192. MTX_DBG( "pushsuccess locks.\n" );
  193. pthread_mutex_lock( &tasklist_mutex );
  194. MTX_DBG( "pushsuccess locked.\n" );
  195. task = &tasklist;
  196. while( *task && ( (*task)->taskid != taskid ) )
  197. *task = (*task)->next;
  198. if( *task && ( (*task)->taskid == taskid ) ) {
  199. struct ot_task *ptask = *task;
  200. *task = (*task)->next;
  201. free( ptask );
  202. }
  203. /* Release lock */
  204. MTX_DBG( "pushsuccess unlocks.\n" );
  205. pthread_mutex_unlock( &tasklist_mutex );
  206. MTX_DBG( "pushsuccess unlocked.\n" );
  207. }
  208. int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) {
  209. struct ot_task * task;
  210. const char byte = 'o';
  211. /* Want exclusive access to tasklist */
  212. MTX_DBG( "pushresult locks.\n" );
  213. pthread_mutex_lock( &tasklist_mutex );
  214. MTX_DBG( "pushresult locked.\n" );
  215. task = tasklist;
  216. while( task && ( task->taskid != taskid ) )
  217. task = task->next;
  218. if( task ) {
  219. task->iovec_entries = iovec_entries;
  220. task->iovec = iovec;
  221. task->tasktype = TASK_DONE;
  222. }
  223. /* Release lock */
  224. MTX_DBG( "pushresult unlocks.\n" );
  225. pthread_mutex_unlock( &tasklist_mutex );
  226. MTX_DBG( "pushresult unlocked.\n" );
  227. io_trywrite( g_self_pipe[1], &byte, 1 );
  228. /* Indicate whether the worker has to throw away results */
  229. return task ? 0 : -1;
  230. }
  231. int64 mutex_workqueue_popresult( int *iovec_entries, struct iovec ** iovec ) {
  232. struct ot_task ** task;
  233. int64 sock = -1;
  234. /* Want exclusive access to tasklist */
  235. MTX_DBG( "popresult locks.\n" );
  236. pthread_mutex_lock( &tasklist_mutex );
  237. MTX_DBG( "popresult locked.\n" );
  238. task = &tasklist;
  239. while( *task && ( (*task)->tasktype != TASK_DONE ) )
  240. task = &(*task)->next;
  241. if( *task && ( (*task)->tasktype == TASK_DONE ) ) {
  242. struct ot_task *ptask = *task;
  243. *iovec_entries = (*task)->iovec_entries;
  244. *iovec = (*task)->iovec;
  245. sock = (*task)->sock;
  246. *task = (*task)->next;
  247. free( ptask );
  248. }
  249. /* Release lock */
  250. MTX_DBG( "popresult unlocks.\n" );
  251. pthread_mutex_unlock( &tasklist_mutex );
  252. MTX_DBG( "popresult unlocked.\n" );
  253. return sock;
  254. }
  255. void mutex_init( ) {
  256. pthread_mutex_init(&tasklist_mutex, NULL);
  257. pthread_cond_init (&tasklist_being_filled, NULL);
  258. pthread_mutex_init(&bucket_mutex, NULL);
  259. pthread_cond_init (&bucket_being_unlocked, NULL);
  260. byte_zero( all_torrents, sizeof( all_torrents ) );
  261. }
  262. void mutex_deinit( ) {
  263. pthread_mutex_destroy(&bucket_mutex);
  264. pthread_cond_destroy(&bucket_being_unlocked);
  265. pthread_mutex_destroy(&tasklist_mutex);
  266. pthread_cond_destroy(&tasklist_being_filled);
  267. byte_zero( all_torrents, sizeof( all_torrents ) );
  268. }
  269. const char *g_version_mutex_c = "$Source: /home/cvsroot/opentracker/ot_mutex.c,v $: $Revision: 1.23 $\n";