Revision b8a88cc5

View differences:

chunklock.c
13 13

  
14 14
#include "net_helper.h"
15 15

  
16
static struct timeval toutdiff = {0, 300000};
17

  
18

  
19

  
16 20
struct lock {
17 21
  int chunkid;
18 22
  struct nodeID *peer;
......
42 46
  }
43 47
}
44 48

  
49
int chunk_lock_timed_out(struct lock *l)
50
{
51
  struct timeval tnow,tout;
52
  gettimeofday(&tnow, NULL);
53
  timeradd(&l->timestamp, &toutdiff, &tout);
54

  
55
  return timercmp(&tnow, &tout, >);
56
}
57

  
58
void chunk_lock_remove(struct lock *l){
59
  nodeID_free(l->peer);
60
  memmove(l, l+1, sizeof(struct lock) * (locks+lcount-l-1));
61
  lcount--;
62
}
63

  
64
void chunk_locks_cleanup(){
65
  int i;
66

  
67
  for (i=lcount-1; i>=0; i--) {
68
    if (chunk_lock_timed_out(locks+i)) {
69
      chunk_lock_remove(locks+i);
70
    }
71
  }
72
}
45 73

  
46 74
void chunk_lock(int chunkid,struct peer *from){
47 75
  locks_init();
......
56 84

  
57 85
  for (i=0; i<lcount; i++) {
58 86
    if (locks[i].chunkid == chunkid) {
59
      nodeID_free(locks[i].peer);
60
      memmove(locks+i, locks+i+1, sizeof(struct lock) * (lcount-i-1));
61
      lcount--;
87
      chunk_lock_remove(locks+i);
62 88
      break;
63 89
    }
64 90
  }
......
67 93
int chunk_islocked(int chunkid){
68 94
  int i;
69 95

  
96
  chunk_locks_cleanup();
97

  
70 98
  for (i=0; i<lcount; i++) {
71 99
    if (locks[i].chunkid == chunkid) {
72 100
      return 1;

Also available in: Unified diff