xref: /haiku/src/add-ons/kernel/file_systems/netfs/headers/shared/ThreadLocal.h (revision 922e7ba1f3228e6f28db69b0ded8f86eb32dea17)
1 // ThreadLocal.h
2 
3 #ifndef THREAD_LOCAL_H
4 #define THREAD_LOCAL_H
5 
6 #include <SupportDefs.h>
7 
8 // ThreadLocalFreeHandler
9 class ThreadLocalFreeHandler {
10 public:
11 								ThreadLocalFreeHandler();
12 	virtual						~ThreadLocalFreeHandler();
13 
14 	virtual	void				Free(void* data) = 0;
15 };
16 
17 // ThreadLocal
18 class ThreadLocal {
19 public:
20 								ThreadLocal(
21 									ThreadLocalFreeHandler* freeHandler = NULL);
22 								~ThreadLocal();
23 
24 			status_t			Set(void* data);
25 			void				Unset();
26 			void*				Get() const;
27 
28 private:
29 			struct ThreadLocalMap;
30 
31 			ThreadLocalMap*		fMap;
32 			ThreadLocalFreeHandler* fFreeHandler;
33 };
34 
35 // ThreadLocalUnsetter
36 class ThreadLocalUnsetter {
37 public:
38 	ThreadLocalUnsetter(ThreadLocal* threadLocal)
39 		: fThreadLocal(threadLocal)
40 	{
41 	}
42 
43 	ThreadLocalUnsetter(ThreadLocal& threadLocal)
44 		: fThreadLocal(&threadLocal)
45 	{
46 	}
47 
48 	~ThreadLocalUnsetter()
49 	{
50 		if (fThreadLocal)
51 			fThreadLocal->Unset();
52 	}
53 
54 private:
55 	ThreadLocal*	fThreadLocal;
56 };
57 
58 #endif	// THREAD_LOCAL_H
59