Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 1 | // |
| 2 | // MutexClass.cpp: implementation file |
| 3 | // |
| 4 | // Copyright (C) Walter E. Capers. All rights reserved |
| 5 | // |
| 6 | // This source is free to use as you like. If you make |
| 7 | // any changes please keep me in the loop. Email them to |
| 8 | // walt.capers@comcast.net. |
| 9 | // |
| 10 | // PURPOSE: |
| 11 | // |
| 12 | // To implement mutexes as a C++ object |
| 13 | // |
| 14 | // REVISIONS |
| 15 | // ======================================================= |
| 16 | // Date: 10.25.07 |
| 17 | // Name: Walter E. Capers |
| 18 | // Description: File creation |
| 19 | // |
| 20 | // Date: |
| 21 | // Name: |
| 22 | // Description: |
| 23 | // |
| 24 | // |
| 25 | #include "Thread.h" |
| 26 | |
| 27 | #include <iostream> |
| 28 | using namespace std; |
| 29 | |
| 30 | CMutexClass::CMutexClass(void) |
| 31 | :m_bCreated(TRUE) |
| 32 | { |
| 33 | #ifdef WINDOWS |
| 34 | m_mutex = CreateMutex(NULL,FALSE,NULL); |
| 35 | if( !m_mutex ) m_bCreated = FALSE; |
| 36 | #else |
| 37 | pthread_mutexattr_t mattr; |
| 38 | |
| 39 | pthread_mutexattr_init( &mattr ); |
| 40 | pthread_mutex_init(&m_mutex,&mattr); |
| 41 | |
| 42 | #endif |
| 43 | memset(&m_owner,0,sizeof(ThreadId_t)); |
| 44 | |
| 45 | } |
| 46 | |
| 47 | CMutexClass::~CMutexClass(void) |
| 48 | { |
| 49 | #ifdef WINDOWS |
| 50 | WaitForSingleObject(m_mutex,INFINITE); |
| 51 | CloseHandle(m_mutex); |
| 52 | #else |
| 53 | pthread_mutex_lock(&m_mutex); |
| 54 | pthread_mutex_unlock(&m_mutex); |
| 55 | pthread_mutex_destroy(&m_mutex); |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | * Lock |
| 62 | * the same thread can not lock the same mutex |
| 63 | * more than once |
| 64 | * |
| 65 | **/ |
| 66 | void |
| 67 | CMutexClass::Lock() |
| 68 | { |
| 69 | ThreadId_t id = CThread::ThreadId(); |
| 70 | try { |
| 71 | if(CThread::ThreadIdsEqual(&m_owner,&id) ) |
| 72 | throw "\n\tthe same thread can not acquire a mutex twice!\n"; // the mutex is already locked by this thread |
| 73 | #ifdef WINDOWS |
| 74 | WaitForSingleObject(m_mutex,INFINITE); |
| 75 | #else |
| 76 | pthread_mutex_lock(&m_mutex); |
| 77 | #endif |
| 78 | m_owner = CThread::ThreadId(); |
| 79 | } |
| 80 | catch( char *psz ) |
| 81 | { |
| 82 | #ifdef WINDOWS |
| 83 | MessageBoxA(NULL,&psz[2],"Fatal exception CMutexClass::Lock",MB_ICONHAND); |
| 84 | exit(-1); |
| 85 | #else |
| 86 | cerr << "Fatal exception CMutexClass::Lock : " << psz; |
| 87 | #endif |
| 88 | |
| 89 | |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * |
| 96 | * Unlock |
| 97 | * releases a mutex. only the thread that acquires |
| 98 | * the mutex can release it. |
| 99 | * |
| 100 | **/ |
| 101 | void |
| 102 | CMutexClass::Unlock() |
| 103 | { |
| 104 | ThreadId_t id = CThread::ThreadId(); |
| 105 | try |
| 106 | { |
| 107 | if( ! CThread::ThreadIdsEqual(&id,&m_owner) ) |
| 108 | throw "\n\tonly the thread that acquires a mutex can release it!"; |
| 109 | |
| 110 | memset(&m_owner,0,sizeof(ThreadId_t)); |
| 111 | #ifdef WINDOWS |
| 112 | ReleaseMutex(m_mutex); |
| 113 | #else |
| 114 | pthread_mutex_unlock(&m_mutex); |
| 115 | #endif |
| 116 | } |
| 117 | catch ( char *psz) |
| 118 | { |
| 119 | #ifdef WINDOWS |
| 120 | MessageBoxA(NULL,&psz[2],"Fatal exception CMutexClass::Unlock",MB_ICONHAND); |
| 121 | exit(-1); |
| 122 | #else |
| 123 | cerr << "Fatal exception CMutexClass::Unlock : " << psz; |
| 124 | #endif |
| 125 | |
| 126 | } |
| 127 | } |
| 128 | |