forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock-windows.c
141 lines (105 loc) · 2.41 KB
/
lock-windows.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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2015-2021 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 <stdlib.h>
#include <windows.h>
struct iio_mutex {
CRITICAL_SECTION lock;
};
struct iio_cond {
CONDITION_VARIABLE cond;
};
struct iio_thrd {
HANDLE thid;
void *d;
int (*func)(void *d);
};
struct iio_mutex * iio_mutex_create(void)
{
struct iio_mutex *lock = malloc(sizeof(*lock));
if (!lock)
return iio_ptr(-ENOMEM);
InitializeCriticalSection(&lock->lock);
return lock;
}
void iio_mutex_destroy(struct iio_mutex *lock)
{
DeleteCriticalSection(&lock->lock);
free(lock);
}
void iio_mutex_lock(struct iio_mutex *lock)
{
EnterCriticalSection(&lock->lock);
}
void iio_mutex_unlock(struct iio_mutex *lock)
{
LeaveCriticalSection(&lock->lock);
}
struct iio_cond * iio_cond_create(void)
{
struct iio_cond *cond = malloc(sizeof(*cond));
if (!cond)
return iio_ptr(-ENOMEM);
InitializeConditionVariable(&cond->cond);
return cond;
}
void iio_cond_destroy(struct iio_cond *cond)
{
free(cond);
}
int iio_cond_wait(struct iio_cond *cond, struct iio_mutex *lock,
unsigned int timeout_ms)
{
BOOL ret;
if (timeout_ms == 0)
timeout_ms = INFINITE;
ret = SleepConditionVariableCS(&cond->cond, &lock->lock, timeout_ms);
return ret ? 0 : -ETIMEDOUT;
}
void iio_cond_signal(struct iio_cond *cond)
{
WakeConditionVariable(&cond->cond);
}
static DWORD iio_thrd_wrapper(void *d)
{
struct iio_thrd *thrd = d;
return (DWORD) thrd->func(thrd->d);
}
struct iio_thrd * iio_thrd_create(int (*thrd)(void *),
void *d, const char *name)
{
struct iio_thrd *iio_thrd;
if (!thrd)
return iio_ptr(-EINVAL);
iio_thrd = malloc(sizeof(*iio_thrd));
if (!iio_thrd)
return iio_ptr(-ENOMEM);
iio_thrd->func = thrd;
iio_thrd->d = d;
iio_thrd->thid = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) iio_thrd_wrapper,
d, 0, NULL);
if (!iio_thrd->thid) {
free(iio_thrd);
return iio_ptr(-(int) GetLastError());
}
/* TODO: set name */
//SetThreadDescription(thrd->thid, name);
return iio_thrd;
}
int iio_thrd_join_and_destroy(struct iio_thrd *thrd)
{
DWORD ret = 0;
WaitForSingleObject(thrd->thid, INFINITE);
GetExitCodeThread(thrd->thid, &ret);
free(thrd);
return (int) ret;
}