SkTLS.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkTLS_DEFINED
  8. #define SkTLS_DEFINED
  9. #include "include/core/SkTypes.h"
  10. /**
  11. * Maintains a per-thread cache, using a CreateProc as the key into that cache.
  12. */
  13. class SkTLS {
  14. public:
  15. typedef void* (*CreateProc)();
  16. typedef void (*DeleteProc)(void*);
  17. /**
  18. * If Get() has previously been called with this CreateProc, then this
  19. * returns its cached data, otherwise it returns nullptr. The CreateProc is
  20. * never invoked in Find, it is only used as a key for searching the
  21. * cache.
  22. */
  23. static void* Find(CreateProc);
  24. /**
  25. * Return the cached data that was returned by the CreateProc. This proc
  26. * is only called the first time Get is called, and there after it is
  27. * cached (per-thread), using the CreateProc as a key to look it up.
  28. *
  29. * When this thread, or Delete is called, the cached data is removed, and
  30. * if a DeleteProc was specified, it is passed the pointer to the cached
  31. * data.
  32. */
  33. static void* Get(CreateProc, DeleteProc);
  34. /**
  35. * Remove (optionally calling the DeleteProc if it was specificed in Get)
  36. * the cached data associated with this CreateProc. If no associated cached
  37. * data is found, do nothing.
  38. */
  39. static void Delete(CreateProc);
  40. private:
  41. // Our implementation requires only 1 TLS slot, as we manage multiple values
  42. // ourselves in a list, with the platform specific value as our head.
  43. /**
  44. * Implemented by the platform, to return the value of our (one) slot per-thread
  45. *
  46. * If forceCreateTheSlot is true, then we must have created the "slot" for
  47. * our TLS, even though we know that the return value will be nullptr in that
  48. * case (i.e. no-slot and first-time-slot both return nullptr). This ensures
  49. * that after calling GetSpecific, we know that we can legally call
  50. * SetSpecific.
  51. *
  52. * If forceCreateTheSlot is false, then the impl can either create the
  53. * slot or not.
  54. */
  55. static void* PlatformGetSpecific(bool forceCreateTheSlot);
  56. /**
  57. * Implemented by the platform, to set the value for our (one) slot per-thread
  58. *
  59. * The implementation can rely on GetSpecific(true) having been previously
  60. * called before SetSpecific is called.
  61. */
  62. static void PlatformSetSpecific(void*);
  63. public:
  64. /**
  65. * Will delete our internal list. To be called by the platform if/when its
  66. * TLS slot is deleted (often at thread shutdown).
  67. *
  68. * Public *only* for the platform's use, not to be called by a client.
  69. */
  70. static void Destructor(void* ptr);
  71. };
  72. #endif