Commit de269272 authored by Kirill Smelkov's avatar Kirill Smelkov

libgolang: Provide func as alias for std::function

std::function is frequently too long to type while func is more native to Go.
parent a6c1c984
......@@ -41,8 +41,8 @@ from libcpp.utility cimport pair
# XXX for std::function cython does not provide operator() and =nullptr
#from libcpp.functional cimport function
#ctypedef function[void()] cancelFunc
cdef extern from "<functional>" namespace "std" nogil:
cppclass cancelFunc "std::function<void()>":
cdef extern from "golang/libgolang.h" namespace "golang" nogil:
cppclass cancelFunc "golang::func<void()>":
void operator() ()
void operator= (nullptr_t)
......
......@@ -37,7 +37,7 @@ cdef extern from "golang/sync.h" namespace "golang::sync" nogil:
void unlock()
cppclass Once:
void do "do_" (...) # ... = std::function
void do "do_" (...) # ... = func<void()>
cppclass WaitGroup:
void done()
......
......@@ -59,7 +59,7 @@ cdef extern from "golang/time.h" namespace "golang::time" nogil:
chan[double] tick(double dt)
chan[double] after(double dt)
Timer after_func(double dt, ...) # ... = std::function<void()>
Timer after_func(double dt, ...) # ... = func<void()>
cppclass _Ticker:
chan[double] c
......
......@@ -30,7 +30,6 @@
#include <vector>
using std::pair;
using std::make_pair;
using std::function;
using std::vector;
......@@ -306,7 +305,7 @@ struct _TimeoutCtx : _CancelCtx {
};
pair<Context, function<void()>>
pair<Context, func<void()>>
with_cancel(Context parent) {
refptr<_CancelCtx> cctx = adoptref(new _CancelCtx({parent}));
Context ctx = newref (static_cast<_Context*>(cctx._ptr()));
......@@ -318,7 +317,7 @@ with_value(Context parent, const void *key, interface value) {
return adoptref(static_cast<_Context*>(new _ValueCtx(key, value, parent)));
}
pair<Context, function<void()>>
pair<Context, func<void()>>
with_deadline(Context parent, double deadline) {
// parent's deadline is before deadline -> just use parent
double pdead = parent->deadline();
......@@ -328,8 +327,8 @@ with_deadline(Context parent, double deadline) {
// timeout <= 0 -> already canceled
double timeout = deadline - time::now();
if (timeout <= 0) {
Context ctx;
function<void()> cancel;
Context ctx;
func<void()> cancel;
tie(ctx, cancel) = with_cancel(parent);
cancel();
return make_pair(ctx, cancel);
......@@ -340,12 +339,12 @@ with_deadline(Context parent, double deadline) {
return make_pair(ctx, [tctx]() { tctx->_cancel(canceled); });
}
pair<Context, function<void()>>
pair<Context, func<void()>>
with_timeout(Context parent, double timeout) {
return with_deadline(parent, time::now() + timeout);
}
pair<Context, function<void()>>
pair<Context, func<void()>>
merge(Context parent1, Context parent2) {
refptr<_CancelCtx> cctx = adoptref(new _CancelCtx({parent1, parent2}));
Context ctx = newref (static_cast<_Context*>(cctx._ptr()));
......
......@@ -78,7 +78,7 @@ extern LIBGOLANG_API const error deadlineExceeded;
//
// The caller should explicitly call cancel to release context resources as soon
// the context is no longer needed.
LIBGOLANG_API std::pair<Context, std::function<void()>>
LIBGOLANG_API std::pair<Context, func<void()>>
with_cancel(Context parent); // -> ctx, cancel
// with_value creates new context with key=value.
......@@ -96,13 +96,13 @@ LIBGOLANG_API Context
//
// The caller should explicitly call cancel to release context resources as soon
// the context is no longer needed.
LIBGOLANG_API std::pair<Context, std::function<void()>>
LIBGOLANG_API std::pair<Context, func<void()>>
with_deadline(Context parent, double deadline); // -> ctx, cancel
// with_timeout creates new context with timeout.
//
// it is shorthand for with_deadline(parent, now+timeout).
LIBGOLANG_API std::pair<Context, std::function<void()>>
LIBGOLANG_API std::pair<Context, func<void()>>
with_timeout(Context parent, double timeout); // -> ctx, cancel
// merge merges 2 contexts into 1.
......@@ -118,7 +118,7 @@ LIBGOLANG_API std::pair<Context, std::function<void()>>
//
// Note: on Go side merge is not part of stdlib context and is provided by
// https://godoc.org/lab.nexedi.com/kirr/go123/xcontext#hdr-Merging_contexts
LIBGOLANG_API std::pair<Context, std::function<void()>>
LIBGOLANG_API std::pair<Context, func<void()>>
merge(Context parent1, Context parent2); // -> ctx, cancel
// for testing
......
......@@ -299,10 +299,14 @@ LIBGOLANG_API extern void (*_tblockforever)(void);
namespace golang {
// func is alias for std::function.
template<typename F>
using func = std::function<F>;
// go provides type-safe wrapper over _taskgo.
template<typename F, typename... Argv> // F = std::function<void(Argv...)>
static inline void go(F /*std::function<void(Argv...)>*/ f, Argv... argv) {
typedef std::function<void(void)> Frun;
template<typename F, typename... Argv> // F = func<void(Argv...)>
static inline void go(F /*func<void(Argv...)>*/ f, Argv... argv) {
typedef func<void()> Frun;
Frun *frun = new Frun (std::bind(f, argv...));
_taskgo([](void *_frun) {
std::unique_ptr<Frun> frun (reinterpret_cast<Frun*>(_frun));
......@@ -452,7 +456,7 @@ int select(const std::vector<_selcase> &casev) {
// NOTE contrary to Go f is called at end of current scope, not function.
#define defer(f) golang::_deferred _defer_ ## __COUNTER__ (f)
struct _deferred {
typedef std::function<void(void)> F;
typedef func<void()> F;
F f;
_deferred(F f) : f(f) {}
......
......@@ -28,7 +28,6 @@
#include <utility>
#include <string.h>
using namespace golang;
using std::function;
using std::move;
using std::tie;
......@@ -186,7 +185,7 @@ template<typename T> void waitBlocked_TX(chan<T> ch) {
// .
//
// f -> heap
static void usestack_and_call(function<void()> f, int nframes=128) {
static void usestack_and_call(func<void()> f, int nframes=128) {
if (nframes == 0) {
f();
return;
......
......@@ -34,7 +34,7 @@ Once::Once() {
Once::~Once() {}
void Once::do_(const std::function<void(void)> &f) {
void Once::do_(const func<void()> &f) {
Once *once = this;
once->_mu.lock();
defer([&]() {
......
......@@ -110,7 +110,7 @@ class Once {
public:
LIBGOLANG_API Once();
LIBGOLANG_API ~Once();
LIBGOLANG_API void do_(const std::function<void(void)> &f);
LIBGOLANG_API void do_(const func<void()> &f);
private:
Once(const Once&); // don't copy
......
......@@ -24,8 +24,6 @@
#include <math.h>
using std::function;
// golang::time:: (except sleep and now)
namespace golang {
......@@ -36,7 +34,7 @@ namespace time {
Ticker new_ticker(double dt);
Timer new_timer (double dt);
Timer _new_timer(double dt, function<void()>);
Timer _new_timer(double dt, func<void()>);
chan<double> tick(double dt) {
......@@ -49,7 +47,7 @@ chan<double> after(double dt) {
return new_timer(dt)->c;
}
Timer after_func(double dt, function<void()> f) {
Timer after_func(double dt, func<void()> f) {
return _new_timer(dt, f);
}
......@@ -120,7 +118,7 @@ void _Timer::decref() {
delete this;
}
Timer _new_timer(double dt, function<void()> f) {
Timer _new_timer(double dt, func<void()> f) {
Timer t = adoptref(new _Timer());
t->c = (f == NULL ? makechan<double>(1) : NULL);
t->_f = f;
......
......@@ -94,7 +94,7 @@ LIBGOLANG_API chan<double> after(double dt);
//
// The function will be called in its own goroutine.
// Returned timer can be used to cancel the call.
LIBGOLANG_API Timer after_func(double dt, std::function<void()> f);
LIBGOLANG_API Timer after_func(double dt, func<void()> f);
// new_ticker creates new Ticker that will be firing at dt intervals.
......@@ -141,7 +141,7 @@ struct _Timer : object {
chan<double> c;
private:
std::function<void()> _f;
func<void()> _f;
sync::Mutex _mu;
double _dt; // +inf - stopped, otherwise - armed
......@@ -151,7 +151,7 @@ private:
private:
_Timer();
~_Timer();
friend Timer _new_timer(double dt, std::function<void()> f);
friend Timer _new_timer(double dt, func<void()> f);
public:
LIBGOLANG_API void decref();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment