blob: 31b269871732990f121940a19c19b746dbc4c574 [file] [log] [blame]
Paul Cercueil388dcd62015-11-27 15:15:48 +01001/*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
4 * Copyright (C) 2015 Analog Devices, Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 */
18
19#ifdef _WIN32
20#include <windows.h>
21#else
22#include <pthread.h>
23#endif
24
25#include <stdlib.h>
26
27struct iio_mutex {
28#ifdef _WIN32
29 CRITICAL_SECTION lock;
30#else
31 pthread_mutex_t lock;
32#endif
33};
34
35struct iio_mutex * iio_mutex_create(void)
36{
37 struct iio_mutex *lock = malloc(sizeof(*lock));
38
39 if (!lock)
40 return NULL;
41
42#ifdef _WIN32
43 InitializeCriticalSection(&lock->lock);
44#else
45 pthread_mutex_init(&lock->lock, NULL);
46#endif
47 return lock;
48}
49
50void iio_mutex_destroy(struct iio_mutex *lock)
51{
52#ifdef _WIN32
53 DeleteCriticalSection(&lock->lock);
54#else
55 pthread_mutex_destroy(&lock->lock);
56#endif
57 free(lock);
58}
59
60void iio_mutex_lock(struct iio_mutex *lock)
61{
62#ifdef _WIN32
63 EnterCriticalSection(&lock->lock);
64#else
65 pthread_mutex_lock(&lock->lock);
66#endif
67}
68
69void iio_mutex_unlock(struct iio_mutex *lock)
70{
71#ifdef _WIN32
72 LeaveCriticalSection(&lock->lock);
73#else
74 pthread_mutex_unlock(&lock->lock);
75#endif
76}