blob: cabb2140ae2462499732554901b9fd09a7259c71 [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
Paul Cercueil2814ed12016-08-25 17:08:18 +020019#include "iio-config.h"
20
Paul Cercueil388dcd62015-11-27 15:15:48 +010021#ifdef _WIN32
22#include <windows.h>
Paul Cercueildf6a2f32016-07-05 12:12:41 +020023#elif !defined(NO_THREADS)
Paul Cercueil388dcd62015-11-27 15:15:48 +010024#include <pthread.h>
25#endif
26
27#include <stdlib.h>
28
29struct iio_mutex {
Paul Cercueil2814ed12016-08-25 17:08:18 +020030#ifdef NO_THREADS
Paul Cercueil52c50a52016-04-28 17:18:59 +020031 int foo; /* avoid complaints about empty structure */
32#else
Paul Cercueil388dcd62015-11-27 15:15:48 +010033#ifdef _WIN32
34 CRITICAL_SECTION lock;
35#else
36 pthread_mutex_t lock;
37#endif
Paul Cercueil52c50a52016-04-28 17:18:59 +020038#endif
Paul Cercueil388dcd62015-11-27 15:15:48 +010039};
40
41struct iio_mutex * iio_mutex_create(void)
42{
43 struct iio_mutex *lock = malloc(sizeof(*lock));
44
45 if (!lock)
46 return NULL;
47
Paul Cercueil2814ed12016-08-25 17:08:18 +020048#ifndef NO_THREADS
Paul Cercueil388dcd62015-11-27 15:15:48 +010049#ifdef _WIN32
50 InitializeCriticalSection(&lock->lock);
51#else
52 pthread_mutex_init(&lock->lock, NULL);
53#endif
Paul Cercueil52c50a52016-04-28 17:18:59 +020054#endif
Paul Cercueil388dcd62015-11-27 15:15:48 +010055 return lock;
56}
57
58void iio_mutex_destroy(struct iio_mutex *lock)
59{
Paul Cercueil2814ed12016-08-25 17:08:18 +020060#ifndef NO_THREADS
Paul Cercueil388dcd62015-11-27 15:15:48 +010061#ifdef _WIN32
62 DeleteCriticalSection(&lock->lock);
63#else
64 pthread_mutex_destroy(&lock->lock);
65#endif
Paul Cercueil52c50a52016-04-28 17:18:59 +020066#endif
Paul Cercueil388dcd62015-11-27 15:15:48 +010067 free(lock);
68}
69
70void iio_mutex_lock(struct iio_mutex *lock)
71{
Paul Cercueil2814ed12016-08-25 17:08:18 +020072#ifndef NO_THREADS
Paul Cercueil388dcd62015-11-27 15:15:48 +010073#ifdef _WIN32
74 EnterCriticalSection(&lock->lock);
75#else
76 pthread_mutex_lock(&lock->lock);
77#endif
Paul Cercueil52c50a52016-04-28 17:18:59 +020078#endif
Paul Cercueil388dcd62015-11-27 15:15:48 +010079}
80
81void iio_mutex_unlock(struct iio_mutex *lock)
82{
Paul Cercueil2814ed12016-08-25 17:08:18 +020083#ifndef NO_THREADS
Paul Cercueil388dcd62015-11-27 15:15:48 +010084#ifdef _WIN32
85 LeaveCriticalSection(&lock->lock);
86#else
87 pthread_mutex_unlock(&lock->lock);
88#endif
Paul Cercueil52c50a52016-04-28 17:18:59 +020089#endif
Paul Cercueil388dcd62015-11-27 15:15:48 +010090}