Skip to content

Commit 435f9a2

Browse files
author
Chrono Law
committed
threads
1 parent 968b881 commit 435f9a2

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

ngxpp/NgxThread.hpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NGX_THREAD_HPP
4+
#define _NGX_THREAD_HPP
5+
6+
#include "NgxPool.hpp"
7+
#include "NgxEvent.hpp"
8+
9+
template<typename T>
10+
class NgxThreadTask final : public NgxWrapper<ngx_thread_task_t>
11+
{
12+
public:
13+
typedef NgxWrapper<ngx_thread_task_t> super_type;
14+
typedef NgxThreadTask this_type;
15+
typedef T task_ctx_type;
16+
public:
17+
NgxThreadTask(ngx_thread_task_t* t) : super_type(t)
18+
{}
19+
20+
~NgxThreadTask() = default;
21+
public:
22+
T& ctx() const
23+
{
24+
return *reinterpret_cast<T*>(get()->ctx);
25+
}
26+
27+
template<typename F>
28+
void handler(F f) const
29+
{
30+
if(!get()->handler)
31+
{
32+
get()->handler = f;
33+
}
34+
}
35+
public:
36+
NgxEvent event() const
37+
{
38+
return &get()->event;
39+
}
40+
public:
41+
static ngx_thread_task_t* create(const NgxPool& pool)
42+
{
43+
auto p = pool.thread_task<T>();
44+
45+
return p;
46+
}
47+
};
48+
49+
class NgxThreadPool final : public NgxWrapper<ngx_thread_pool_t>
50+
{
51+
public:
52+
typedef NgxWrapper<ngx_thread_pool_t> super_type;
53+
typedef NgxThreadPool this_type;
54+
public:
55+
NgxThreadPool(ngx_thread_pool_t* p) : super_type(p)
56+
{}
57+
58+
~NgxThreadPool() = default;
59+
public:
60+
void post(ngx_thread_task_t* task) const
61+
{
62+
auto rc = ngx_thread_task_post(get(), task);
63+
64+
NgxException::require(rc);
65+
}
66+
public:
67+
static ngx_thread_pool_t* acquire(ngx_str_t& name)
68+
{
69+
auto p = ngx_thread_pool_get((ngx_cycle_t *) ngx_cycle, &name);
70+
71+
NgxException::require(p);
72+
73+
return p;
74+
}
75+
};
76+
77+
#endif //_NGX_THREAD_HPP

0 commit comments

Comments
 (0)