【ミューテックス管理クラス】
ロック操作を管理する。
【概要】
| 1 | Mutex(void); | 
ミューテックスを生成する。
| 1 | uint8_t lock(int32_t timeout = WAIT_INFINITE); | 
ミューテックスをロックする。成功するとWAIT_OBJECTを返す。ロックを獲得できない場合、マイナスのタイムアウト指定では永久に待ち続け、ゼロ指定ではWAIT_TIMEOUTを即座に返す。1以上の指定時はその指定時間後にWAIT_TIMEOUTを返す。
| 1 | bool unlock(void); | 
ミューテックスをアンロックする。待ち状態にあるタスクがロックを獲得する。
【ソース・コード】
| 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 | /*   mutex.h - Mutex Function Class   Copyright (c) 2021 Sasapea's Lab. All right reserved.   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.   You should have received a copy of the GNU Lesser General   Public License along with this library; if not, write to the   Free Software Foundation, Inc., 59 Temple Place, Suite 330,   Boston, MA  02111-1307  USA */ #ifndef __MUTEX_H #define __MUTEX_H #include <stdint.h> #include <stdbool.h> class Mutex : public Wait {   private:     size_t _count;     TASK_T *_task;   public:     Mutex(void);     ~Mutex(void);     uint8_t lock(int32_t timeout = WAIT_INFINITE);     bool unlock(void); }; #endif | 
| 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 | /*   mutex.h - Mutex Function Class   Copyright (c) 2021 Sasapea's Lab. All right reserved.   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.   You should have received a copy of the GNU Lesser General   Public License along with this library; if not, write to the   Free Software Foundation, Inc., 59 Temple Place, Suite 330,   Boston, MA  02111-1307  USA */ #include <stdint.h> #include <stdbool.h> #include <util/atomic.h> #include "avros.h" Mutex::Mutex(void) : _count(0) , _task(nullptr) { } Mutex::~Mutex(void) { } uint8_t Mutex::lock(int32_t timeout) {   uint8_t rv = WAIT_TIMEOUT;   ATOMIC_BLOCK(ATOMIC_RESTORESTATE)   {     if ((_count == 0) || (_task == Task::current()))     {       ++_count;       _task = Task::current();       rv = WAIT_OBJECT;     }     else if (timeout)     {       rv = Wait::sleep(timeout);       if (rv == WAIT_OBJECT)       {         _task  = Task::current();         _count = 1;       }     }   }   return rv; } bool Mutex::unlock(void) {   bool rv = false;   ATOMIC_BLOCK(ATOMIC_RESTORESTATE)   {     if (_count && (_task == Task::current()))     {       if (--_count == 0)         Wait::wakeup();       rv = true;     }   }   return rv; } | 
【関連する投稿】
理想のRTOSを自作する (1)
理想のRTOSを自作する (2)
理想のRTOSを自作する (3)
理想のRTOSを自作する (4)
理想のRTOSを自作する (5)
理想のRTOSを自作する (6)
理想のRTOSを自作する (7)
理想のRTOSを自作する (8)
理想のRTOSを自作する (9)
理想のRTOSを自作する (10)
理想のRTOSを自作する (11)
