4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
36 #define DEADLOCK_WARNING 10
40 * @memo Create a lockable instance and initialize internal locks
42 Lockable::Lockable() {
43 if (pthread_mutex_init(&mutex
, NULL
)) {
48 * @memo Free up a lockable instance
50 Lockable::~Lockable() {
51 if (pthread_mutex_destroy(&mutex
)) {
56 * @memo Unlock the instance
57 * @precondition This thread must have locked the instance
58 * @postcondition The instance will be unlocked
60 void Lockable::unlock() {
65 * @memo Unlock a given mutex lock
66 * @precondition The lock must be held by this thread
67 * @postcondition The lock will be released
68 * @param myMutex The lock to unlock
70 void Lockable::unlock(pthread_mutex_t
*myMutex
) {
71 pthread_mutex_unlock(myMutex
);
75 * @memo Lock the instance
76 * @postcondition The lock will be held by this thread.
78 void Lockable::lock() {
83 * @memo Lock the given mutex lock
84 * @postcondition The lock will be held by this thread
85 * @param myMutex The mutex lock to take
87 void Lockable::lock(pthread_mutex_t
*myMutex
) {
92 status
= pthread_mutex_trylock(myMutex
);
94 switch (pthread_mutex_trylock(myMutex
)) {
96 cerr
<< "Lock failed: Fault" << endl
;
99 cerr
<< "Lock failed: Invalid" << endl
;
102 if (loop
> DEADLOCK_WARNING
) {
103 cerr
<< "Lock failed: Deadlock" << endl
;
107 cerr
<< "Lock failed: Owner died" << endl
;
110 cerr
<< "Lock failed: Unmapped" << endl
;
112 case ENOTRECOVERABLE
:
113 cerr
<< "Lock failed: not recoverable" << endl
;
115 if (loop
> DEADLOCK_WARNING
) {
116 cerr
<< "Lock failed: " <<strerror(status
) << endl
;
121 break; // Lock taken succesfully