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