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
|
// $Id$
#if !defined (ACE_ID_GENERATOR_C)
#define ACE_ID_GENERATOR_C
#include "ace/Object_Manager.h"
#include "ID_Generator.h"
ACE_RCSID(Caching, ID_Generator, "$Id$")
time_t ACE_ID_Generator::last_time_ = 0;
size_t ACE_ID_Generator::last_number_ = 0;
ACE_SYNCH_MUTEX *ACE_ID_Generator::lock_ = 0;
char *
ACE_ID_Generator::get_new_id (char *id)
{
time_t t;
size_t sn;
ACE_ID_Generator::get_serial_id (t, sn);
ACE_NEW_RETURN (id, char [ACE_OFFER_ID_LENGTH], 0);
ACE_OS::sprintf (id, "%014d%06d", t, sn);
return id;
}
void
ACE_ID_Generator::get_serial_id (time_t &t, size_t &s)
{
ACE_MT (ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, *ACE_ID_Generator::get_lock ()));
ACE_OS::time (&t);
if (t != ACE_ID_Generator::last_time_)
{
ACE_ID_Generator::last_time_ = t;
s = ACE_ID_Generator::last_number_ = 0;
}
else
s = ACE_ID_Generator::last_number_++;
}
ACE_SYNCH_MUTEX *
ACE_ID_Generator::get_lock (void)
{
#if defined (ACE_HAS_THREADS)
if (ACE_ID_Generator::lock_ == 0)
{
ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, *ACE_Static_Object_Lock::instance (), 0));
// Double-checked Locking Optimization.
if (ACE_ID_Generator::lock_ == 0)
ACE_NEW_RETURN (ACE_ID_Generator::lock_, ACE_SYNCH_MUTEX, 0);
}
#endif /* ACE_HAS_THREADS */
return ACE_ID_Generator::lock_;
}
#endif /* ACE_ID_GENERATOR_C */
|