Statistics
| Branch: | Revision:

streamers / chunklock.c @ 2843a018

History | View | Annotate | Download (1.36 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7
#include <stdlib.h>
8
#include <stdio.h>
9
#include <string.h>
10
#include <stdint.h>
11

    
12
#include "chunklock.h"
13

    
14
#include "net_helper.h"
15

    
16
struct lock {
17
  int chunkid;
18
  struct nodeID *peer;
19
  struct timeval timestamp;
20
};
21

    
22
struct lock *locks;
23
static size_t lsize, lcount=0;
24
#define LSIZE_INCREMENT 10
25

    
26
void locks_init()
27
{
28
  if (!locks) {
29
    lsize = LSIZE_INCREMENT;
30
    locks = malloc(sizeof(struct lock) * lsize);
31
    lcount = 0;
32
  }
33

    
34
  if (lcount == lsize) {
35
    lsize += LSIZE_INCREMENT;
36
    locks = realloc(locks , sizeof(struct lock) * lsize);
37
  }
38

    
39
  if (!locks) {
40
    fprintf(stderr, "Error allocating memory for locks!\n");
41
    exit(EXIT_FAILURE);
42
  }
43
}
44

    
45

    
46
void chunk_lock(int chunkid,struct peer *from){
47
  locks_init();
48
  locks[lcount].chunkid = chunkid;
49
  locks[lcount].peer = nodeid_dup(from->id);
50
  gettimeofday(&locks[lcount].timestamp,NULL);
51
  lcount++;
52
}
53

    
54
void chunk_unlock(int chunkid){
55
  int i;
56

    
57
  for (i=0; i<lcount; i++) {
58
    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--;
62
      break;
63
    }
64
  }
65
}
66

    
67
int chunk_islocked(int chunkid){
68
  int i;
69

    
70
  for (i=0; i<lcount; i++) {
71
    if (locks[i].chunkid == chunkid) {
72
      return 1;
73
    }
74
  }
75
  return 0;
76
}