1 /* SPDX-License-Identifier: GPL-2.0 */
17 #include <sys/param.h>
20 #include "gcc-compat.h"
23 #include "gcc-compat.h"
27 #if (defined(__GNUC__) && defined(__i386__))
28 #define get16bits(d) (*((const uint16_t *) (d)))
30 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
31 +(uint32_t)(((const uint8_t *)(d))[0]) )
35 * SuperFastHash, by Paul Hsieh.
36 * http://www.azillionmonkeys.com/qed/hash.html
38 uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash)
45 for (;len > 0; len--) {
46 hash += get16bits (data);
47 tmp = (get16bits (data+2) << 11) ^ hash;
48 hash = (hash << 16) ^ tmp;
49 data += 2*sizeof (uint16_t);
53 /* Handle end cases */
56 hash += get16bits (data);
58 hash ^= data[sizeof (uint16_t)] << 18;
62 hash += get16bits (data);
71 /* Force "avalanching" of final 127 bits */
81 static int mutex_get(key_t key, int flags)
85 DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key));
86 ret = semget(key, 2, flags);
88 return -ERRNO_TO_DSS_ERROR(errno);
92 static int do_semop(int id, struct sembuf *sops, int num)
96 DSS_DEBUG_LOG(("calling semop\n"));
98 ret = semop(id, sops, num);
101 } while (errno == EINTR);
102 return -ERRNO_TO_DSS_ERROR(errno);
105 static bool mutex_is_locked(int id)
110 DSS_DEBUG_LOG(("trying to lock\n"));
114 sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
116 ret = do_semop(id, &sops, 1);
122 int lock_dss(uint32_t key)
125 struct sembuf sops[4];
127 ret = mutex_get(key, IPC_CREAT | 0600);
134 sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
138 sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
142 sops[2].sem_flg = SEM_UNDO | IPC_NOWAIT;
146 sops[3].sem_flg = SEM_UNDO | IPC_NOWAIT;
148 return do_semop(id, sops, 4);
151 int get_dss_pid(uint32_t key, pid_t *pid)
157 ret = mutex_get(key, 0);
159 return ret == -ERRNO_TO_DSS_ERROR(ENOENT)? -E_NOT_RUNNING : ret;
161 ret = semctl(semid, 1, GETPID);
163 return -E_NOT_RUNNING;
166 return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;