michael@0: // michael@0: // SkTLS.h michael@0: // michael@0: // michael@0: // Created by Mike Reed on 4/21/12. michael@0: // Copyright (c) 2012 __MyCompanyName__. All rights reserved. michael@0: // michael@0: michael@0: #ifndef SkTLS_DEFINED michael@0: #define SkTLS_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: /** michael@0: * Maintains a per-thread cache, using a CreateProc as the key into that cache. michael@0: */ michael@0: class SkTLS { michael@0: public: michael@0: typedef void* (*CreateProc)(); michael@0: typedef void (*DeleteProc)(void*); michael@0: michael@0: /** michael@0: * If Get() has previously been called with this CreateProc, then this michael@0: * returns its cached data, otherwise it returns NULL. The CreateProc is michael@0: * never invoked in Find, it is only used as a key for searching the michael@0: * cache. michael@0: */ michael@0: static void* Find(CreateProc); michael@0: michael@0: /** michael@0: * Return the cached data that was returned by the CreateProc. This proc michael@0: * is only called the first time Get is called, and there after it is michael@0: * cached (per-thread), using the CreateProc as a key to look it up. michael@0: * michael@0: * When this thread, or Delete is called, the cached data is removed, and michael@0: * if a DeleteProc was specified, it is passed the pointer to the cached michael@0: * data. michael@0: */ michael@0: static void* Get(CreateProc, DeleteProc); michael@0: michael@0: /** michael@0: * Remove (optionally calling the DeleteProc if it was specificed in Get) michael@0: * the cached data associated with this CreateProc. If no associated cached michael@0: * data is found, do nothing. michael@0: */ michael@0: static void Delete(CreateProc); michael@0: michael@0: private: michael@0: // Our implementation requires only 1 TLS slot, as we manage multiple values michael@0: // ourselves in a list, with the platform specific value as our head. michael@0: michael@0: /** michael@0: * Implemented by the platform, to return the value of our (one) slot per-thread michael@0: * michael@0: * If forceCreateTheSlot is true, then we must have created the "slot" for michael@0: * our TLS, even though we know that the return value will be NULL in that michael@0: * case (i.e. no-slot and first-time-slot both return NULL). This ensures michael@0: * that after calling GetSpecific, we know that we can legally call michael@0: * SetSpecific. michael@0: * michael@0: * If forceCreateTheSlot is false, then the impl can either create the michael@0: * slot or not. michael@0: */ michael@0: static void* PlatformGetSpecific(bool forceCreateTheSlot); michael@0: michael@0: /** michael@0: * Implemented by the platform, to set the value for our (one) slot per-thread michael@0: * michael@0: * The implementation can rely on GetSpecific(true) having been previously michael@0: * called before SetSpecific is called. michael@0: */ michael@0: static void PlatformSetSpecific(void*); michael@0: michael@0: public: michael@0: /** michael@0: * Will delete our internal list. To be called by the platform if/when its michael@0: * TLS slot is deleted (often at thread shutdown). michael@0: * michael@0: * Public *only* for the platform's use, not to be called by a client. michael@0: */ michael@0: static void Destructor(void* ptr); michael@0: }; michael@0: michael@0: #endif