Jack2  1.9.10
JackPosixProcessSync.cpp
00001  /*
00002 Copyright (C) 2004-2008 Grame
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU Lesser General Public License as published by
00006 the Free Software Foundation; either version 2.1 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #include "JackPosixProcessSync.h"
00021 #include "JackError.h"
00022 
00023 namespace Jack
00024 {
00025 
00026 void JackPosixProcessSync::Signal()
00027 {
00028     int res = pthread_cond_signal(&fCond);
00029     if (res != 0) {
00030         jack_error("JackPosixProcessSync::Signal error err = %s", strerror(res));
00031     }
00032 }
00033 
00034 // TO DO : check thread consistency?
00035 void JackPosixProcessSync::LockedSignal()
00036 {
00037     int res = pthread_mutex_lock(&fMutex);
00038     if (res != 0) {
00039         jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
00040     }
00041     res = pthread_cond_signal(&fCond);
00042     if (res != 0) {
00043         jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
00044     }
00045     res = pthread_mutex_unlock(&fMutex);
00046     if (res != 0) {
00047         jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
00048     }
00049 }
00050 
00051 void JackPosixProcessSync::SignalAll()
00052 {
00053     int res = pthread_cond_broadcast(&fCond);
00054     if (res != 0) {
00055         jack_error("JackPosixProcessSync::SignalAll error err = %s", strerror(res));
00056     }
00057 }
00058 
00059 // TO DO : check thread consistency?
00060 void JackPosixProcessSync::LockedSignalAll()
00061 {
00062     int res = pthread_mutex_lock(&fMutex);
00063     if (res != 0) {
00064         jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
00065     }
00066     res = pthread_cond_broadcast(&fCond);
00067     if (res != 0) {
00068         jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
00069     }
00070     res = pthread_mutex_unlock(&fMutex);
00071     if (res != 0) {
00072         jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
00073     }
00074 }
00075 
00076 void JackPosixProcessSync::Wait()
00077 {
00078     ThrowIf(!pthread_equal(pthread_self(), fOwner), JackException("JackPosixProcessSync::Wait: a thread has to have locked a mutex before it can wait"));
00079     fOwner = 0;
00080 
00081     int res = pthread_cond_wait(&fCond, &fMutex);
00082     if (res != 0) {
00083         jack_error("JackPosixProcessSync::Wait error err = %s", strerror(res));
00084     } else {
00085         fOwner = pthread_self();
00086     }
00087 }
00088 
00089 // TO DO : check thread consistency?
00090 void JackPosixProcessSync::LockedWait()
00091 {
00092     int res;
00093     res = pthread_mutex_lock(&fMutex);
00094     if (res != 0) {
00095         jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
00096     }
00097     if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0) {
00098         jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
00099     }
00100     res = pthread_mutex_unlock(&fMutex);
00101     if (res != 0) {
00102         jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
00103     }
00104 }
00105 
00106 bool JackPosixProcessSync::TimedWait(long usec)
00107 {
00108     ThrowIf(!pthread_equal(pthread_self(), fOwner), JackException("JackPosixProcessSync::TimedWait: a thread has to have locked a mutex before it can wait"));
00109     fOwner = 0;
00110 
00111     struct timeval T0, T1;
00112     timespec time;
00113     struct timeval now;
00114     int res;
00115 
00116     jack_log("JackPosixProcessSync::TimedWait time out = %ld", usec);
00117     gettimeofday(&T0, 0);
00118 
00119     gettimeofday(&now, 0);
00120     unsigned int next_date_usec = now.tv_usec + usec;
00121     time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
00122     time.tv_nsec = (next_date_usec % 1000000) * 1000;
00123 
00124     res = pthread_cond_timedwait(&fCond, &fMutex, &time);
00125     if (res != 0) {
00126         jack_error("JackPosixProcessSync::TimedWait error usec = %ld err = %s", usec, strerror(res));
00127     } else {
00128         fOwner = pthread_self();
00129     }
00130 
00131     gettimeofday(&T1, 0);
00132     jack_log("JackPosixProcessSync::TimedWait finished delta = %5.1lf",
00133              (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
00134 
00135     return (res == 0);
00136 }
00137 
00138 // TO DO : check thread consistency?
00139 bool JackPosixProcessSync::LockedTimedWait(long usec)
00140 {
00141     struct timeval T0, T1;
00142     timespec time;
00143     struct timeval now;
00144     int res1, res2;
00145 
00146     res1 = pthread_mutex_lock(&fMutex);
00147     if (res1 != 0) {
00148         jack_error("JackPosixProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
00149     }
00150 
00151     jack_log("JackPosixProcessSync::TimedWait time out = %ld", usec);
00152     gettimeofday(&T0, 0);
00153 
00154     gettimeofday(&now, 0);
00155     unsigned int next_date_usec = now.tv_usec + usec;
00156     time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
00157     time.tv_nsec = (next_date_usec % 1000000) * 1000;
00158     res2 = pthread_cond_timedwait(&fCond, &fMutex, &time);
00159     if (res2 != 0) {
00160         jack_error("JackPosixProcessSync::LockedTimedWait error usec = %ld err = %s", usec, strerror(res2));
00161     }
00162 
00163     gettimeofday(&T1, 0);
00164     res1 = pthread_mutex_unlock(&fMutex);
00165     if (res1 != 0) {
00166         jack_error("JackPosixProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
00167     }
00168 
00169     jack_log("JackPosixProcessSync::TimedWait finished delta = %5.1lf",
00170              (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
00171 
00172     return (res2 == 0);
00173 }
00174 
00175 
00176 } // end of namespace
00177