Jack2 1.9.6

JackProcessSync.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 "JackProcessSync.h"
00021 #include "JackError.h"
00022 
00023 namespace Jack
00024 {
00025 
00026 void JackProcessSync::Signal()
00027 {
00028     int res = pthread_cond_signal(&fCond);
00029     if (res != 0)
00030         jack_error("JackProcessSync::Signal error err = %s", strerror(res));
00031 }
00032 
00033 void JackProcessSync::LockedSignal()
00034 {
00035     int res = pthread_mutex_lock(&fMutex);
00036     if (res != 0)
00037         jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
00038     res = pthread_cond_signal(&fCond);
00039     if (res != 0)
00040         jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
00041     res = pthread_mutex_unlock(&fMutex);
00042     if (res != 0)
00043         jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
00044 }
00045 
00046 void JackProcessSync::SignalAll()
00047 {
00048     int res = pthread_cond_broadcast(&fCond);
00049     if (res != 0)
00050         jack_error("JackProcessSync::SignalAll error err = %s", strerror(res));
00051 }
00052 
00053 void JackProcessSync::LockedSignalAll()
00054 {
00055     int res = pthread_mutex_lock(&fMutex);
00056     if (res != 0)
00057         jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
00058     res = pthread_cond_broadcast(&fCond);
00059     if (res != 0)
00060         jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
00061     res = pthread_mutex_unlock(&fMutex);
00062     if (res != 0)
00063         jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
00064 }
00065 
00066 void JackProcessSync::Wait()
00067 {
00068     int res;
00069     if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0)
00070         jack_error("JackProcessSync::Wait error err = %s", strerror(res));
00071  }
00072 
00073 void JackProcessSync::LockedWait()
00074 {
00075     int res;
00076     res = pthread_mutex_lock(&fMutex);
00077     if (res != 0)
00078         jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
00079     if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0)
00080         jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
00081     res = pthread_mutex_unlock(&fMutex);
00082     if (res != 0)
00083         jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
00084 }
00085 
00086 bool JackProcessSync::TimedWait(long usec)
00087 {
00088     struct timeval T0, T1;
00089     timespec time;
00090     struct timeval now;
00091     int res;
00092 
00093     jack_log("JackProcessSync::TimedWait time out = %ld", usec);
00094     gettimeofday(&T0, 0);
00095 
00096     gettimeofday(&now, 0);
00097     unsigned int next_date_usec = now.tv_usec + usec;
00098     time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
00099     time.tv_nsec = (next_date_usec % 1000000) * 1000;
00100     res = pthread_cond_timedwait(&fCond, &fMutex, &time);
00101     if (res != 0)
00102         jack_error("JackProcessSync::TimedWait error usec = %ld err = %s", usec, strerror(res));
00103 
00104     gettimeofday(&T1, 0);
00105     jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
00106              (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
00107     
00108     return (res == 0);
00109 }
00110 
00111 bool JackProcessSync::LockedTimedWait(long usec)
00112 {
00113     struct timeval T0, T1;
00114     timespec time;
00115     struct timeval now;
00116     int res1, res2;
00117 
00118     res1 = pthread_mutex_lock(&fMutex);
00119     if (res1 != 0)
00120         jack_error("JackProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
00121         
00122     jack_log("JackProcessSync::TimedWait time out = %ld", usec);
00123     gettimeofday(&T0, 0);
00124 
00125     gettimeofday(&now, 0);
00126     unsigned int next_date_usec = now.tv_usec + usec;
00127     time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
00128     time.tv_nsec = (next_date_usec % 1000000) * 1000;
00129     res2 = pthread_cond_timedwait(&fCond, &fMutex, &time);
00130     if (res2 != 0)
00131         jack_error("JackProcessSync::LockedTimedWait error usec = %ld err = %s", usec, strerror(res2));
00132 
00133     gettimeofday(&T1, 0);
00134     res1 = pthread_mutex_unlock(&fMutex);
00135     if (res1 != 0)
00136         jack_error("JackProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
00137         
00138     jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
00139              (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
00140     
00141     return (res2 == 0);
00142 }
00143 
00144 
00145 } // end of namespace
00146