Developer World
Spresense Arduino Library v3.2.0-77d75a4
MPMutex.h
Go to the documentation of this file.
1/*
2 * MPMutex.h - Spresense Arduino Multi-Processer Mutex library
3 * Copyright 2019 Sony Semiconductor Solutions Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef _MPMUTEX_H_
21#define _MPMUTEX_H_
22
37/****************************************************************************
38 * Included Files
39 ****************************************************************************/
40
41#include <Arduino.h>
42#include <sdk/config.h>
43#include <stdio.h>
44#include <string.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <sys/ioctl.h>
48
49#include <cxd56_sph.h>
50
51/****************************************************************************
52 * Pre-processor Definitions
53 ****************************************************************************/
54
55#define MP_MUTEX_ID0 "/dev/hsem14"
56#define MP_MUTEX_ID1 "/dev/hsem13"
57#define MP_MUTEX_ID2 "/dev/hsem12"
58#define MP_MUTEX_ID3 "/dev/hsem11"
59#define MP_MUTEX_ID4 "/dev/hsem10"
60#define MP_MUTEX_ID5 "/dev/hsem9"
61#define MP_MUTEX_ID6 "/dev/hsem8"
62#define MP_MUTEX_ID7 "/dev/hsem7"
63#define MP_MUTEX_ID8 "/dev/hsem6"
64#define MP_MUTEX_ID9 "/dev/hsem5"
65#define MP_MUTEX_ID10 "/dev/hsem4"
66
67/****************************************************************************
68 * class declaration
69 ****************************************************************************/
70
77{
78public:
79 MPMutex(const char *devname) : _fd(-1) {
80 strncpy(_devname, devname, sizeof(_devname));
81 };
82 ~MPMutex() {
83 if (_fd >= 0) {
84 close(_fd);
85 }
86 }
87#if 0 /* not support yet */
88 int Lock() {
89 if (_create()) { return -1; }
90 return ioctl(_fd, HSLOCK, 0);
91 };
92#endif
93 int Trylock() {
94 if (_create()) { return -1; }
95 return ioctl(_fd, HSTRYLOCK, 0);
96 };
97 int Unlock() {
98 if (_create()) { return -1; }
99 return ioctl(_fd, HSUNLOCK, 0);
100 };
101
102private:
103 int _fd;
104 char _devname[16];
105 int _create() {
106 if (_fd < 0) {
107 _fd = open(_devname, 0);
108 return (_fd < 0) ? -1 : 0;
109 }
110 return 0;
111 }
112};
113
116#endif /* _MPMUTEX_H_ */
This is the interface for MP Mutex.
Definition: MPMutex.h:77