| 1 | /* | 
|---|
| 2 | * Legal Notice | 
|---|
| 3 | * | 
|---|
| 4 | * This document and associated source code (the "Work") is a part of a | 
|---|
| 5 | * benchmark specification maintained by the TPC. | 
|---|
| 6 | * | 
|---|
| 7 | * The TPC reserves all right, title, and interest to the Work as provided | 
|---|
| 8 | * under U.S. and international laws, including without limitation all patent | 
|---|
| 9 | * and trademark rights therein. | 
|---|
| 10 | * | 
|---|
| 11 | * No Warranty | 
|---|
| 12 | * | 
|---|
| 13 | * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION | 
|---|
| 14 | *     CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE | 
|---|
| 15 | *     AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER | 
|---|
| 16 | *     WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, | 
|---|
| 17 | *     INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES, | 
|---|
| 18 | *     DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR | 
|---|
| 19 | *     PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF | 
|---|
| 20 | *     WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE. | 
|---|
| 21 | *     ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, | 
|---|
| 22 | *     QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT | 
|---|
| 23 | *     WITH REGARD TO THE WORK. | 
|---|
| 24 | * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO | 
|---|
| 25 | *     ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE | 
|---|
| 26 | *     COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS | 
|---|
| 27 | *     OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, | 
|---|
| 28 | *     INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY, | 
|---|
| 29 | *     OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT | 
|---|
| 30 | *     RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD | 
|---|
| 31 | *     ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. | 
|---|
| 32 | * | 
|---|
| 33 | * Contributors | 
|---|
| 34 | * - Christopher Chan-Nui, Matt Emmerton | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #include "utilities/locking.h" | 
|---|
| 38 |  | 
|---|
| 39 | #include <cstring> | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef WIN32 | 
|---|
| 42 | #include <iostream> | 
|---|
| 43 | #include <sstream> | 
|---|
| 44 | #include <stdexcept> | 
|---|
| 45 | #include <cerrno> | 
|---|
| 46 | #include <cstdlib> | 
|---|
| 47 | #include <sys/time.h> | 
|---|
| 48 |  | 
|---|
| 49 | using std::exit; | 
|---|
| 50 | using std::strerror; | 
|---|
| 51 | #endif | 
|---|
| 52 |  | 
|---|
| 53 | #include "utilities/error.h" | 
|---|
| 54 |  | 
|---|
| 55 | namespace TPCE { | 
|---|
| 56 |  | 
|---|
| 57 | #ifdef WIN32 | 
|---|
| 58 |  | 
|---|
| 59 | ////////////////////////////////////////////////////////// | 
|---|
| 60 | // Windows Implementation | 
|---|
| 61 | ////////////////////////////////////////////////////////// | 
|---|
| 62 |  | 
|---|
| 63 | CMutex::CMutex() : mutex_() { | 
|---|
| 64 | InitializeCriticalSection(&mutex_); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | CMutex::~CMutex() { | 
|---|
| 68 | DeleteCriticalSection(&mutex_); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | LPCRITICAL_SECTION CMutex::mutex() { | 
|---|
| 72 | return &mutex_; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | void CMutex::lock() { | 
|---|
| 76 | EnterCriticalSection(&mutex_); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void CMutex::unlock() { | 
|---|
| 80 | LeaveCriticalSection(&mutex_); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | #else | 
|---|
| 84 |  | 
|---|
| 85 | ////////////////////////////////////////////////////////// | 
|---|
| 86 | // Non-Windows Implementation (pthreads) | 
|---|
| 87 | ////////////////////////////////////////////////////////// | 
|---|
| 88 |  | 
|---|
| 89 | CMutex::CMutex() : mutex_() { | 
|---|
| 90 | int rc = pthread_mutex_init(&mutex_, NULL); | 
|---|
| 91 | if (rc != 0) { | 
|---|
| 92 | std::ostringstream strm; | 
|---|
| 93 | strm << "pthread_mutex_init error: "<< strerror(rc) << "("<< rc << ")"; | 
|---|
| 94 | throw std::runtime_error(strm.str()); | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | CMutex::~CMutex() { | 
|---|
| 99 | int rc = pthread_mutex_destroy(&mutex_); | 
|---|
| 100 | if (rc != 0) { | 
|---|
| 101 | std::ostringstream strm; | 
|---|
| 102 | strm << "pthread_mutex_destroy error: "<< strerror(rc) << "("<< rc << ")"; | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | pthread_mutex_t *CMutex::mutex() { | 
|---|
| 107 | return &mutex_; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void CMutex::lock() { | 
|---|
| 111 | int rc = pthread_mutex_lock(&mutex_); | 
|---|
| 112 | if (rc != 0) { | 
|---|
| 113 | std::ostringstream strm; | 
|---|
| 114 | strm << "pthread_cond_wait error: "<< strerror(rc) << "("<< rc << ")"; | 
|---|
| 115 | throw std::runtime_error(strm.str()); | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | void CMutex::unlock() { | 
|---|
| 120 | int rc = pthread_mutex_unlock(&mutex_); | 
|---|
| 121 | if (rc != 0) { | 
|---|
| 122 | std::ostringstream strm; | 
|---|
| 123 | strm << "pthread_cond_wait error: "<< strerror(rc) << "("<< rc << ")"; | 
|---|
| 124 | throw std::runtime_error(strm.str()); | 
|---|
| 125 | } | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | #endif | 
|---|
| 129 |  | 
|---|
| 130 | } // namespace TPCE | 
|---|
| 131 |  | 
|---|