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