Correctly set python extension dir when prefix is nonstandard
[elliptics.git] / library / locks.c
blobd554561ec0e8d257fb7f43ae0c7faf832f1eaaf3
1 /*
2 * 2011+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
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.
16 #include <sys/stat.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <signal.h>
24 #include "elliptics.h"
26 void dnet_locks_destroy(struct dnet_node *n)
28 int i;
30 if (n->locks) {
31 for (i = 0; i < n->locks->num; ++i) {
32 pthread_mutex_destroy(&n->locks->lock[i]);
35 free(n->locks);
36 n->locks = NULL;
40 int dnet_locks_init(struct dnet_node *n, int num)
42 int err, i;
44 n->locks = malloc(sizeof(struct dnet_locks) + num * sizeof(pthread_mutex_t));
45 if (!n->locks) {
46 err = -ENOMEM;
47 goto err_out_exit;
50 n->locks->num = num;
52 for (i = 0; i < num; ++i) {
53 err = pthread_mutex_init(&n->locks->lock[i], NULL);
54 if (err) {
55 err = -err;
56 dnet_log(n, DNET_LOG_ERROR, "Could not create lock %d/%d: %s [%d]\n", i, num, strerror(-err), err);
58 n->locks->num = i;
59 goto err_out_destroy;
63 return 0;
65 err_out_destroy:
66 dnet_locks_destroy(n);
67 err_out_exit:
68 return err;
71 static unsigned int dnet_ophash_index(struct dnet_node *n, struct dnet_id *key)
73 unsigned int *ptr = (unsigned int *)key->id;
74 unsigned int h = 0;
75 unsigned int i;
77 for (i = 0; i < sizeof(key->id) / sizeof(unsigned int); ++i) {
78 h ^= ptr[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);
101 int err;
103 err = pthread_mutex_trylock(&n->locks->lock[idx]);
104 return err;