2 //=============================================================================
4 * @file Task_Ex_Test.cpp
6 * This test program illustrates the ACE_Task_Ex class which has the ACE_Message_Queue_Ex
7 * that has the capability to hold user-defined messages instead of ACE_Message_Block
9 * @author Kobi Cohen-Arazi <kobi-co@barak-online.net>
11 //=============================================================================
14 #include "test_config.h"
15 #include "Task_Ex_Test.h"
16 #include "ace/Task_Ex_T.h"
17 #include "ace/Log_Msg.h"
18 #include "ace/Auto_Ptr.h"
22 #if defined (ACE_HAS_THREADS)
25 #if defined (ACE_VXWORKS) || defined (ACE_LYNXOS_MAJOR)
26 // this is a very expensive test on VxWorks so limit it otherwise it will never finish in time:-)
27 const ACE_INT32 PRODUCER_THREADS_NO
=10;
28 const ACE_INT32 CONSUMER_THREADS_NO
=10;
29 const ACE_INT32 NUMBER_OF_MSGS
=200;
31 const ACE_INT32 PRODUCER_THREADS_NO
=20;
32 const ACE_INT32 CONSUMER_THREADS_NO
=20;
33 const ACE_INT32 NUMBER_OF_MSGS
=2000;
36 /// @class Consumer consumes user defined Msgs
37 class Consumer
: public ACE_Task_Ex
<ACE_MT_SYNCH
, User_Defined_Msg
>
40 //FUZZ: disable check_for_lack_ACE_OS
41 /// activate/spawn the threads.
42 ///FUZZ: enable check_for_lack_ACE_OS
45 /// svc thread entry point
46 virtual int svc (void);
51 int Consumer::open (void*)
53 if(this->activate (THR_NEW_LWP
| THR_JOINABLE
,
54 CONSUMER_THREADS_NO
)==-1)
56 ACE_ERROR_RETURN((LM_ERROR
,
57 ACE_TEXT("Consumer::open Error spanwing thread %p\n"),
66 User_Defined_Msg
* pMsg
=0;
67 while(this->getq (pMsg
)!=-1)
69 ACE_TEST_ASSERT (pMsg
!=0);
70 #if defined (ACE_HAS_CPP11)
71 std::unique_ptr
<User_Defined_Msg
> pAuto(pMsg
);
73 auto_ptr
<User_Defined_Msg
> pAuto(pMsg
);
74 #endif /* ACE_HAS_CPP11 */
76 ACE_TEXT("Consumer::svc got msg id=%d\n"),
78 if(pMsg
->msg_id ()==NUMBER_OF_MSGS
-1)
83 ACE_TEXT("Consumer::svc ended thread %t\n")));
89 /// producer function produces user defined messages.
90 ACE_THR_FUNC_RETURN
producer (void *arg
)
92 Consumer
* c
= static_cast<Consumer
*> (arg
);
93 ACE_TEST_ASSERT(c
!=0);
97 ACE_TEXT("producer Error casting to consumer\n")));
98 return (ACE_THR_FUNC_RETURN
)-1;
100 for (int i
=0;i
!=NUMBER_OF_MSGS
;++i
)
102 User_Defined_Msg
* pMsg
=0;
103 ACE_NEW_NORETURN(pMsg
, User_Defined_Msg(i
));
107 ACE_TEXT("producer Error allocating data %p\n"),
109 return (ACE_THR_FUNC_RETURN
)-1;
111 if(c
->putq (pMsg
)==-1)
114 ACE_TEXT("producer Error putq data %p\n"),
116 return (ACE_THR_FUNC_RETURN
)-1;
122 #endif /* ACE_HAS_THREADS */
125 run_main (int, ACE_TCHAR
*[])
127 ACE_START_TEST (ACE_TEXT ("Task_Ex_Test"));
129 #if defined (ACE_HAS_THREADS)
133 ACE_ERROR_RETURN((LM_ERROR
,
134 ACE_TEXT ("main Error opening consumer\n")),-1);
137 int result
=ACE_Thread_Manager::instance()->spawn_n (PRODUCER_THREADS_NO
,
138 ACE_THR_FUNC(producer
),
139 static_cast<void*> (&c
));
142 ACE_ERROR_RETURN((LM_ERROR
,
143 ACE_TEXT ("main Error spawning threads %p\n"),
148 int wait_result
=ACE_Thread_Manager::instance()->wait();
152 ACE_TEXT("main Error Thread_Manager->wait %p\n"),
158 ACE_TEXT ("threads not supported on this platform\n")));
159 #endif /* ACE_HAS_THREADS */