forked from ShawnZhong/tpcc_sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.c
82 lines (69 loc) · 1.23 KB
/
sequence.c
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
/*
* sequence.c
* manage sequence shared by threads
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* weight */
static int no;
static int py;
static int os;
static int dl;
static int sl;
static int total;
static pthread_mutex_t mutex;
static int *seq;
static int next_num;
static void shuffle()
{
int i,j,rnd,tmp;
for( i=0, j=0; i < no ; i++, j++ ){
seq[j]=0;
}
for( i=0; i < py ; i++, j++){
seq[j]=1;
}
for( i=0; i < os ; i++, j++){
seq[j]=2;
}
for( i=0; i < dl ; i++, j++){
seq[j]=3;
}
for( i=0; i < sl ; i++, j++){
seq[j]=4;
}
for( i=0, j = total - 1; j>0; i++, j--){
rnd = rand()%(j+1);
tmp = seq[rnd+i];
seq[rnd+i] = seq[i];
seq[i] = tmp;
}
}
void seq_init( int n, int p, int o, int d, int s )
{
pthread_mutex_init( &mutex, NULL );
no = n;
py = p;
os = o;
dl = d;
sl = s;
printf("seq_init with %d %d %d %d %d\n", n, p, o, d, s);
total = n + p + o + d + s;
seq = malloc( sizeof(int) * total );
shuffle();
next_num = 0;
}
int seq_get()
{
int retval;
pthread_mutex_lock( &mutex );
if(next_num >= total){
shuffle();
next_num = 0;
}
retval = seq[next_num];
++next_num;
pthread_mutex_unlock( &mutex );
return(retval);
}