AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
lockimpl.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
9 #include "ajabase/system/debug.h"
10 #include <errno.h>
11 
12 // For converting milliseconds to nanoseconds
13 static const int MIL_2_NSEC = 1000000;
14 
15 // Number of nanoseconds in a second
16 static const int MAX_NSEC = 1000000000;
17 
18 // class AJALockImpl
19 
20 AJALockImpl::AJALockImpl(const char* pName) :
21  mName(pName),
22  mOwner(0),
23  mRefCount(0)
24 {
25  bool okSoFar = true;
26  bool freeAttr = false;
27 
28  // Set up the thread attributes
29  pthread_mutexattr_t attr;
30  int rc = pthread_mutexattr_init(&attr);
31  if (rc)
32  {
33  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl(%s) attr init reported error %d", mName, rc);
34  okSoFar = false;
35  }
36  freeAttr = true;
37 
38  if (okSoFar)
39  {
40  rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
41  if (rc)
42  {
43  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl(%s) attr settype reported error %d", mName, rc);
44  okSoFar = false;
45  }
46  }
47 
48  if (okSoFar)
49  {
50  rc = pthread_mutex_init(&mMutex, &attr);
51  if (rc)
52  {
53  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl(%s) mutex init reported error %d", mName, rc);
54  okSoFar = false;
55  }
56  }
57 
58  // Clean up the attribute memory
59  if (freeAttr)
60  {
61  rc = pthread_mutexattr_destroy(&attr);
62  if (rc)
63  {
64  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl(%s) attr destroy reported error %d", mName, rc);
65  }
66  }
67 }
68 
69 
71 {
72  int rc;
73 
74  rc = pthread_mutex_destroy(&mMutex);
75  if (rc)
76  {
77  AJA_REPORT(0, AJA_DebugSeverity_Error, "~AJALockImpl(%s) mutex destroy reported error %d", mName, rc);
78  }
79 }
80 
81 
83 AJALockImpl::Lock(uint32_t timeout)
84 {
85  int rc;
86 
87  // Allow multiple locks by the same thread
88  if (mOwner && (mOwner == pthread_self()))
89  {
90  mRefCount++;
91  return AJA_STATUS_SUCCESS;
92  }
93 
94  // get the current time of day
95  struct timespec ts;
97 
98  // calculate how long to wait
99  if (timeout == 0xffffffff)
100  {
101  ts.tv_sec += 60 * 60 * 24 * 365; // A year is infinite enough
102  ts.tv_nsec = 0;
103  }
104  else
105  {
106  uint64_t bigtimeout = (uint64_t) timeout * MIL_2_NSEC;
107  ts.tv_sec += bigtimeout / MAX_NSEC;
108  ts.tv_nsec += bigtimeout % MAX_NSEC;
109  if (ts.tv_nsec >= MAX_NSEC)
110  {
111  ts.tv_sec++;
112  ts.tv_nsec -= MAX_NSEC;
113  }
114  }
115 
116  // Wait for the lock
117  rc = pthread_mutex_timedlock(&mMutex, &ts);
118  if (rc)
119  {
120  if (rc == ETIMEDOUT)
121  return AJA_STATUS_TIMEOUT;
122 
123  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl::Lock(%s) mutex lock reported error %d", mName, rc);
124  return AJA_STATUS_FAIL;
125  }
126 
127  mOwner = pthread_self();
128  mRefCount = 1;
129 
130  return AJA_STATUS_SUCCESS;
131 }
132 
133 
134 AJAStatus
136 {
137  if (mOwner != pthread_self())
138  {
139  return AJA_STATUS_FAIL;
140  }
141 
142  // Allow multiple unlocks by the same thread
143  mRefCount--;
144  if (mRefCount)
145  {
146  return AJA_STATUS_SUCCESS;
147  }
148 
149  mOwner = 0;
150  mRefCount = 0;
151  pthread_mutex_unlock(&mMutex);
152 
153  return AJA_STATUS_SUCCESS;
154 }
155 
lockimpl.h
Declares the AJALockImpl class.
AJALockImpl::~AJALockImpl
virtual ~AJALockImpl()
Definition: lockimpl.cpp:72
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:381
AJALockImpl::Unlock
AJAStatus Unlock()
Definition: lockimpl.cpp:143
AJA_DebugSeverity_Error
@ AJA_DebugSeverity_Error
Definition: debugshare.h:28
AJAStatus
AJAStatus
Definition: types.h:378
CLOCK_REALTIME
#define CLOCK_REALTIME
Definition: pthreadsextra.h:21
AJA_STATUS_FAIL
@ AJA_STATUS_FAIL
Definition: types.h:382
MIL_2_NSEC
static const int MIL_2_NSEC
Definition: lockimpl.cpp:13
AJA_REPORT
#define AJA_REPORT(_index_, _severity_, _format_,...)
Definition: debug.h:117
AJALockImpl::Lock
AJAStatus Lock(uint32_t uTimeout=0xffffffff)
Definition: lockimpl.cpp:87
AJA_STATUS_TIMEOUT
@ AJA_STATUS_TIMEOUT
Definition: types.h:384
MAX_NSEC
static const int MAX_NSEC
Definition: lockimpl.cpp:16
AJALockImpl::AJALockImpl
AJALockImpl(const char *pName)
Definition: lockimpl.cpp:20
clock_gettime
int clock_gettime(clockid_t clk_id, struct timespec *tp)
Definition: pthreadsextra.cpp:12
pthread_mutex_timedlock
int pthread_mutex_timedlock(pthread_mutex_t *__restrict mutex, const struct timespec *__restrict abs_timeout)
Definition: pthreadsextra.cpp:43
debug.h
Declares the AJADebug class.