Init engine fields, cleanup.
[jack2.git] / posix / JackProcessSync.cpp
blob2daa49b75be0f7d5c036b65888c75c9d00701364
1 /*
2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "JackProcessSync.h"
21 #include "JackError.h"
23 namespace Jack
26 void JackProcessSync::Signal()
28 int res = pthread_cond_signal(&fCond);
29 if (res != 0)
30 jack_error("JackProcessSync::Signal error err = %s", strerror(res));
33 void JackProcessSync::LockedSignal()
35 int res = pthread_mutex_lock(&fMutex);
36 if (res != 0)
37 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
38 res = pthread_cond_signal(&fCond);
39 if (res != 0)
40 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
41 res = pthread_mutex_unlock(&fMutex);
42 if (res != 0)
43 jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
46 void JackProcessSync::SignalAll()
48 int res = pthread_cond_broadcast(&fCond);
49 if (res != 0)
50 jack_error("JackProcessSync::SignalAll error err = %s", strerror(res));
53 void JackProcessSync::LockedSignalAll()
55 int res = pthread_mutex_lock(&fMutex);
56 if (res != 0)
57 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
58 res = pthread_cond_broadcast(&fCond);
59 if (res != 0)
60 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
61 res = pthread_mutex_unlock(&fMutex);
62 if (res != 0)
63 jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
66 void JackProcessSync::Wait()
68 int res;
69 if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0)
70 jack_error("JackProcessSync::Wait error err = %s", strerror(res));
73 void JackProcessSync::LockedWait()
75 int res;
76 res = pthread_mutex_lock(&fMutex);
77 if (res != 0)
78 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
79 if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0)
80 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
81 res = pthread_mutex_unlock(&fMutex);
82 if (res != 0)
83 jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
86 bool JackProcessSync::TimedWait(long usec)
88 struct timeval T0, T1;
89 timespec time;
90 struct timeval now;
91 int res;
93 jack_log("JackProcessSync::TimedWait time out = %ld", usec);
94 gettimeofday(&T0, 0);
96 gettimeofday(&now, 0);
97 unsigned int next_date_usec = now.tv_usec + usec;
98 time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
99 time.tv_nsec = (next_date_usec % 1000000) * 1000;
100 res = pthread_cond_timedwait(&fCond, &fMutex, &time);
101 if (res != 0)
102 jack_error("JackProcessSync::TimedWait error usec = %ld err = %s", usec, strerror(res));
104 gettimeofday(&T1, 0);
105 jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
106 (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
108 return (res == 0);
111 bool JackProcessSync::LockedTimedWait(long usec)
113 struct timeval T0, T1;
114 timespec time;
115 struct timeval now;
116 int res1, res2;
118 res1 = pthread_mutex_lock(&fMutex);
119 if (res1 != 0)
120 jack_error("JackProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
122 jack_log("JackProcessSync::TimedWait time out = %ld", usec);
123 gettimeofday(&T0, 0);
125 gettimeofday(&now, 0);
126 unsigned int next_date_usec = now.tv_usec + usec;
127 time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
128 time.tv_nsec = (next_date_usec % 1000000) * 1000;
129 res2 = pthread_cond_timedwait(&fCond, &fMutex, &time);
130 if (res2 != 0)
131 jack_error("JackProcessSync::LockedTimedWait error usec = %ld err = %s", usec, strerror(res2));
133 gettimeofday(&T1, 0);
134 res1 = pthread_mutex_unlock(&fMutex);
135 if (res1 != 0)
136 jack_error("JackProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
138 jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
139 (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
141 return (res2 == 0);
145 } // end of namespace