mptag.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #define mp_decl DECL_EXPORT
  14. #include "mptag.hpp"
  15. #include "jmutex.hpp"
  16. #include <jhash.hpp>
  17. #include <jarray.hpp>
  18. #include "jexcept.hpp"
  19. //for later reincluding mptag.hpp so default tags loaded into table
  20. #undef DEFTAG
  21. #undef DEFTAGRANGE
  22. #undef _DEFTAG
  23. #undef TAGENUM
  24. #undef TAGENUMEND
  25. #undef DEFSTDTAG
  26. #define DEFSTDTAG(t, v)
  27. #define TAGENUM
  28. #define TAGENUMEND
  29. #define DEFTAG(x) associateMPtag(x,#x,0); //tag,tracename,traceid
  30. #define DEFTAGRANGE(x,r) associateMPtag(x,#x,0); //tag,tracename,traceid // MORETBD
  31. typedef UnsignedArray FreedTagsArray;
  32. class CMPtagAllocator: implements IMPtagAllocator, public CInterface
  33. {
  34. CriticalSection crit;
  35. FreedTagsArray freedtags;
  36. unsigned counter;
  37. unsigned max;
  38. public:
  39. IMPLEMENT_IINTERFACE;
  40. CMPtagAllocator(mptag_t base,unsigned count)
  41. {
  42. counter = (unsigned)base;
  43. max = counter+count-1;
  44. }
  45. mptag_t alloc()
  46. {
  47. CriticalBlock block(crit);
  48. if (freedtags.length()>0)
  49. return (mptag_t)(freedtags.popGet());
  50. if(counter>max)
  51. throw MakeStringException(MSGAUD_operator,-1,"Out of message tags");
  52. mptag_t t = (mptag_t)counter;
  53. counter++;
  54. return t;
  55. }
  56. void release(mptag_t tag)
  57. {
  58. CriticalBlock block(crit);
  59. freedtags.append(tag);
  60. }
  61. };
  62. IMPtagAllocator *createMPtagRangeAllocator(mptag_t base,unsigned count)
  63. {
  64. return new CMPtagAllocator(base,count);
  65. }