David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H |
| 21 | #define _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H 1 |
| 22 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 23 | #include <thrift/cxxfunctional.h> |
| 24 | #include <thrift/concurrency/Thread.h> |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 25 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 26 | namespace apache { |
| 27 | namespace thrift { |
| 28 | namespace concurrency { |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 29 | |
| 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 Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 38 | * // To create a thread that executes my_thread_main once: |
Jake Farrell | 753b6c5 | 2011-12-06 01:41:59 +0000 | [diff] [blame] | 39 | * shared_ptr<Thread> thread = factory->newThread( |
| 40 | * FunctionRunner::create(my_thread_main, some_argument)); |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 41 | * thread->start(); |
| 42 | * |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 43 | * bool A::foo(); |
| 44 | * A* a = new A(); |
| 45 | * // To create a thread that executes a.foo() every 100 milliseconds: |
Jake Farrell | 753b6c5 | 2011-12-06 01:41:59 +0000 | [diff] [blame] | 46 | * factory->newThread(FunctionRunner::create( |
Roger Meier | 8252577 | 2012-11-16 00:38:27 +0000 | [diff] [blame] | 47 | * apache::thrift::stdcxx::bind(&A::foo, a), 100))->start(); |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 48 | * |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | class FunctionRunner : public Runnable { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 52 | public: |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 53 | // This is the type of callback 'pthread_create()' expects. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 54 | typedef void* (*PthreadFuncPtr)(void* arg); |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 55 | // This a fully-generic void(void) callback for custom bindings. |
Roger Meier | 8252577 | 2012-11-16 00:38:27 +0000 | [diff] [blame] | 56 | typedef apache::thrift::stdcxx::function<void()> VoidFunc; |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 57 | |
Roger Meier | 8252577 | 2012-11-16 00:38:27 +0000 | [diff] [blame] | 58 | typedef apache::thrift::stdcxx::function<bool()> BoolFunc; |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 59 | |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 60 | /** |
Jake Farrell | 753b6c5 | 2011-12-06 01:41:59 +0000 | [diff] [blame] | 61 | * 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 68 | static boost::shared_ptr<FunctionRunner> create(PthreadFuncPtr func, void* arg) { |
Jake Farrell | 753b6c5 | 2011-12-06 01:41:59 +0000 | [diff] [blame] | 69 | return boost::shared_ptr<FunctionRunner>(new FunctionRunner(func, arg)); |
| 70 | } |
| 71 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 72 | private: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 73 | static void pthread_func_wrapper(PthreadFuncPtr func, void* arg) { |
| 74 | // discard return value |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 75 | func(arg); |
| 76 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 77 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 78 | public: |
Jake Farrell | 753b6c5 | 2011-12-06 01:41:59 +0000 | [diff] [blame] | 79 | /** |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 80 | * 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 84 | : func_(apache::thrift::stdcxx::bind(pthread_func_wrapper, func, arg)) {} |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Given a generic callback, this FunctionRunner will execute it. |
| 88 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 89 | FunctionRunner(const VoidFunc& cob) : func_(cob) {} |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 90 | |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 91 | /** |
| 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 97 | FunctionRunner(const BoolFunc& cob, int intervalMs) : repFunc_(cob), intervalMs_(intervalMs) {} |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 98 | |
| 99 | void run() { |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 100 | if (repFunc_) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 101 | while (repFunc_()) { |
| 102 | THRIFT_SLEEP_USEC(intervalMs_ * 1000); |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 103 | } |
| 104 | } else { |
| 105 | func_(); |
| 106 | } |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 109 | private: |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 110 | VoidFunc func_; |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 111 | BoolFunc repFunc_; |
| 112 | int intervalMs_; |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 113 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 114 | } |
| 115 | } |
| 116 | } // apache::thrift::concurrency |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 117 | |
| 118 | #endif // #ifndef _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H |