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);
38
#include "CSException.h"
44
* ---------------------------------------------------------------
57
if ((err = pthread_mutex_init(&iMutex, NULL)))
58
CSException::throwOSError(CS_CONTEXT, err);
63
pthread_mutex_destroy(&iMutex);
70
if ((err = pthread_mutex_lock(&iMutex)))
71
CSException::throwOSError(CS_CONTEXT, err);
73
iLocker = CSThread::getSelf();
75
CSL.logf(iLocker, CSLog::Protocol, "Mutex locked\n");
79
void CSMutex::unlock()
83
CSL.logf(iLocker, CSLog::Protocol, "Mutex unlocked\n");
86
pthread_mutex_unlock(&iMutex);
90
* ---------------------------------------------------------------
110
if (iLockingThread != self) {
111
if ((err = pthread_mutex_lock(&iMutex)))
112
CSException::throwOSError(CS_CONTEXT, err);
113
iLockingThread = self;
119
void CSLock::unlock()
122
ASSERT(iLockingThread == self);
123
if (!(--iLockCount)) {
124
iLockingThread = NULL;
125
pthread_mutex_unlock(&iMutex);
130
bool CSLock::haveLock()
133
return_(iLockingThread == self);
137
* ---------------------------------------------------------------
138
* A SYNCRONISATION ENTITY
146
if ((err = pthread_cond_init(&iCondition, NULL)))
147
CSException::throwOSError(CS_CONTEXT, err);
152
pthread_cond_destroy(&iCondition);
161
ASSERT(iLockingThread == self);
162
lock_count = iLockCount;
164
iLockingThread = NULL;
165
err = pthread_cond_wait(&iCondition, &iMutex);
166
iLockCount = lock_count;
167
iLockingThread = self;
169
CSException::throwOSError(CS_CONTEXT, err);
173
void CSSync::wait(time_t milli_sec)
175
struct timespec abstime;
183
/* Get the current time in microseconds: */
184
gettimeofday(&now, NULL);
185
micro_sec = (uint64_t) now.tv_sec * (uint64_t) 1000000 + (uint64_t) now.tv_usec;
187
/* Add the timeout which is in milli seconds */
188
micro_sec += (uint64_t) milli_sec * (uint64_t) 1000;
190
/* Setup the end time, which is in nano-seconds. */
191
abstime.tv_sec = (long) (micro_sec / 1000000); /* seconds */
192
abstime.tv_nsec = (long) ((micro_sec % 1000000) * 1000); /* and nanoseconds */
194
ASSERT(iLockingThread == self);
195
lock_count = iLockCount;
197
iLockingThread = NULL;
198
err = pthread_cond_timedwait(&iCondition, &iMutex, &abstime);
199
iLockCount = lock_count;
200
iLockingThread = self;
201
if (err && err != ETIMEDOUT)
202
CSException::throwOSError(CS_CONTEXT, err);
206
void CSSync::wakeup()
210
if ((err = pthread_cond_broadcast(&iCondition)))
211
CSException::throwOSError(CS_CONTEXT, err);