-
Notifications
You must be signed in to change notification settings - Fork 318
/
lock.c
154 lines (117 loc) · 2.63 KB
/
lock.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2015 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*/
#include "iio-config.h"
#include <iio/iio-backend.h>
#include <iio/iio-lock.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
struct iio_mutex {
pthread_mutex_t lock;
};
struct iio_cond {
pthread_cond_t cond;
};
struct iio_thrd {
pthread_t thid;
void *d;
int (*func)(void *);
};
struct iio_mutex * iio_mutex_create(void)
{
struct iio_mutex *lock = malloc(sizeof(*lock));
if (!lock)
return iio_ptr(-ENOMEM);
pthread_mutex_init(&lock->lock, NULL);
return lock;
}
void iio_mutex_destroy(struct iio_mutex *lock)
{
pthread_mutex_destroy(&lock->lock);
free(lock);
}
void iio_mutex_lock(struct iio_mutex *lock)
{
pthread_mutex_lock(&lock->lock);
}
void iio_mutex_unlock(struct iio_mutex *lock)
{
pthread_mutex_unlock(&lock->lock);
}
struct iio_cond * iio_cond_create(void)
{
struct iio_cond *cond = malloc(sizeof(*cond));
if (!cond)
return iio_ptr(-ENOMEM);
pthread_cond_init(&cond->cond, NULL);
return cond;
}
void iio_cond_destroy(struct iio_cond *cond)
{
pthread_cond_destroy(&cond->cond);
free(cond);
}
int iio_cond_wait(struct iio_cond *cond, struct iio_mutex *lock,
unsigned int timeout_ms)
{
struct timespec ts;
uint64_t usec;
int ret = 0;
if (timeout_ms == 0) {
pthread_cond_wait(&cond->cond, &lock->lock);
} else {
clock_gettime(CLOCK_REALTIME, &ts);
usec = ts.tv_sec * 1000000ull + ts.tv_nsec / 1000;
usec += timeout_ms * 1000ull;
ts.tv_sec = usec / 1000000;
ts.tv_nsec = (usec % 1000000) * 1000;
ret = - pthread_cond_timedwait(&cond->cond, &lock->lock, &ts);
}
return ret;
}
void iio_cond_signal(struct iio_cond *cond)
{
pthread_cond_signal(&cond->cond);
}
static void * iio_thrd_wrapper(void *d)
{
struct iio_thrd *thrd = d;
return (void *)(intptr_t) thrd->func(thrd->d);
}
struct iio_thrd * iio_thrd_create(int (*thrd)(void *),
void *d, const char *name)
{
struct iio_thrd *iio_thrd;
int ret;
if (!thrd)
return iio_ptr(-EINVAL);
iio_thrd = malloc(sizeof(*iio_thrd));
if (!iio_thrd)
return iio_ptr(-ENOMEM);
iio_thrd->d = d;
iio_thrd->func = thrd;
ret = pthread_create(&iio_thrd->thid, NULL,
iio_thrd_wrapper, iio_thrd);
if (ret) {
free(iio_thrd);
return iio_ptr(ret);
}
/* TODO: Set name */
return iio_thrd;
}
int iio_thrd_join_and_destroy(struct iio_thrd *thrd)
{
void *retval = NULL;
int ret;
ret = pthread_join(thrd->thid, &retval);
if (ret < 0)
return ret;
free(thrd);
return (int)(intptr_t) retval;
}