blob: b776794109468d149a4756c4ae4075a2e8268395 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
David Reiss791a57f2008-06-10 22:54:56 +000019
20#ifndef _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H
21#define _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H 1
22
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040023#include <thrift/cxxfunctional.h>
24#include <thrift/concurrency/Thread.h>
David Reiss791a57f2008-06-10 22:54:56 +000025
Konrad Grochowski16a23a62014-11-13 15:33:38 +010026namespace apache {
27namespace thrift {
28namespace concurrency {
David Reiss791a57f2008-06-10 22:54:56 +000029
30/**
31 * Convenient implementation of Runnable that will execute arbitrary callbacks.
32 * Interfaces are provided to accept both a generic 'void(void)' callback, and
33 * a 'void* (void*)' pthread_create-style callback.
34 *
35 * Example use:
36 * void* my_thread_main(void* arg);
37 * shared_ptr<ThreadFactory> factory = ...;
David Reiss6c1ee212010-03-09 05:19:56 +000038 * // To create a thread that executes my_thread_main once:
Jake Farrell753b6c52011-12-06 01:41:59 +000039 * shared_ptr<Thread> thread = factory->newThread(
40 * FunctionRunner::create(my_thread_main, some_argument));
David Reiss791a57f2008-06-10 22:54:56 +000041 * thread->start();
42 *
David Reiss6c1ee212010-03-09 05:19:56 +000043 * bool A::foo();
44 * A* a = new A();
45 * // To create a thread that executes a.foo() every 100 milliseconds:
Jake Farrell753b6c52011-12-06 01:41:59 +000046 * factory->newThread(FunctionRunner::create(
Roger Meier82525772012-11-16 00:38:27 +000047 * apache::thrift::stdcxx::bind(&A::foo, a), 100))->start();
David Reiss791a57f2008-06-10 22:54:56 +000048 *
David Reiss791a57f2008-06-10 22:54:56 +000049 */
50
51class FunctionRunner : public Runnable {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010052public:
David Reiss791a57f2008-06-10 22:54:56 +000053 // This is the type of callback 'pthread_create()' expects.
Konrad Grochowski16a23a62014-11-13 15:33:38 +010054 typedef void* (*PthreadFuncPtr)(void* arg);
David Reiss791a57f2008-06-10 22:54:56 +000055 // This a fully-generic void(void) callback for custom bindings.
Roger Meier82525772012-11-16 00:38:27 +000056 typedef apache::thrift::stdcxx::function<void()> VoidFunc;
David Reiss791a57f2008-06-10 22:54:56 +000057
Roger Meier82525772012-11-16 00:38:27 +000058 typedef apache::thrift::stdcxx::function<bool()> BoolFunc;
David Reiss6c1ee212010-03-09 05:19:56 +000059
David Reiss791a57f2008-06-10 22:54:56 +000060 /**
Jake Farrell753b6c52011-12-06 01:41:59 +000061 * Syntactic sugar to make it easier to create new FunctionRunner
62 * objects wrapped in shared_ptr.
63 */
64 static boost::shared_ptr<FunctionRunner> create(const VoidFunc& cob) {
65 return boost::shared_ptr<FunctionRunner>(new FunctionRunner(cob));
66 }
67
Konrad Grochowski16a23a62014-11-13 15:33:38 +010068 static boost::shared_ptr<FunctionRunner> create(PthreadFuncPtr func, void* arg) {
Jake Farrell753b6c52011-12-06 01:41:59 +000069 return boost::shared_ptr<FunctionRunner>(new FunctionRunner(func, arg));
70 }
71
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040072private:
Konrad Grochowski16a23a62014-11-13 15:33:38 +010073 static void pthread_func_wrapper(PthreadFuncPtr func, void* arg) {
74 // discard return value
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040075 func(arg);
76 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +010077
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040078public:
Jake Farrell753b6c52011-12-06 01:41:59 +000079 /**
David Reiss791a57f2008-06-10 22:54:56 +000080 * Given a 'pthread_create' style callback, this FunctionRunner will
81 * execute the given callback. Note that the 'void*' return value is ignored.
82 */
83 FunctionRunner(PthreadFuncPtr func, void* arg)
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084 : func_(apache::thrift::stdcxx::bind(pthread_func_wrapper, func, arg)) {}
David Reiss791a57f2008-06-10 22:54:56 +000085
86 /**
87 * Given a generic callback, this FunctionRunner will execute it.
88 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +010089 FunctionRunner(const VoidFunc& cob) : func_(cob) {}
David Reiss791a57f2008-06-10 22:54:56 +000090
David Reiss6c1ee212010-03-09 05:19:56 +000091 /**
92 * Given a bool foo(...) type callback, FunctionRunner will execute
93 * the callback repeatedly with 'intervalMs' milliseconds between the calls,
94 * until it returns false. Note that the actual interval between calls will
95 * be intervalMs plus execution time of the callback.
96 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097 FunctionRunner(const BoolFunc& cob, int intervalMs) : repFunc_(cob), intervalMs_(intervalMs) {}
David Reiss791a57f2008-06-10 22:54:56 +000098
99 void run() {
David Reiss6c1ee212010-03-09 05:19:56 +0000100 if (repFunc_) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100101 while (repFunc_()) {
102 THRIFT_SLEEP_USEC(intervalMs_ * 1000);
David Reiss6c1ee212010-03-09 05:19:56 +0000103 }
104 } else {
105 func_();
106 }
David Reiss791a57f2008-06-10 22:54:56 +0000107 }
108
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100109private:
David Reiss791a57f2008-06-10 22:54:56 +0000110 VoidFunc func_;
David Reiss6c1ee212010-03-09 05:19:56 +0000111 BoolFunc repFunc_;
112 int intervalMs_;
David Reiss791a57f2008-06-10 22:54:56 +0000113};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100114}
115}
116} // apache::thrift::concurrency
David Reiss791a57f2008-06-10 22:54:56 +0000117
118#endif // #ifndef _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H