Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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
 */
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
 
26
namespace apache { namespace thrift { namespace concurrency {
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 = ...;
36
 *  shared_ptr<Thread> thread =
37
 *    factory->newThread(shared_ptr<FunctionRunner>(
38
 *      new FunctionRunner(my_thread_main, some_argument)));
39
 *  thread->start();
40
 *
41
 *
42
 */
43
 
44
class FunctionRunner : public Runnable {
45
 public:
46
  // This is the type of callback 'pthread_create()' expects.
47
  typedef void* (*PthreadFuncPtr)(void *arg);
48
  // This a fully-generic void(void) callback for custom bindings.
49
  typedef std::tr1::function<void()> VoidFunc;
50
 
51
  /**
52
   * Given a 'pthread_create' style callback, this FunctionRunner will
53
   * execute the given callback.  Note that the 'void*' return value is ignored.
54
   */
55
  FunctionRunner(PthreadFuncPtr func, void* arg)
56
   : func_(std::tr1::bind(func, arg))
57
  { }
58
 
59
  /**
60
   * Given a generic callback, this FunctionRunner will execute it.
61
   */
62
  FunctionRunner(const VoidFunc& cob)
63
   : func_(cob)
64
  { }
65
 
66
 
67
  void run() {
68
    func_();
69
  }
70
 
71
 private:
72
  VoidFunc func_;
73
};
74
 
75
}}} // apache::thrift::concurrency
76
 
77
#endif // #ifndef _THRIFT_CONCURRENCY_FUNCTION_RUNNER_H