forked from birdwyx/phpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_local_storage.h
79 lines (59 loc) · 1.96 KB
/
task_local_storage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include <libgo/config.h>
#include <libgo/scheduler.h>
#define INITIAL_TASK_CAPACITY (1024 + 1) /* 0 not used */
#define MAX_TASK_CAPACITY (1024 * 1024 +1) /* 0 not used */
#define INITIAL_KEY_CAPACITY (16 + 1) /* 0 not used */
#define MAX_KEY_CAPACITY (1024 + 1) /* 0 not used */
namespace co
{
class Freeable;
class Task;
typedef uint64_t kls_key_t;
class TaskLocalStorage{
static std::vector<std::vector<Freeable*>> kls;
static std::map<std::string, kls_key_t> kls_key_map;
static uint64_t kls_key_count;
static std::mutex kls_mutex;
public:
static kls_key_t CreateKey(std::string str_key);
static bool SetSpecific(kls_key_t key, Freeable* pointer);
ALWAYS_INLINE static Freeable* GetSpecific(kls_key_t key){
//cout << "get key:" << key << endl;
Task* tk = g_Scheduler.GetCurrentTask();
if(!tk) return nullptr;
auto task_id = tk->id_;
if(kls.size() < task_id +1) return nullptr;
if(kls[task_id].size() < key + 1) return nullptr;
return kls[task_id][key];
}
ALWAYS_INLINE static Freeable* GetSpecific(kls_key_t key, uint64_t task_id){
//printf( "get key: %d task %d\n", key, task_id );
if(kls.size() < task_id +1) return nullptr;
if(kls[task_id].size() < key + 1) return nullptr;
return kls[task_id][key];
}
static void FreeSpecifics(uint64_t);
static void Dump();
};
template <class T>
class TaskLocalMaker {
T* tlv;
kls_key_t key;
public:
TaskLocalMaker<T>(std::string str_key){
tlv = nullptr;
key = TaskLocalStorage::CreateKey(str_key);
if( g_Scheduler.GetCurrentTask() && !(tlv = (T*)TaskLocalStorage::GetSpecific(key)) ){
tlv = new T();
TaskLocalStorage::SetSpecific(key, tlv);
}
}
ALWAYS_INLINE operator T*(){
return tlv;
}
ALWAYS_INLINE operator T&(){
return *tlv;
}
};
} //namespace co