forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.c
90 lines (80 loc) · 1.78 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
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2015 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#include "iio-config.h"
#ifdef _WIN32
#include <windows.h>
#elif !defined(NO_THREADS)
#include <pthread.h>
#endif
#include <stdlib.h>
struct iio_mutex {
#ifdef NO_THREADS
int foo; /* avoid complaints about empty structure */
#else
#ifdef _WIN32
CRITICAL_SECTION lock;
#else
pthread_mutex_t lock;
#endif
#endif
};
struct iio_mutex * iio_mutex_create(void)
{
struct iio_mutex *lock = malloc(sizeof(*lock));
if (!lock)
return NULL;
#ifndef NO_THREADS
#ifdef _WIN32
InitializeCriticalSection(&lock->lock);
#else
pthread_mutex_init(&lock->lock, NULL);
#endif
#endif
return lock;
}
void iio_mutex_destroy(struct iio_mutex *lock)
{
#ifndef NO_THREADS
#ifdef _WIN32
DeleteCriticalSection(&lock->lock);
#else
pthread_mutex_destroy(&lock->lock);
#endif
#endif
free(lock);
}
void iio_mutex_lock(struct iio_mutex *lock)
{
#ifndef NO_THREADS
#ifdef _WIN32
EnterCriticalSection(&lock->lock);
#else
pthread_mutex_lock(&lock->lock);
#endif
#endif
}
void iio_mutex_unlock(struct iio_mutex *lock)
{
#ifndef NO_THREADS
#ifdef _WIN32
LeaveCriticalSection(&lock->lock);
#else
pthread_mutex_unlock(&lock->lock);
#endif
#endif
}