2 * 2011+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
24 #include "elliptics.h"
26 void dnet_locks_destroy(struct dnet_node
*n
)
31 for (i
= 0; i
< n
->locks
->num
; ++i
) {
32 pthread_mutex_destroy(&n
->locks
->lock
[i
]);
40 int dnet_locks_init(struct dnet_node
*n
, int num
)
44 n
->locks
= malloc(sizeof(struct dnet_locks
) + num
* sizeof(pthread_mutex_t
));
52 for (i
= 0; i
< num
; ++i
) {
53 err
= pthread_mutex_init(&n
->locks
->lock
[i
], NULL
);
56 dnet_log(n
, DNET_LOG_ERROR
, "Could not create lock %d/%d: %s [%d]\n", i
, num
, strerror(-err
), err
);
66 dnet_locks_destroy(n
);
71 static unsigned int dnet_ophash_index(struct dnet_node
*n
, struct dnet_id
*key
)
73 unsigned int *ptr
= (unsigned int *)key
->id
;
77 for (i
= 0; i
< sizeof(key
->id
) / sizeof(unsigned int); ++i
) {
81 return h
% n
->locks
->num
;
84 void dnet_oplock(struct dnet_node
*n
, struct dnet_id
*key
)
86 unsigned int idx
= dnet_ophash_index(n
, key
);
88 pthread_mutex_lock(&n
->locks
->lock
[idx
]);
91 void dnet_opunlock(struct dnet_node
*n
, struct dnet_id
*key
)
93 unsigned int idx
= dnet_ophash_index(n
, key
);
95 pthread_mutex_unlock(&n
->locks
->lock
[idx
]);
98 int dnet_optrylock(struct dnet_node
*n
, struct dnet_id
*key
)
100 unsigned int idx
= dnet_ophash_index(n
, key
);
103 err
= pthread_mutex_trylock(&n
->locks
->lock
[idx
]);