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 | |
| 23 | #include <tr1/functional> |
| 24 | #include "thrift/lib/cpp/concurrency/Thread.h" |
| 25 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 26 | namespace apache { namespace thrift { namespace concurrency { |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Convenient implementation of Runnable that will execute arbitrary callbacks. |
| 30 | * Interfaces are provided to accept both a generic 'void(void)' callback, and |
| 31 | * a 'void* (void*)' pthread_create-style callback. |
| 32 | * |
| 33 | * Example use: |
| 34 | * void* my_thread_main(void* arg); |
| 35 | * shared_ptr<ThreadFactory> factory = ...; |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 36 | * // To create a thread that executes my_thread_main once: |
Jake Farrell | 753b6c5 | 2011-12-06 01:41:59 +0000 | [diff] [blame] | 37 | * shared_ptr<Thread> thread = factory->newThread( |
| 38 | * FunctionRunner::create(my_thread_main, some_argument)); |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 39 | * thread->start(); |
| 40 | * |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 41 | * bool A::foo(); |
| 42 | * A* a = new A(); |
| 43 | * // To create a thread that executes a.foo() every 100 milliseconds: |
Jake Farrell | 753b6c5 | 2011-12-06 01:41:59 +0000 | [diff] [blame] | 44 | * factory->newThread(FunctionRunner::create( |
| 45 | * std::tr1::bind(&A::foo, a), 100))->start(); |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 46 | * |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 47 | */ |
| 48 | |
| 49 | class FunctionRunner : public Runnable { |
| 50 | public: |
| 51 | // This is the type of callback 'pthread_create()' expects. |
| 52 | typedef void* (*PthreadFuncPtr)(void *arg); |
| 53 | // This a fully-generic void(void) callback for custom bindings. |
| 54 | typedef std::tr1::function<void()> VoidFunc; |
| 55 | |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 56 | typedef std::tr1::function<bool()> BoolFunc; |
| 57 | |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 58 | /** |
Jake Farrell | 753b6c5 | 2011-12-06 01:41:59 +0000 | [diff] [blame] | 59 | * Syntactic sugar to make it easier to create new FunctionRunner |
| 60 | * objects wrapped in shared_ptr. |
| 61 | */ |
| 62 | static boost::shared_ptr<FunctionRunner> create(const VoidFunc& cob) { |
| 63 | return boost::shared_ptr<FunctionRunner>(new FunctionRunner(cob)); |
| 64 | } |
| 65 | |
| 66 | static boost::shared_ptr<FunctionRunner> create(PthreadFuncPtr func, |
| 67 | void* arg) { |
| 68 | return boost::shared_ptr<FunctionRunner>(new FunctionRunner(func, arg)); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 73 | * Given a 'pthread_create' style callback, this FunctionRunner will |
| 74 | * execute the given callback. Note that the 'void*' return value is ignored. |
| 75 | */ |
| 76 | FunctionRunner(PthreadFuncPtr func, void* arg) |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 77 | : func_(std::tr1::bind(func, arg)), repFunc_(0) |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 78 | { } |
| 79 | |
| 80 | /** |
| 81 | * Given a generic callback, this FunctionRunner will execute it. |
| 82 | */ |
| 83 | FunctionRunner(const VoidFunc& cob) |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 84 | : func_(cob), repFunc_(0) |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 85 | { } |
| 86 | |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 87 | /** |
| 88 | * Given a bool foo(...) type callback, FunctionRunner will execute |
| 89 | * the callback repeatedly with 'intervalMs' milliseconds between the calls, |
| 90 | * until it returns false. Note that the actual interval between calls will |
| 91 | * be intervalMs plus execution time of the callback. |
| 92 | */ |
| 93 | FunctionRunner(const BoolFunc& cob, int intervalMs) |
| 94 | : func_(0), repFunc_(cob), intervalMs_(intervalMs) |
| 95 | { } |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 96 | |
| 97 | void run() { |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 98 | if (repFunc_) { |
| 99 | while(repFunc_()) { |
| 100 | usleep(intervalMs_*1000); |
| 101 | } |
| 102 | } else { |
| 103 | func_(); |
| 104 | } |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private: |
| 108 | VoidFunc func_; |
David Reiss | 6c1ee21 | 2010-03-09 05:19:56 +0000 | [diff] [blame] | 109 | BoolFunc repFunc_; |
| 110 | int intervalMs_; |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 113 | }}} // apache::thrift::concurrency |
David Reiss | 791a57f | 2008-06-10 22:54:56 +0000 | [diff] [blame] | 114 | |
| 115 | #endif // #ifndef _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H |