1/*
2 * counter.c --
3 *
4 *	Implementation of a mutex protected counter.
5 *	Used to generate channel handles.
6 *
7 * Copyright (C) 1996-1999 Andreas Kupries (a.kupries@westend.com)
8 * All rights reserved.
9 *
10 * Permission is hereby granted, without written agreement and without
11 * license or royalty fees, to use, copy, modify, and distribute this
12 * software and its documentation for any purpose, provided that the
13 * above copyright notice and the following two paragraphs appear in
14 * all copies of this software.
15 *
16 * IN NO EVENT SHALL I BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
17 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
18 * SOFTWARE AND ITS DOCUMENTATION, EVEN IF I HAVE BEEN ADVISED OF THE
19 * POSSIBILITY OF SUCH DAMAGE.
20 *
21 * I SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
24 * I HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
25 * ENHANCEMENTS, OR MODIFICATIONS.
26 *
27 * CVS: $Id: counter.c,v 1.6 2000/09/26 20:52:50 aku Exp $
28 */
29
30
31#include "memchanInt.h"
32
33Tcl_Obj*
34MemchanGenHandle (prefix)
35CONST char* prefix;
36{
37  /* 3 alternatives for implementation:
38   * a) Tcl before 8.x
39   * b) 8.0.x (objects, non-threaded)
40   * c) 8.1.x (objects, possibly threaded)
41   */
42
43  /*
44   * count number of generated (memory) channels,
45   * used for id generation. Ids are never reclaimed
46   * and there is no dealing with wrap around. On the
47   * other hand, "unsigned long" should be big enough
48   * except for absolute longrunners (generate a 100 ids
49   * per second => overflow will occur in 1 1/3 years).
50   */
51
52#if GT81
53  TCL_DECLARE_MUTEX (memchanCounterMutex)
54  static unsigned long memCounter = 0;
55
56  char     channelName [50];
57  Tcl_Obj* res = Tcl_NewStringObj ((char*) prefix, -1);
58
59  Tcl_MutexLock (&memchanCounterMutex);
60  {
61    LTOA (memCounter, channelName);
62    memCounter ++;
63  }
64  Tcl_MutexUnlock (&memchanCounterMutex);
65
66  Tcl_AppendStringsToObj (res, channelName, (char*) NULL);
67
68  return res;
69
70#else /* TCL_MAJOR_VERSION == 8 */
71  static unsigned long memCounter = 0;
72
73  char     channelName [50];
74  Tcl_Obj* res = Tcl_NewStringObj ((char*) prefix, -1);
75
76  LTOA (memCounter, channelName);
77  memCounter ++;
78
79  Tcl_AppendStringsToObj (res, channelName, (char*) NULL);
80
81  return res;
82#endif
83}
84