blob: ad5daa7c1fba0a34312bfb90de54f084513e4df0 [file] [log] [blame]
reed@google.come6f7d682012-04-23 12:51:32 +00001//
2// SkTLS.h
rmistry@google.comfbfcd562012-08-23 18:09:54 +00003//
reed@google.come6f7d682012-04-23 12:51:32 +00004//
5// Created by Mike Reed on 4/21/12.
6// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7//
8
9#ifndef SkTLS_DEFINED
10#define SkTLS_DEFINED
11
12#include "SkTypes.h"
13
reed@google.comae1b6b62012-04-23 15:49:45 +000014/**
15 * Maintains a per-thread cache, using a CreateProc as the key into that cache.
16 */
reed@google.come6f7d682012-04-23 12:51:32 +000017class SkTLS {
18public:
19 typedef void* (*CreateProc)();
20 typedef void (*DeleteProc)(void*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000021
reed@google.come6f7d682012-04-23 12:51:32 +000022 /**
23 * If Get() has previously been called with this CreateProc, then this
24 * returns its cached data, otherwise it returns NULL. The CreateProc is
25 * never invoked in Find, it is only used as a key for searching the
26 * cache.
27 */
28 static void* Find(CreateProc);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029
reed@google.comae1b6b62012-04-23 15:49:45 +000030 /**
31 * Return the cached data that was returned by the CreateProc. This proc
32 * is only called the first time Get is called, and there after it is
33 * cached (per-thread), using the CreateProc as a key to look it up.
34 *
35 * When this thread, or Delete is called, the cached data is removed, and
36 * if a DeleteProc was specified, it is passed the pointer to the cached
37 * data.
38 */
39 static void* Get(CreateProc, DeleteProc);
40
41 /**
42 * Remove (optionally calling the DeleteProc if it was specificed in Get)
43 * the cached data associated with this CreateProc. If no associated cached
44 * data is found, do nothing.
45 */
46 static void Delete(CreateProc);
reed@google.com48ca7e32012-05-07 20:23:27 +000047
48private:
49 // Our implementation requires only 1 TLS slot, as we manage multiple values
50 // ourselves in a list, with the platform specific value as our head.
51
52 /**
reed@google.com331e2dc2012-05-08 21:45:03 +000053 * Implemented by the platform, to return the value of our (one) slot per-thread
54 *
55 * If forceCreateTheSlot is true, then we must have created the "slot" for
56 * our TLS, even though we know that the return value will be NULL in that
57 * case (i.e. no-slot and first-time-slot both return NULL). This ensures
58 * that after calling GetSpecific, we know that we can legally call
59 * SetSpecific.
60 *
61 * If forceCreateTheSlot is false, then the impl can either create the
62 * slot or not.
reed@google.com48ca7e32012-05-07 20:23:27 +000063 */
reed@google.com331e2dc2012-05-08 21:45:03 +000064 static void* PlatformGetSpecific(bool forceCreateTheSlot);
reed@google.com48ca7e32012-05-07 20:23:27 +000065
66 /**
reed@google.com331e2dc2012-05-08 21:45:03 +000067 * Implemented by the platform, to set the value for our (one) slot per-thread
68 *
69 * The implementation can rely on GetSpecific(true) having been previously
70 * called before SetSpecific is called.
reed@google.com48ca7e32012-05-07 20:23:27 +000071 */
72 static void PlatformSetSpecific(void*);
73
74public:
75 /**
76 * Will delete our internal list. To be called by the platform if/when its
77 * TLS slot is deleted (often at thread shutdown).
78 *
79 * Public *only* for the platform's use, not to be called by a client.
80 */
81 static void Destructor(void* ptr);
reed@google.come6f7d682012-04-23 12:51:32 +000082};
83
84#endif