AJA NTV2 SDK  17.0.1.1246
NTV2 SDK 17.0.1.1246
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 #if 0
26  bool okSoFar = true;
27  bool freeAttr = false;
28 
29  // Set up the thread attributes
30  pthread_mutexattr_t attr;
31  int rc = pthread_mutexattr_init(&attr);
32  if (rc)
33  {
34  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl(%s) attr init reported error %d", mName, rc);
35  okSoFar = false;
36  }
37  freeAttr = true;
38 
39  if (okSoFar)
40  {
41  rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
42  if (rc)
43  {
44  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl(%s) attr settype reported error %d", mName, rc);
45  okSoFar = false;
46  }
47  }
48 
49  if (okSoFar)
50  {
51  rc = pthread_mutex_init(&mMutex, &attr);
52  if (rc)
53  {
54  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl(%s) mutex init reported error %d", mName, rc);
55  okSoFar = false;
56  }
57  }
58 
59  // Clean up the attribute memory
60  if (freeAttr)
61  {
62  rc = pthread_mutexattr_destroy(&attr);
63  if (rc)
64  {
65  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl(%s) attr destroy reported error %d", mName, rc);
66  }
67  }
68 #endif
69 }
70 
71 
73 {
74 #if 0
75  int rc;
76 
77  rc = pthread_mutex_destroy(&mMutex);
78  if (rc)
79  {
80  AJA_REPORT(0, AJA_DebugSeverity_Error, "~AJALockImpl(%s) mutex destroy reported error %d", mName, rc);
81  }
82 #endif
83 }
84 
85 
87 AJALockImpl::Lock(uint32_t timeout)
88 {
89 #if 0
90  int rc;
91 
92  // Allow multiple locks by the same thread
93  if (mOwner && (mOwner == pthread_self()))
94  {
95  mRefCount++;
96  return AJA_STATUS_SUCCESS;
97  }
98 
99  // get the current time of day
100  struct timespec ts;
102 
103  // calculate how long to wait
104  if (timeout == 0xffffffff)
105  {
106  ts.tv_sec += 60 * 60 * 24 * 365; // A year is infinite enough
107  ts.tv_nsec = 0;
108  }
109  else
110  {
111  uint64_t bigtimeout = (uint64_t) timeout * MIL_2_NSEC;
112  ts.tv_sec += bigtimeout / MAX_NSEC;
113  ts.tv_nsec += bigtimeout % MAX_NSEC;
114  if (ts.tv_nsec >= MAX_NSEC)
115  {
116  ts.tv_sec++;
117  ts.tv_nsec -= MAX_NSEC;
118  }
119  }
120 
121  // Wait for the lock
122  rc = pthread_mutex_timedlock(&mMutex, &ts);
123  if (rc)
124  {
125  if (rc == ETIMEDOUT)
126  return AJA_STATUS_TIMEOUT;
127 
128  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJALockImpl::Lock(%s) mutex lock reported error %d", mName, rc);
129  return AJA_STATUS_FAIL;
130  }
131 
132  mOwner = pthread_self();
133  mRefCount = 1;
134 
135  return AJA_STATUS_SUCCESS;
136 #else
137  return AJA_STATUS_FAIL;
138 #endif
139 }
140 
141 
142 AJAStatus
144 {
145 #if 0
146  if (mOwner != pthread_self())
147  {
148  return AJA_STATUS_FAIL;
149  }
150 
151  // Allow multiple unlocks by the same thread
152  mRefCount--;
153  if (mRefCount)
154  {
155  return AJA_STATUS_SUCCESS;
156  }
157 
158  mOwner = 0;
159  mRefCount = 0;
160  pthread_mutex_unlock(&mMutex);
161 
162  return AJA_STATUS_SUCCESS;
163 #else
164  return AJA_STATUS_FAIL;
165 #endif
166 }
167 
AJALockImpl::~AJALockImpl
virtual ~AJALockImpl()
Definition: lockimpl.cpp:72
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:368
AJALockImpl::Unlock
AJAStatus Unlock()
Definition: lockimpl.cpp:143
AJA_DebugSeverity_Error
@ AJA_DebugSeverity_Error
Definition: debugshare.h:28
AJAStatus
AJAStatus
Definition: types.h:365
CLOCK_REALTIME
#define CLOCK_REALTIME
Definition: pthreadsextra.h:21
AJA_STATUS_FAIL
@ AJA_STATUS_FAIL
Definition: types.h:369
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:371
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
MAX_NSEC
static const int MAX_NSEC
Definition: lockimpl.cpp:16
MIL_2_NSEC
static const int MIL_2_NSEC
Definition: lockimpl.cpp:13
debug.h
Declares the AJADebug class.
lockimpl.h