| 74 | |
| 75 | |
| 76 | extern "C" int |
| 77 | DmtcpMutexTryLock(DmtcpMutex *mutex) |
| 78 | { |
| 79 | pid_t owner = 1; |
| 80 | |
| 81 | if (mutex->type != DMTCP_MUTEX_LLL) { |
| 82 | owner = gettid(); |
| 83 | |
| 84 | if ((pid_t)(mutex->owner) == owner) { |
| 85 | if (mutex->type == DMTCP_MUTEX_RECURSIVE) { |
| 86 | JASSERT(mutex->count + 1 != 0); |
| 87 | mutex->count++; |
| 88 | return 0; |
| 89 | } |
| 90 | return EDEADLK; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (__sync_val_compare_and_swap(&mutex->futex, |
| 95 | LOCK_FREE, |
| 96 | LOCK_ACQUIRED) == LOCK_FREE) { |
| 97 | // We successfully acquired the lock. |
| 98 | mutex->owner = (mutex_owner_t)owner; |
| 99 | mutex->count = 1; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | return EAGAIN; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* Set mutex->futex to 0 (LOCK_FREE), releasing the lock. If mutex->futex was |
no test coverage detected