Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 1 | /* Copyright 2015 Pierre Ossman for Cendio AB |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | #ifndef __OS_MUTEX_H__ |
| 20 | #define __OS_MUTEX_H__ |
| 21 | |
| 22 | namespace os { |
| 23 | class Condition; |
| 24 | |
| 25 | class Mutex { |
| 26 | public: |
| 27 | Mutex(); |
| 28 | ~Mutex(); |
| 29 | |
| 30 | void lock(); |
| 31 | void unlock(); |
| 32 | |
| 33 | private: |
Pierre Ossman | 769931e | 2015-12-08 15:27:24 +0100 | [diff] [blame] | 34 | friend class Condition; |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 35 | |
| 36 | void* systemMutex; |
| 37 | }; |
| 38 | |
| 39 | class AutoMutex { |
| 40 | public: |
| 41 | AutoMutex(Mutex* mutex) { m = mutex; m->lock(); } |
| 42 | ~AutoMutex() { m->unlock(); } |
| 43 | private: |
| 44 | Mutex* m; |
| 45 | }; |
| 46 | |
| 47 | class Condition { |
| 48 | public: |
| 49 | Condition(Mutex* mutex); |
| 50 | ~Condition(); |
| 51 | |
| 52 | void wait(); |
| 53 | |
| 54 | void signal(); |
| 55 | void broadcast(); |
| 56 | |
| 57 | private: |
| 58 | Mutex* mutex; |
| 59 | void* systemCondition; |
| 60 | }; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | #endif |