1
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
24
* A basic mutex (mutual exclusion) object.
32
extern int gettimeofday(struct timeval *tv, struct timezone *tz);
39
#include "CSException.h"
45
* ---------------------------------------------------------------
58
if ((err = pthread_mutex_init(&iMutex, NULL)))
59
CSException::throwOSError(CS_CONTEXT, err);
64
pthread_mutex_destroy(&iMutex);
73
while (((err = pthread_mutex_trylock(&iMutex)) == EBUSY) && (waiting > 0)) {
79
CSL.logf(iLocker, CSLog::Protocol, "Thread holding lock.\n");
82
if ((err) || (err = pthread_mutex_lock(&iMutex)))
83
CSException::throwOSError(CS_CONTEXT, err);
86
iLocker = CSThread::getSelf();
88
CSL.logf(iLocker, CSLog::Protocol, "Mutex locked\n");
90
if ((err = pthread_mutex_lock(&iMutex)))
91
CSException::throwOSError(CS_CONTEXT, err);
95
void CSMutex::unlock()
99
CSL.logf(iLocker, CSLog::Protocol, "Mutex unlocked\n");
102
pthread_mutex_unlock(&iMutex);
106
* ---------------------------------------------------------------
112
iLockingThread(NULL),
126
if (iLockingThread != self) {
127
if ((err = pthread_mutex_lock(&iMutex)))
128
CSException::throwOSError(CS_CONTEXT, err);
129
iLockingThread = self;
135
void CSLock::unlock()
138
ASSERT(iLockingThread == self);
139
if (!(--iLockCount)) {
140
iLockingThread = NULL;
141
pthread_mutex_unlock(&iMutex);
146
bool CSLock::haveLock()
149
return_(iLockingThread == self);
153
* ---------------------------------------------------------------
154
* A SYNCRONISATION ENTITY
162
if ((err = pthread_cond_init(&iCondition, NULL)))
163
CSException::throwOSError(CS_CONTEXT, err);
168
pthread_cond_destroy(&iCondition);
177
ASSERT(iLockingThread == self);
178
lock_count = iLockCount;
180
iLockingThread = NULL;
181
err = pthread_cond_wait(&iCondition, &iMutex);
182
iLockCount = lock_count;
183
iLockingThread = self;
185
CSException::throwOSError(CS_CONTEXT, err);
189
void CSSync::wait(time_t milli_sec)
191
struct timespec abstime;
199
/* Get the current time in microseconds: */
200
gettimeofday(&now, NULL);
201
micro_sec = (uint64_t) now.tv_sec * (uint64_t) 1000000 + (uint64_t) now.tv_usec;
203
/* Add the timeout which is in milli seconds */
204
micro_sec += (uint64_t) milli_sec * (uint64_t) 1000;
206
/* Setup the end time, which is in nano-seconds. */
207
abstime.tv_sec = (long) (micro_sec / 1000000); /* seconds */
208
abstime.tv_nsec = (long) ((micro_sec % 1000000) * 1000); /* and nanoseconds */
210
ASSERT(iLockingThread == self);
211
lock_count = iLockCount;
213
iLockingThread = NULL;
214
err = pthread_cond_timedwait(&iCondition, &iMutex, &abstime);
215
iLockCount = lock_count;
216
iLockingThread = self;
217
if (err && err != ETIMEDOUT)
218
CSException::throwOSError(CS_CONTEXT, err);
222
void CSSync::wakeup()
226
if ((err = pthread_cond_broadcast(&iCondition)))
227
CSException::throwOSError(CS_CONTEXT, err);