Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: BL_Mutex.h | |
5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
7 | * | |
8 | * The above named program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public | |
10 | * License version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * The above named program is distributed in the hope that it will be | |
13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public | |
18 | * License along with this work; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | * | |
21 | * ========== Copyright Header End ============================================ | |
22 | */ | |
23 | #ifndef __BL_Mutex_h__ | |
24 | #define __BL_Mutex_h__ | |
25 | ||
26 | //#include <thread.h> | |
27 | //#include <synch.h> | |
28 | //#include <errno.h> | |
29 | #include <stdlib.h> | |
30 | #include <stdio.h> | |
31 | #include <pthread.h> | |
32 | ||
33 | class BL_CondVar | |
34 | { | |
35 | public: | |
36 | BL_CondVar() { pthread_cond_init(&cond,NULL); } | |
37 | ~BL_CondVar() { pthread_cond_destroy(&cond); } | |
38 | ||
39 | int signal() { return pthread_cond_signal(&cond); } | |
40 | int broadcast() { return pthread_cond_broadcast(&cond); } | |
41 | ||
42 | private: | |
43 | BL_CondVar( const BL_CondVar& m ) {} | |
44 | ||
45 | friend class BL_Mutex; | |
46 | ||
47 | pthread_cond_t cond; | |
48 | }; | |
49 | ||
50 | ||
51 | class BL_Mutex | |
52 | { | |
53 | public: | |
54 | BL_Mutex() { pthread_mutex_init(&mutex,NULL); } | |
55 | ~BL_Mutex() { pthread_mutex_destroy(&mutex); } | |
56 | BL_Mutex( const BL_Mutex& m ) | |
57 | : mutex(m.mutex) | |
58 | { | |
59 | // this code guarantees that the mutex is unlock when BL_Mutex is | |
60 | // copied | |
61 | if (trylock()) | |
62 | { | |
63 | fprintf(stderr, "BL_Mutex( const BL_Mutex& m ): mutex at %p is held\n", | |
64 | &m.mutex); | |
65 | exit(1); | |
66 | } | |
67 | else | |
68 | unlock(); | |
69 | } | |
70 | ||
71 | int lock() { return pthread_mutex_lock(&mutex); } | |
72 | int unlock() { return pthread_mutex_unlock(&mutex); } | |
73 | int trylock() { return pthread_mutex_trylock(&mutex); } | |
74 | ||
75 | int wait( BL_CondVar& cv ) { return pthread_cond_wait(&cv.cond,&mutex); } | |
76 | ||
77 | protected: | |
78 | pthread_mutex_t mutex; | |
79 | }; | |
80 | ||
81 | ||
82 | // BL_RdWrLock is a wrapper around the solaris pthread_rwlock. | |
83 | // Be carefull when and where you use this as this lock is at | |
84 | // least 4.5 times more expensive as a mutex. So don't use this | |
85 | // to saveguard tlb access for example. Actually avoid using it. | |
86 | ||
87 | class BL_RdWrLock | |
88 | { | |
89 | public: | |
90 | BL_RdWrLock() { pthread_rwlock_init(&rw,NULL); } | |
91 | ~BL_RdWrLock() { pthread_rwlock_destroy(&rw); } | |
92 | ||
93 | int rdlock() { return pthread_rwlock_rdlock(&rw); } | |
94 | int rdunlock() { return pthread_rwlock_unlock(&rw); } | |
95 | int tryrdlock() { return pthread_rwlock_tryrdlock(&rw); } | |
96 | ||
97 | int wrlock() { return pthread_rwlock_wrlock(&rw); } | |
98 | int wrunlock() { return pthread_rwlock_unlock(&rw); } | |
99 | int trywrlock() { return pthread_rwlock_trywrlock(&rw); } | |
100 | ||
101 | private: | |
102 | BL_RdWrLock( const BL_RdWrLock& ) {} | |
103 | ||
104 | pthread_rwlock_t rw; | |
105 | }; | |
106 | ||
107 | #endif |