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 General Public License as published by
6 the Free Software Foundation; either version 2 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "JackMachThread.h"
30 #include "JackPosixThread.h"
31 #include "JackMutex.h"
36 static void CleanupHandler(void * arg
)
38 JackLockAble
* locked
= (JackLockAble
*)arg
;
39 printf("CleanupHandler locked %px \n", locked
);
43 struct LockedObject
: public JackLockAble
{
47 LockedObject():fCount(0)
50 virtual ~LockedObject()
58 printf("LockedMethod1 self %x fCount %d\n", pthread_self(), fCount);
60 printf("Terminate self %x\n", pthread_self());
69 printf("LockedMethod2 self %x fCount %d\n", pthread_self(), fCount);
71 printf("Terminate self %x\n", pthread_self());
80 printf("LockedMethod3 self %x fCount %d\n", pthread_self(), fCount);
82 printf("Terminate self %x\n", pthread_self());
90 pthread_cleanup_push(CleanupHandler
, this);
93 //printf("LockedMethod1 self %x fCount %d\n", pthread_self(), fCount);
95 printf("Terminate self = %px count = %d\n", pthread_self(), fCount
);
99 pthread_cleanup_pop(0);
104 pthread_cleanup_push(CleanupHandler
, this);
108 //printf("LockedMethod2 self %x fCount %d\n", pthread_self(), fCount);
109 if (fCount
>= 1500) {
110 printf("Terminate self = %px count = %d\n", pthread_self(), fCount
);
114 pthread_cleanup_pop(0);
119 pthread_cleanup_push(CleanupHandler
, this);
123 //printf("LockedMethod3 self %x fCount %d\n", pthread_self(), fCount);
124 if (fCount
>= 3000) {
125 printf("Terminate self = %px count = %d\n", pthread_self(), fCount
);
129 pthread_cleanup_pop(0);
135 struct TestThread
: public JackRunnableInterface
{
137 JackMachThread
* fThread
;
138 LockedObject
* fObject
;
141 TestThread(LockedObject
* obj
, int num
)
143 printf("TestThread\n");
144 fThread
= new JackMachThread(this);
147 fThread
->StartSync();
150 virtual ~TestThread()
152 printf("DELETE %px\n", fThread
);
159 //printf("TestThread Execute\n");
163 fObject
->LockedMethod1();
165 if (fObject->fCount >= 500) {
166 printf("Terminate self %x\n", pthread_self());
167 fThread->Terminate();
173 fObject
->LockedMethod2();
175 if (fObject->fCount >= 1500) {
176 printf("Terminate self %x\n", pthread_self());
177 fThread->Terminate();
183 fObject
->LockedMethod3();
185 if (fObject->fCount >= 2000) {
186 printf("Terminate self %x\n", pthread_self());
187 fThread->Terminate();
193 //usleep(fNum * 1000);
199 static void* TestThread1_Execute(void* arg
);
201 struct TestThread1
: public JackRunnableInterface
{
204 LockedObject
* fObject
;
207 TestThread1(LockedObject
* obj
, int num
)
209 if (jack_client_create_thread(NULL
, &fThread
, 0, 0, TestThread1_Execute
, this))
210 jack_error( "Can't create the network manager control thread." );
215 virtual ~TestThread1()
220 printf("TestThread Execute\n");
224 fObject
->LockedMethod1();
228 fObject
->LockedMethod2();
232 fObject
->LockedMethod3();
236 //usleep(fNum * 1000);
242 static void* TestThread1_Execute(void* arg
)
244 TestThread1
* obj
= (TestThread1
*)arg
;
247 //printf("TestThread Execute\n");
251 obj
->fObject
->LockedMethod1();
255 obj
->fObject
->LockedMethod2();
259 obj
->fObject
->LockedMethod3();
263 //usleep(obj->fNum * 1000);
269 int main (int argc
, char * const argv
[])
275 TestThread
th1(&obj
, 1);
276 TestThread
th2(&obj
, 2);
277 TestThread
th3(&obj
, 3);
281 TestThread1 th1(&obj, 1);
282 TestThread th2(&obj, 2);
283 TestThread th3(&obj, 3);
287 while ((c = getchar()) != 'q') {