Skip to content

Commit

Permalink
threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrono Law committed Nov 16, 2015
1 parent 968b881 commit 435f9a2
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions ngxpp/NgxThread.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2015
// Author: Chrono Law
#ifndef _NGX_THREAD_HPP
#define _NGX_THREAD_HPP

#include "NgxPool.hpp"
#include "NgxEvent.hpp"

template<typename T>
class NgxThreadTask final : public NgxWrapper<ngx_thread_task_t>
{
public:
typedef NgxWrapper<ngx_thread_task_t> super_type;
typedef NgxThreadTask this_type;
typedef T task_ctx_type;
public:
NgxThreadTask(ngx_thread_task_t* t) : super_type(t)
{}

~NgxThreadTask() = default;
public:
T& ctx() const
{
return *reinterpret_cast<T*>(get()->ctx);
}

template<typename F>
void handler(F f) const
{
if(!get()->handler)
{
get()->handler = f;
}
}
public:
NgxEvent event() const
{
return &get()->event;
}
public:
static ngx_thread_task_t* create(const NgxPool& pool)
{
auto p = pool.thread_task<T>();

return p;
}
};

class NgxThreadPool final : public NgxWrapper<ngx_thread_pool_t>
{
public:
typedef NgxWrapper<ngx_thread_pool_t> super_type;
typedef NgxThreadPool this_type;
public:
NgxThreadPool(ngx_thread_pool_t* p) : super_type(p)
{}

~NgxThreadPool() = default;
public:
void post(ngx_thread_task_t* task) const
{
auto rc = ngx_thread_task_post(get(), task);

NgxException::require(rc);
}
public:
static ngx_thread_pool_t* acquire(ngx_str_t& name)
{
auto p = ngx_thread_pool_get((ngx_cycle_t *) ngx_cycle, &name);

NgxException::require(p);

return p;
}
};

#endif //_NGX_THREAD_HPP

0 comments on commit 435f9a2

Please sign in to comment.