summaryrefslogtreecommitdiff
path: root/ACE/apps/JAWS/clients/Caching/ID_Generator.h
blob: 827515a9b412882d21f7a69bd4cef3d9ef679eee (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
/* -*- C++ -*- */


//=============================================================================
/**
 *  @file    ID_Generator.h
 *
 *  @author Nanbor Wang
 */
//=============================================================================


#ifndef ACE_ID_GENERATOR_H
#define ACE_ID_GENERATOR_h

#include "ace/config-all.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#define ACE_OFFER_ID_LENGTH 21

/**
 * @class ACE_ID_Generator
 *
 * @brief An unique ID generator.
 */
class ACE_ID_Generator

  //     Generate an offer ID according to current time and avoid
  //     duplicate ID.  It guarantees ID uniqueness within a process,
  //     i.e. no two threads may get the same ID string.  Using a
  //     similar method like the backery algorithm.
{
public:
  /// allocate a new ID string and point <id> to it.
  static char *get_new_id (char *id);

private:
  /// Atomically get info required to generate an offer ID.
  static void get_serial_id (time_t &t, size_t &s);

  /// Get the lock instance.
  static ACE_SYNCH_MUTEX *get_lock (void);

  /// Record the time last offer ID generated.
  static time_t last_time_;

  /// Record serial number of last offer ID with same
  /// generation time.
  static size_t last_number_;

  /// mutex to access private member.
  static ACE_SYNCH_MUTEX *lock_;
};

#endif /* ACE_ID_GENERATOR_H */