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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
24
* A basic mutex (mutual exclusion) object.
33
#include "CSException.h"
38
* ---------------------------------------------------------------
46
if ((err = pthread_mutex_init(&iMutex, NULL)))
47
CSException::throwOSError(CS_CONTEXT, err);
52
pthread_mutex_destroy(&iMutex);
59
if ((err = pthread_mutex_lock(&iMutex)))
60
CSException::throwOSError(CS_CONTEXT, err);
63
void CSMutex::unlock()
65
pthread_mutex_unlock(&iMutex);
69
* ---------------------------------------------------------------
89
if (iLockingThread != self) {
90
if ((err = pthread_mutex_lock(&iMutex)))
91
CSException::throwOSError(CS_CONTEXT, err);
92
iLockingThread = self;
101
ASSERT(iLockingThread == self);
102
if (!(--iLockCount)) {
103
iLockingThread = NULL;
104
pthread_mutex_unlock(&iMutex);
109
bool CSLock::haveLock()
112
return_(iLockingThread == self);
116
* ---------------------------------------------------------------
117
* A SYNCRONISATION ENTITY
125
if ((err = pthread_cond_init(&iCondition, NULL)))
126
CSException::throwOSError(CS_CONTEXT, err);
131
pthread_cond_destroy(&iCondition);
140
ASSERT(iLockingThread == self);
141
lock_count = iLockCount;
143
iLockingThread = NULL;
144
err = pthread_cond_wait(&iCondition, &iMutex);
145
iLockCount = lock_count;
146
iLockingThread = self;
148
CSException::throwOSError(CS_CONTEXT, err);
152
void CSSync::wait(time_t milli_sec)
154
struct timespec abstime;
162
GetSystemTimeAsFileTime(&now.ft);
164
/* System time is measured in 100ns units.
165
* This calculation will be reversed by the Windows implementation
166
* of pthread_cond_timedwait(), in order to extract the
167
* milli-second timeout!
169
abstime.tv.i64 = now.i64 + (milli_sec * 10000);
171
abstime.max_timeout_msec = milli_sec;
176
/* Get the current time in microseconds: */
177
gettimeofday(&now, NULL);
178
micro_sec = (uint64_t) now.tv_sec * (uint64_t) 1000000 + (uint64_t) now.tv_usec;
180
/* Add the timeout which is in milli seconds */
181
micro_sec += (uint64_t) milli_sec * (uint64_t) 1000;
183
/* Setup the end time, which is in nano-seconds. */
184
abstime.tv_sec = (long) (micro_sec / 1000000); /* seconds */
185
abstime.tv_nsec = (long) ((micro_sec % 1000000) * 1000); /* and nanoseconds */
187
ASSERT(iLockingThread == self);
188
lock_count = iLockCount;
190
iLockingThread = NULL;
191
err = pthread_cond_timedwait(&iCondition, &iMutex, &abstime);
192
iLockCount = lock_count;
193
iLockingThread = self;
194
if (err && err != ETIMEDOUT)
195
CSException::throwOSError(CS_CONTEXT, err);
199
void CSSync::wakeup()
203
if ((err = pthread_cond_broadcast(&iCondition)))
204
CSException::throwOSError(CS_CONTEXT, err);