cheshirekow  v0.1.0
mutex.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of cpp-pthreads.
5  *
6  * cpp-pthreads is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * cpp-pthreads is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with cpp-pthreads. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef CPP_PTHREADS_MUTEX_H_
28 #define CPP_PTHREADS_MUTEX_H_
29 
30 #include <pthread.h>
31 
32 #include <cpp_pthreads/attr.h>
33 
34 namespace pthreads {
35 
36 class Mutex;
37 
39 
53 class ScopedLock {
54  private:
57 
58  public:
60  ScopedLock(Mutex* mutex = 0);
61 
63  ScopedLock(Mutex& mutex);
64 
66  //ScopedLock( ScopedLock&& other );
67 
69  ~ScopedLock();
70 
72 
76 
78  void swap(ScopedLock& other);
79 };
80 
82 class Condition;
83 
85 class Mutex {
86  private:
87  pthread_mutex_t m_data;
88 
89  public:
90  friend class Condition;
91 
93  int init();
94 
96  int init(const Attr<Mutex>& attr);
97 
99  int destroy();
100 
102  int setPriorityCeiling(int newCeil, int* oldCeil);
103 
105  int getPriorityCeiling(int&);
106 
108  int lock();
109 
111  int trylock();
112 
115 
117  int unlock();
118 };
119 
120 }
121 
122 #endif // MUTEX_H_
Points to a locked mutex and unlocks the mutex when destroyed.
Definition: mutex.h:53
int unlock()
unlock the mutex after a successful lock
int getPriorityCeiling(int &)
get the priority ceiling
int setPriorityCeiling(int newCeil, int *oldCeil)
change the priority ceiling
pthread_mutex_t m_data
Definition: mutex.h:87
void swap(ScopedLock &other)
swaps mutexes with another lock
~ScopedLock()
move constructor transfers ownership of the lock to the new object
int trylock()
try to lock the mutex, but dont block
int lock()
lock the mutex, block until succeed
ScopedLock(Mutex *mutex=0)
default constructor has an empty mutex
Mutex * m_mutex
< the mutex being locked
Definition: mutex.h:56
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
int init()
calls pthread_mutex_init
int destroy()
calls pthread_mutex_destroy
Attributes object for type Base.
Definition: attr.h:35
ScopedLock & operator=(ScopedLock &other)
assignment transfers ownership of the lock to the assignee
ScopedLock scopedLock()
return a scoped lock
a mutual exclusion lock
Definition: mutex.h:85
Allows multiple threads to wait until a condition is satisfied.
Definition: condition.h:44