blob: 93765c89c1c72967748c4b6aa7191d9d6e6fce9d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#include "ace/Local_Tokens.h"
#include "ace/Token_Manager.h"
#include "ace/Task.h"
#include "ace/OS_NS_time.h"
#if defined (ACE_HAS_TOKENS_LIBRARY)
class Device;
// Listing 1 code/ch14
class HA_Device_Repository
{
public:
enum { N_DEVICES = 100 };
HA_Device_Repository ()
{
for (int i = 0; i < N_DEVICES; i++)
tokens_[i] = new ACE_Local_Mutex (0, 0, 1);
}
~HA_Device_Repository ()
{
for (int i = 0; i < N_DEVICES; i++)
delete tokens_[i];
}
int update_device (int device_id, char *commands)
{
this->tokens_[device_id]->acquire ();
Device *curr_device = this->devices_[device_id];
internal_do (curr_device);
this->tokens_[device_id]->release ();
return 0;
}
void internal_do (Device *device);
private:
Device *devices_[N_DEVICES];
ACE_Local_Mutex *tokens_[N_DEVICES];
unsigned int seed_;
};
// Listing 1
void
HA_Device_Repository::internal_do (Device *device)
{
ACE_UNUSED_ARG (device); // Real code would use this.
ACE_Time_Value tv (0, ACE_OS::rand_r (&this->seed_) % 10000);
timespec_t t = (timespec_t)tv;
ACE_OS::nanosleep (&t);
}
// Listing 2 code/ch14
class HA_CommandHandler : public ACE_Task_Base
{
public:
enum { N_THREADS = 5 };
HA_CommandHandler (HA_Device_Repository &rep) : rep_(rep)
{ }
int svc ()
{
for (int i = 0; i < HA_Device_Repository::N_DEVICES; i++)
rep_.update_device (i, "");
return 0;
}
private:
HA_Device_Repository &rep_;
};
int ACE_TMAIN (int, ACE_TCHAR *[])
{
HA_Device_Repository rep;
HA_CommandHandler handler (rep);
handler.activate (THR_NEW_LWP | THR_JOINABLE,
HA_CommandHandler::N_THREADS);
handler.wait ();
return 0;
}
// Listing 2
#else /* defined (ACE_HAS_TOKENS_LIBRARY) */
int ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("local tokens not supported ")
ACE_TEXT ("on this platform\n")));
return 0;
}
#endif /* defined (ACE_HAS_TOKENS_LIBRARY) */
|