-
Notifications
You must be signed in to change notification settings - Fork 2
/
dethread.c
83 lines (64 loc) · 1.87 KB
/
dethread.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
#include <pthread.h>
#include <talloc.h>
#include "dethread.h"
#include "rngod.h"
#include "rng-private.h"
struct rng_dethread {
struct rngod rng;
pthread_mutex_t lock;
struct rngod *locked;
};
static uint64_t dethread_rand(struct rngod*);
static uint64_t dethread_range(struct rngod *, uint64_t min, uint64_t max);
static uint32_t dethread_dx(struct rngod *, uint32_t x);
static uint32_t dethread_ndx(struct rngod *, uint32_t n, uint32_t x);
struct rngod *
rngod_dethread_add(struct rngod *rng) {
struct rng_dethread *rngd;
rngd = talloc(NULL, struct rng_dethread);
rngd->rng.rand = dethread_rand;
rngd->rng.dx = dethread_dx;
rngd->rng.ndx = dethread_ndx;
rngd->rng.range = dethread_range;
rngd->locked = rng;
/* Take ownership */
talloc_steal(rngd, rng);
pthread_mutex_init(&rngd->lock, NULL);
return (struct rngod *)rngd;
}
static uint64_t
dethread_rand(struct rngod *rng){
uint64_t rv;
struct rng_dethread *rngd = (struct rng_dethread *)rng;
pthread_mutex_lock(&rngd->lock);
rv = rngd->locked->rand(rngd->locked);
pthread_mutex_unlock(&rngd->lock);
return rv;
}
static uint64_t
dethread_range(struct rngod *rng, uint64_t min, uint64_t max){
uint32_t rv;
struct rng_dethread *rngd = (struct rng_dethread *)rng;
pthread_mutex_lock(&rngd->lock);
rv = rngd->locked->range(rngd->locked, min, max);
pthread_mutex_unlock(&rngd->lock);
return rv;
}
static uint32_t
dethread_dx(struct rngod *rng, uint32_t x){
uint32_t rv;
struct rng_dethread *rngd = (struct rng_dethread *)rng;
pthread_mutex_lock(&rngd->lock);
rv = rngd->locked->dx(rngd->locked, x);
pthread_mutex_unlock(&rngd->lock);
return rv;
}
static uint32_t
dethread_ndx(struct rngod *rng, uint32_t n, uint32_t x){
uint32_t rv;
struct rng_dethread *rngd = (struct rng_dethread *)rng;
pthread_mutex_lock(&rngd->lock);
rv = rngd->locked->ndx(rngd->locked, n, x);
pthread_mutex_unlock(&rngd->lock);
return rv;
}