blob: 5a8cd3dcdfe2453b824e930473b672ad00d11ec7 [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
23#include <tr1/functional>
24#include "thrift/lib/cpp/concurrency/Thread.h"
25
T Jake Lucianib5e62212009-01-31 22:36:20 +000026namespace apache { namespace thrift { namespace concurrency {
David Reiss791a57f2008-06-10 22:54:56 +000027
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 Reiss6c1ee212010-03-09 05:19:56 +000036 * // To create a thread that executes my_thread_main once:
David Reiss791a57f2008-06-10 22:54:56 +000037 * shared_ptr<Thread> thread =
38 * factory->newThread(shared_ptr<FunctionRunner>(
39 * new FunctionRunner(my_thread_main, some_argument)));
40 * thread->start();
41 *
David Reiss6c1ee212010-03-09 05:19:56 +000042 * bool A::foo();
43 * A* a = new A();
44 * // To create a thread that executes a.foo() every 100 milliseconds:
45 * factory->newThread(shared_ptr<FunctionRunner>(
46 * new FunctionRunner(std::tr1::bind(&A::foo, a), 100)))->start();
David Reiss791a57f2008-06-10 22:54:56 +000047 *
David Reiss791a57f2008-06-10 22:54:56 +000048 */
49
50class FunctionRunner : public Runnable {
51 public:
52 // This is the type of callback 'pthread_create()' expects.
53 typedef void* (*PthreadFuncPtr)(void *arg);
54 // This a fully-generic void(void) callback for custom bindings.
55 typedef std::tr1::function<void()> VoidFunc;
56
David Reiss6c1ee212010-03-09 05:19:56 +000057 typedef std::tr1::function<bool()> BoolFunc;
58
David Reiss791a57f2008-06-10 22:54:56 +000059 /**
60 * Given a 'pthread_create' style callback, this FunctionRunner will
61 * execute the given callback. Note that the 'void*' return value is ignored.
62 */
63 FunctionRunner(PthreadFuncPtr func, void* arg)
David Reiss6c1ee212010-03-09 05:19:56 +000064 : func_(std::tr1::bind(func, arg)), repFunc_(0)
David Reiss791a57f2008-06-10 22:54:56 +000065 { }
66
67 /**
68 * Given a generic callback, this FunctionRunner will execute it.
69 */
70 FunctionRunner(const VoidFunc& cob)
David Reiss6c1ee212010-03-09 05:19:56 +000071 : func_(cob), repFunc_(0)
David Reiss791a57f2008-06-10 22:54:56 +000072 { }
73
David Reiss6c1ee212010-03-09 05:19:56 +000074 /**
75 * Given a bool foo(...) type callback, FunctionRunner will execute
76 * the callback repeatedly with 'intervalMs' milliseconds between the calls,
77 * until it returns false. Note that the actual interval between calls will
78 * be intervalMs plus execution time of the callback.
79 */
80 FunctionRunner(const BoolFunc& cob, int intervalMs)
81 : func_(0), repFunc_(cob), intervalMs_(intervalMs)
82 { }
David Reiss791a57f2008-06-10 22:54:56 +000083
84 void run() {
David Reiss6c1ee212010-03-09 05:19:56 +000085 if (repFunc_) {
86 while(repFunc_()) {
87 usleep(intervalMs_*1000);
88 }
89 } else {
90 func_();
91 }
David Reiss791a57f2008-06-10 22:54:56 +000092 }
93
94 private:
95 VoidFunc func_;
David Reiss6c1ee212010-03-09 05:19:56 +000096 BoolFunc repFunc_;
97 int intervalMs_;
David Reiss791a57f2008-06-10 22:54:56 +000098};
99
T Jake Lucianib5e62212009-01-31 22:36:20 +0000100}}} // apache::thrift::concurrency
David Reiss791a57f2008-06-10 22:54:56 +0000101
102#endif // #ifndef _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H