summaryrefslogtreecommitdiff
path: root/ot_mutex.c
blob: 8094e99293f1b0cc620f0d9dcc6342ce4f174ec8 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

/* System */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/uio.h>

/* Libowfat */
#include "byte.h"
#include "io.h"
#include "uint32.h"

/* Opentracker */
#include "trackerlogic.h"
#include "ot_mutex.h"

//#define MTX_DBG( STRING ) fprintf( stderr, STRING )
#define MTX_DBG( STRING )

/* Our global all torrents list */
static ot_vector all_torrents[OT_BUCKET_COUNT];
static size_t    g_torrent_count;

/* Bucket Magic */
static int bucket_locklist[ OT_MAX_THREADS ];
static int bucket_locklist_count = 0;
static pthread_mutex_t bucket_mutex;
static pthread_cond_t bucket_being_unlocked;

static int bucket_check( int bucket ) {
  /* C should come with auto-i ;) */
  int i;

  /* No more space to acquire lock to bucket -- should not happen */
  if( bucket_locklist_count == OT_MAX_THREADS ) {
    fprintf( stderr, "More lock requests than mutexes. Consult source code.\n" );
    return -1;
  }

  /* See, if bucket is already locked */
  for( i=0; i<bucket_locklist_count; ++i )
    if( bucket_locklist[ i ] == bucket )
      return -1;

  return 0;
}

static void bucket_push( int bucket ) {
  bucket_locklist[ bucket_locklist_count++ ] = bucket;
}

static void bucket_remove( int bucket ) {
  int i = 0;

  while( ( i < bucket_locklist_count ) && ( bucket_locklist[ i ] != bucket ) )
    ++i;

  if( i == bucket_locklist_count ) {
    fprintf( stderr, "Request to unlock bucket that was never locked. Consult source code.\n" );
    return;
  }

  for( ; i < bucket_locklist_count - 1; ++i )
    bucket_locklist[ i ] = bucket_locklist[ i + 1 ];

  --bucket_locklist_count;
}

ot_vector *mutex_bucket_lock( int bucket ) {
  pthread_mutex_lock( &bucket_mutex );
  while( bucket_check( bucket ) )
    pthread_cond_wait( &bucket_being_unlocked, &bucket_mutex );
  bucket_push( bucket );
  pthread_mutex_unlock( &bucket_mutex );
  return all_torrents + bucket;
}

ot_vector *mutex_bucket_lock_by_hash( ot_hash *hash ) {
  int bucket =  uint32_read_big( (char*)*hash ) >> OT_BUCKET_COUNT_SHIFT;

  /* Can block */
  mutex_bucket_lock( bucket );
  return all_torrents + bucket;
}

void mutex_bucket_unlock( int bucket, int delta_torrentcount ) {
  pthread_mutex_lock( &bucket_mutex );
  bucket_remove( bucket );
  g_torrent_count += delta_torrentcount;
  pthread_cond_broadcast( &bucket_being_unlocked );
  pthread_mutex_unlock( &bucket_mutex );
}

void mutex_bucket_unlock_by_hash( ot_hash *hash, int delta_torrentcount ) {
  mutex_bucket_unlock( uint32_read_big( (char*)*hash ) >> OT_BUCKET_COUNT_SHIFT, delta_torrentcount );
}

size_t mutex_get_torrent_count( ) {
  size_t torrent_count;
  pthread_mutex_lock( &bucket_mutex );
  torrent_count = g_torrent_count;
  pthread_mutex_unlock( &bucket_mutex );
  return torrent_count;
}

/* TaskQueue Magic */

struct ot_task {
  ot_taskid       taskid;
  ot_tasktype     tasktype;
  int64           socket;
  int             iovec_entries;
  struct iovec   *iovec;
  struct ot_task *next;
};

static ot_taskid next_free_taskid = 1;
static struct ot_task *tasklist = NULL;
static pthread_mutex_t tasklist_mutex;
static pthread_cond_t tasklist_being_filled;

int mutex_workqueue_pushtask( int64 socket, ot_tasktype tasktype ) {
  struct ot_task ** tmptask, * task;

  /* Want exclusive access to tasklist */
  MTX_DBG( "pushtask locks.\n" );
  pthread_mutex_lock( &tasklist_mutex );
  MTX_DBG( "pushtask locked.\n" );

  task = malloc(sizeof( struct ot_task));
  if( !task ) {
    MTX_DBG( "pushtask fail unlocks.\n" );
    pthread_mutex_unlock( &tasklist_mutex );
    MTX_DBG( "pushtask fail unlocked.\n" );
    return -1;
  }

  /* Skip to end of list */
  tmptask = &tasklist;
  while( *tmptask )
    tmptask = &(*tmptask)->next;
  *tmptask = task;

  task->taskid        = 0;
  task->tasktype      = tasktype;
  task->socket        = socket;
  task->iovec_entries = 0;
  task->iovec         = NULL;
  task->next          = 0;

  /* Inform waiting workers and release lock */
  MTX_DBG( "pushtask broadcasts.\n" );
  pthread_cond_broadcast( &tasklist_being_filled );
  MTX_DBG( "pushtask broadcasted, mutex unlocks.\n" );
  pthread_mutex_unlock( &tasklist_mutex );
  MTX_DBG( "pushtask end mutex unlocked.\n" );
  return 0;
}

void mutex_workqueue_canceltask( int64 socket ) {
  struct ot_task ** task;

  /* Want exclusive access to tasklist */
  MTX_DBG( "canceltask locks.\n" );
  pthread_mutex_lock( &tasklist_mutex );
  MTX_DBG( "canceltask locked.\n" );

  task = &tasklist;
  while( *task && ( (*task)->socket != socket ) )
    *task = (*task)->next;

  if( *task && ( (*task)->socket == socket ) ) {
    struct iovec *iovec = (*task)->iovec;
    struct ot_task *ptask = *task;
    int i;

    /* Free task's iovec */
    for( i=0; i<(*task)->iovec_entries; ++i )
      munmap( iovec[i].iov_base, iovec[i].iov_len );

    *task = (*task)->next;
    free( ptask );
  }

  /* Release lock */
  MTX_DBG( "canceltask unlocks.\n" );
  pthread_mutex_unlock( &tasklist_mutex );
  MTX_DBG( "canceltask unlocked.\n" );
}

ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) {
  struct ot_task * task;
  ot_taskid taskid = 0;

  /* Want exclusive access to tasklist */
  MTX_DBG( "poptask mutex locks.\n" );
  pthread_mutex_lock( &tasklist_mutex );
  MTX_DBG( "poptask mutex locked.\n" );

  while( !taskid ) {
    /* Skip to the first unassigned task this worker wants to do */
    task = tasklist;
    while( task && ( ( ( TASK_CLASS_MASK & task->tasktype ) != *tasktype ) || task->taskid ) )
      task = task->next;

    /* If we found an outstanding task, assign a taskid to it
       and leave the loop */
    if( task ) {
      task->taskid = taskid = ++next_free_taskid;
      *tasktype = task->tasktype;
    } else {
      /* Wait until the next task is being fed */
      MTX_DBG( "poptask cond waits.\n" );
      pthread_cond_wait( &tasklist_being_filled, &tasklist_mutex );
      MTX_DBG( "poptask cond waited.\n" );
    }
  }

  /* Release lock */
  MTX_DBG( "poptask end mutex unlocks.\n" );
  pthread_mutex_unlock( &tasklist_mutex );
  MTX_DBG( "poptask end mutex unlocked.\n" );

  return taskid;
}

void mutex_workqueue_pushsuccess( ot_taskid taskid ) {
  struct ot_task ** task;

  /* Want exclusive access to tasklist */
  MTX_DBG( "pushsuccess locks.\n" );
  pthread_mutex_lock( &tasklist_mutex );
  MTX_DBG( "pushsuccess locked.\n" );

  task = &tasklist;
  while( *task && ( (*task)->taskid != taskid ) )
    *task = (*task)->next;

  if( *task && ( (*task)->taskid == taskid ) ) {
    struct ot_task *ptask = *task;
    *task = (*task)->next;
    free( ptask );
  }

  /* Release lock */
  MTX_DBG( "pushsuccess unlocks.\n" );
  pthread_mutex_unlock( &tasklist_mutex );
  MTX_DBG( "pushsuccess unlocked.\n" );
}

int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) {
  struct ot_task * task;
  /* Want exclusive access to tasklist */
  MTX_DBG( "pushresult locks.\n" );
  pthread_mutex_lock( &tasklist_mutex );
  MTX_DBG( "pushresult locked.\n" );

  task = tasklist;
  while( task && ( task->taskid != taskid ) )
    task = task->next;

  if( task ) {
    task->iovec_entries = iovec_entries;
    task->iovec         = iovec;
    task->tasktype      = TASK_DONE;
  }

  /* Release lock */
  MTX_DBG( "pushresult unlocks.\n" );
  pthread_mutex_unlock( &tasklist_mutex );
  MTX_DBG( "pushresult unlocked.\n" );

  /* Indicate whether the worker has to throw away results */
  return task ? 0 : -1;
}

int64 mutex_workqueue_popresult( int *iovec_entries, struct iovec ** iovec ) {
  struct ot_task ** task;
  int64 socket = -1;

  /* Want exclusive access to tasklist */
  MTX_DBG( "popresult locks.\n" );
  pthread_mutex_lock( &tasklist_mutex );
  MTX_DBG( "popresult locked.\n" );

  task = &tasklist;
  while( *task && ( (*task)->tasktype != TASK_DONE ) )
    task = &(*task)->next;

  if( *task && ( (*task)->tasktype == TASK_DONE ) ) {
    struct ot_task *ptask = *task;

    *iovec_entries = (*task)->iovec_entries;
    *iovec         = (*task)->iovec;
    socket         = (*task)->socket;

    *task = (*task)->next;
    free( ptask );
  }

  /* Release lock */
  MTX_DBG( "popresult unlocks.\n" );
  pthread_mutex_unlock( &tasklist_mutex );
  MTX_DBG( "popresult unlocked.\n" );
  return socket;
}

void mutex_init( ) {
  pthread_mutex_init(&tasklist_mutex, NULL);
  pthread_cond_init (&tasklist_being_filled, NULL);
  pthread_mutex_init(&bucket_mutex, NULL);
  pthread_cond_init (&bucket_being_unlocked, NULL);
  byte_zero( all_torrents, sizeof( all_torrents ) );
}

void mutex_deinit( ) {
  pthread_mutex_destroy(&bucket_mutex);
  pthread_cond_destroy(&bucket_being_unlocked);
  pthread_mutex_destroy(&tasklist_mutex);
  pthread_cond_destroy(&tasklist_being_filled);
  byte_zero( all_torrents, sizeof( all_torrents ) );
}

const char *g_version_mutex_c = "$Source$: $Revision$\n";