Merge pull request #2317 from jwillemsen/jwi-deleteop
[ACE_TAO.git] / ACE / apps / JAWS / clients / Caching / ID_Generator.cpp
blob6ff6d9815a464de3e17ba40a070bd2f8a56de75e
1 #if !defined (ACE_ID_GENERATOR_C)
2 #define ACE_ID_GENERATOR_C
4 #include "ace/Object_Manager.h"
5 #include "ID_Generator.h"
7 time_t ACE_ID_Generator::last_time_ = 0;
9 size_t ACE_ID_Generator::last_number_ = 0;
11 ACE_SYNCH_MUTEX *ACE_ID_Generator::lock_ = 0;
13 char *
14 ACE_ID_Generator::get_new_id (char *id)
16 time_t t;
17 size_t sn;
19 ACE_ID_Generator::get_serial_id (t, sn);
20 ACE_NEW_RETURN (id, char [ACE_OFFER_ID_LENGTH], 0);
22 ACE_OS::sprintf (id, "%014d%06d", t, sn);
23 return id;
26 void
27 ACE_ID_Generator::get_serial_id (time_t &t, size_t &s)
29 ACE_MT (ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, *ACE_ID_Generator::get_lock ()));
30 ACE_OS::time (&t);
32 if (t != ACE_ID_Generator::last_time_)
34 ACE_ID_Generator::last_time_ = t;
35 s = ACE_ID_Generator::last_number_ = 0;
37 else
38 s = ACE_ID_Generator::last_number_++;
41 ACE_SYNCH_MUTEX *
42 ACE_ID_Generator::get_lock ()
44 #if defined (ACE_HAS_THREADS)
45 if (ACE_ID_Generator::lock_ == 0)
47 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, *ACE_Static_Object_Lock::instance (), 0));
49 // Double-checked Locking Optimization.
50 if (ACE_ID_Generator::lock_ == 0)
51 ACE_NEW_RETURN (ACE_ID_Generator::lock_, ACE_SYNCH_MUTEX, 0);
53 #endif /* ACE_HAS_THREADS */
54 return ACE_ID_Generator::lock_;
57 #endif /* ACE_ID_GENERATOR_C */