-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcclock.cpp
84 lines (72 loc) · 2.48 KB
/
cclock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <pthread.h>
#include <assert_only.h>
#include <cclock.hpp>
#include <errno.h>
// do not include PAL
void CCLock::Reset(bool shouldTrackThreadId)
{
Assert(sizeof(pthread_mutex_t) <= sizeof(this->mutexPtr));
if (*((size_t*)mutexPtr) != 0)
{
return; // already initialized
}
int err;
pthread_mutexattr_t mtconf;
if (shouldTrackThreadId)
{
err = pthread_mutexattr_init(&mtconf); Assert(err == 0);
err = pthread_mutexattr_settype(&mtconf, PTHREAD_MUTEX_RECURSIVE); Assert(err == 0);
}
pthread_mutex_t *mutex = (pthread_mutex_t*)this->mutexPtr;
err = pthread_mutex_init(mutex, shouldTrackThreadId ? &mtconf : NULL); Assert(err == 0);
if (shouldTrackThreadId)
{
err = pthread_mutexattr_destroy(&mtconf); Assert(err == 0);
}
}
CCLock::~CCLock()
{
pthread_mutex_t *mutex = (pthread_mutex_t*)this->mutexPtr;
pthread_mutex_destroy(mutex);
*((size_t*)mutexPtr) = 0;
}
void CCLock::Enter()
{
pthread_mutex_t *mutex = (pthread_mutex_t*)this->mutexPtr;
int err = pthread_mutex_lock(mutex);
AssertMsg(err == 0 || *((size_t*)mutexPtr) == 0, "Mutex Enter has failed");
}
void CCLock::Leave()
{
pthread_mutex_t *mutex = (pthread_mutex_t*)this->mutexPtr;
int err = pthread_mutex_unlock(mutex);
AssertMsg(err == 0 || *((size_t*)mutexPtr) == 0, "Mutex Leave has failed");
}
bool CCLock::TryEnter()
{
pthread_mutex_t *mutex = (pthread_mutex_t*)this->mutexPtr;
int err = pthread_mutex_trylock(mutex);
AssertMsg(err == 0 || err == EBUSY || *((size_t*)mutexPtr) == 0, "Mutex TryEnter has failed");
if (err != EBUSY)
{
return true;
}
return false;
}
bool CCLock::IsLocked() const
{
#ifndef DEBUG
fprintf(stderr, "you shouldn't need this for release build, It's expensive!\n");
abort();
#endif
return true; // there is no good way to do this, return true for CC Assert stuff.
// if you need to know whether it is locked or not, you may try
// TryEnter -> Leave couple. If it's not locked, TryEnter should succeed
}