blob: 35343301f2a8007c815a0c55ff596263960d38a0 [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
Chris Friedt0a29a482022-10-23 09:19:48 -040023#include <functional>
cyy316723a2019-01-05 16:35:14 +080024#include <memory>
David Reiss791a57f2008-06-10 22:54:56 +000025
Chris Friedt0a29a482022-10-23 09:19:48 -040026#include <thrift/concurrency/Thread.h>
27
Konrad Grochowski16a23a62014-11-13 15:33:38 +010028namespace apache {
29namespace thrift {
30namespace concurrency {
David Reiss791a57f2008-06-10 22:54:56 +000031
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 Reiss6c1ee212010-03-09 05:19:56 +000040 * // To create a thread that executes my_thread_main once:
Jake Farrell753b6c52011-12-06 01:41:59 +000041 * shared_ptr<Thread> thread = factory->newThread(
42 * FunctionRunner::create(my_thread_main, some_argument));
David Reiss791a57f2008-06-10 22:54:56 +000043 * thread->start();
44 *
David Reiss6c1ee212010-03-09 05:19:56 +000045 * bool A::foo();
46 * A* a = new A();
47 * // To create a thread that executes a.foo() every 100 milliseconds:
Jake Farrell753b6c52011-12-06 01:41:59 +000048 * factory->newThread(FunctionRunner::create(
cyy316723a2019-01-05 16:35:14 +080049 * std::bind(&A::foo, a), 100))->start();
David Reiss791a57f2008-06-10 22:54:56 +000050 *
David Reiss791a57f2008-06-10 22:54:56 +000051 */
52
53class FunctionRunner : public Runnable {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010054public:
David Reiss791a57f2008-06-10 22:54:56 +000055 // This is the type of callback 'pthread_create()' expects.
Konrad Grochowski16a23a62014-11-13 15:33:38 +010056 typedef void* (*PthreadFuncPtr)(void* arg);
David Reiss791a57f2008-06-10 22:54:56 +000057 // This a fully-generic void(void) callback for custom bindings.
cyy316723a2019-01-05 16:35:14 +080058 typedef std::function<void()> VoidFunc;
David Reiss791a57f2008-06-10 22:54:56 +000059
cyy316723a2019-01-05 16:35:14 +080060 typedef std::function<bool()> BoolFunc;
David Reiss6c1ee212010-03-09 05:19:56 +000061
David Reiss791a57f2008-06-10 22:54:56 +000062 /**
Jake Farrell753b6c52011-12-06 01:41:59 +000063 * Syntactic sugar to make it easier to create new FunctionRunner
64 * objects wrapped in shared_ptr.
65 */
cyy316723a2019-01-05 16:35:14 +080066 static std::shared_ptr<FunctionRunner> create(const VoidFunc& cob) {
67 return std::shared_ptr<FunctionRunner>(new FunctionRunner(cob));
Jake Farrell753b6c52011-12-06 01:41:59 +000068 }
69
cyy316723a2019-01-05 16:35:14 +080070 static std::shared_ptr<FunctionRunner> create(PthreadFuncPtr func, void* arg) {
71 return std::shared_ptr<FunctionRunner>(new FunctionRunner(func, arg));
Jake Farrell753b6c52011-12-06 01:41:59 +000072 }
73
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040074private:
Konrad Grochowski16a23a62014-11-13 15:33:38 +010075 static void pthread_func_wrapper(PthreadFuncPtr func, void* arg) {
76 // discard return value
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040077 func(arg);
78 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +010079
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040080public:
Jake Farrell753b6c52011-12-06 01:41:59 +000081 /**
David Reiss791a57f2008-06-10 22:54:56 +000082 * 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)
cyy316723a2019-01-05 16:35:14 +080086 : func_(std::bind(pthread_func_wrapper, func, arg)), intervalMs_(-1) {}
David Reiss791a57f2008-06-10 22:54:56 +000087
88 /**
89 * Given a generic callback, this FunctionRunner will execute it.
90 */
James E. King, III36200902016-10-05 14:47:18 -040091 FunctionRunner(const VoidFunc& cob) : func_(cob), intervalMs_(-1) {}
David Reiss791a57f2008-06-10 22:54:56 +000092
David Reiss6c1ee212010-03-09 05:19:56 +000093 /**
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 Grochowski16a23a62014-11-13 15:33:38 +010099 FunctionRunner(const BoolFunc& cob, int intervalMs) : repFunc_(cob), intervalMs_(intervalMs) {}
David Reiss791a57f2008-06-10 22:54:56 +0000100
Sebastian Zenker042580f2019-01-29 15:48:12 +0100101 void run() override {
David Reiss6c1ee212010-03-09 05:19:56 +0000102 if (repFunc_) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100103 while (repFunc_()) {
104 THRIFT_SLEEP_USEC(intervalMs_ * 1000);
David Reiss6c1ee212010-03-09 05:19:56 +0000105 }
106 } else {
107 func_();
108 }
David Reiss791a57f2008-06-10 22:54:56 +0000109 }
110
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111private:
David Reiss791a57f2008-06-10 22:54:56 +0000112 VoidFunc func_;
David Reiss6c1ee212010-03-09 05:19:56 +0000113 BoolFunc repFunc_;
114 int intervalMs_;
David Reiss791a57f2008-06-10 22:54:56 +0000115};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100116}
117}
118} // apache::thrift::concurrency
David Reiss791a57f2008-06-10 22:54:56 +0000119
120#endif // #ifndef _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H