1 #include "ace/Local_Tokens.h"
2 #include "ace/Token_Manager.h"
4 #include "ace/OS_NS_time.h"
6 #if defined (ACE_HAS_TOKENS_LIBRARY)
10 // Listing 1 code/ch14
11 class HA_Device_Repository
14 enum { N_DEVICES
= 100 };
16 HA_Device_Repository ()
18 for (int i
= 0; i
< N_DEVICES
; i
++)
19 tokens_
[i
] = new ACE_Local_Mutex (0, 0, 1);
22 ~HA_Device_Repository ()
24 for (int i
= 0; i
< N_DEVICES
; i
++)
28 int update_device (int device_id
, char *commands
)
30 this->tokens_
[device_id
]->acquire ();
32 Device
*curr_device
= this->devices_
[device_id
];
33 internal_do (curr_device
);
35 this->tokens_
[device_id
]->release ();
40 void internal_do (Device
*device
);
43 Device
*devices_
[N_DEVICES
];
44 ACE_Local_Mutex
*tokens_
[N_DEVICES
];
50 HA_Device_Repository::internal_do (Device
*device
)
52 ACE_UNUSED_ARG (device
); // Real code would use this.
53 ACE_Time_Value
tv (0, ACE_OS::rand_r (&this->seed_
) % 10000);
54 timespec_t t
= (timespec_t
)tv
;
55 ACE_OS::nanosleep (&t
);
58 // Listing 2 code/ch14
59 class HA_CommandHandler
: public ACE_Task_Base
62 enum { N_THREADS
= 5 };
64 HA_CommandHandler (HA_Device_Repository
&rep
) : rep_(rep
)
69 for (int i
= 0; i
< HA_Device_Repository::N_DEVICES
; i
++)
70 rep_
.update_device (i
, "");
75 HA_Device_Repository
&rep_
;
78 int ACE_TMAIN (int, ACE_TCHAR
*[])
80 HA_Device_Repository rep
;
81 HA_CommandHandler
handler (rep
);
82 handler
.activate (THR_NEW_LWP
| THR_JOINABLE
,
83 HA_CommandHandler::N_THREADS
);
89 #else /* defined (ACE_HAS_TOKENS_LIBRARY) */
91 int ACE_TMAIN (int, ACE_TCHAR
*[])
94 ACE_TEXT ("local tokens not supported ")
95 ACE_TEXT ("on this platform\n")));
99 #endif /* defined (ACE_HAS_TOKENS_LIBRARY) */