6d8999b7c1
Some simple changes for HPHP to help make it clang friendly ---hphp/compiler/package.cpp ---hphp/compiler/package.h ---hphp/runtime/ext/ext_curl.cpp ---hphp/runtime/ext/pdo_mysql.cpp ---hphp/tools/tc-print/offline-x86-code.h ---hphp/util/async_func.cpp ---hphp/util/async_func.h ---hphp/util/compression.cpp ---hphp/util/compression.h ---hphp/util/db_conn.cpp ---hphp/util/db_conn.h Deleted unused private fields ~~~hphp/compiler/analysis/symbol_table.h Static assert had to be outside the union +++hphp/runtime/TARGETS Added clang specific flag to supress unneded declaration warning ~~~hphp/runtime/base/datatype.h Use of logical '&&' with constant operand. Added !=0 to remove warning, ~~~hphp/runtime/base/string_data.h Static fields cannot be declared in an anonymous struct/union ~~~hphp/runtime/ext/bcmath/TARGETS Moved gcc specific flag from preprocessor_flags to compiler_specific_flags ~---hphp/runtime/ext/pdo_mysql.cpp Removed unnecessary self asignment for row_count ~~~hphp/runtime/vm/bytecode.h Added default return statement ~---hphp/runtime/vm/jit/codegen.cpp spillSlotsToSize was unused ~~~hphp/runtime/vm/jit/irtranslator.cpp ~~~hphp/runtime/vm/jit/linearscan.cpp The c++ standard states default arguments shall not be specified in the parameter-declaration-clause of a lambda-declarator. ~~~hphp/runtime/vm/jit/vectortranslator-internal.h Clang had some issues determining the correct cast when these macros were used A simple '!= 0' check was added to make things explicit for clang. ~~~hphp/tools/tc-print/perf-events.h Added parens to make clang happy +++hphp/util/asm-x64.h The constexpr needed to be initialized +++hphp/util/base.h Clang also supports tr1 libraries ~~~hphp/util/bits.h Again, clang had issues with implicit casting to bool. added != 0 check ~~~hphp/util/malloc_size_class.h Ambiguous operator precedence. Added parentheses. ~~~hphp/util/thread_local.h Misspelled function
217 linhas
5.9 KiB
C++
217 linhas
5.9 KiB
C++
/*
|
|
+----------------------------------------------------------------------+
|
|
| HipHop for PHP |
|
|
+----------------------------------------------------------------------+
|
|
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
|
|
+----------------------------------------------------------------------+
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
| available through the world-wide-web at the following url: |
|
|
| http://www.php.net/license/3_01.txt |
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
+----------------------------------------------------------------------+
|
|
*/
|
|
|
|
#ifndef incl_HPHP_CONCURRENCY_ASYNC_FUNC_H_
|
|
#define incl_HPHP_CONCURRENCY_ASYNC_FUNC_H_
|
|
|
|
#include "hphp/util/base.h"
|
|
#include <pthread.h>
|
|
#include "hphp/util/synchronizable.h"
|
|
#include "hphp/util/lock.h"
|
|
#include "hphp/util/exception.h"
|
|
#include "hphp/util/alloc.h"
|
|
|
|
namespace HPHP {
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* Invokes a function asynchrously. For example,
|
|
*
|
|
* class MyClass {
|
|
* public:
|
|
* void doJob();
|
|
* };
|
|
*
|
|
* MyClass obj;
|
|
* AsyncFunc<MyClass> func(&obj, &MyClasss::doJob);
|
|
* func.start(); // this will call obj.doJob() in a separate thread
|
|
* // do something else
|
|
* func.waitForEnd();
|
|
*
|
|
* Asynchronous function is a slightly different way of thinking about threads.
|
|
* Maybe this can help people understand asynchronous function is actually a
|
|
* broader/identical view of running threads,
|
|
*
|
|
* class MyRunnable {
|
|
* public:
|
|
* void run();
|
|
* };
|
|
*
|
|
* MyRunnable thread;
|
|
* AsyncFunc<Runnable> func(&thread, &MyRunnable::run);
|
|
* thread.run();
|
|
*
|
|
* Well, asynchronous function is sometimes more flexible in writing a server,
|
|
* because it can bind different threads to methods on the same object:
|
|
*
|
|
* class MyServer {
|
|
* public:
|
|
* void thread1();
|
|
* void thread2();
|
|
* };
|
|
*
|
|
* MyServer server;
|
|
* AsyncFunc<MyServer> func1(&server, &MyServer::thread1);
|
|
* AsyncFunc<MyServer> func2(&server, &MyServer::thread2);
|
|
* func1.start();
|
|
* func2.start();
|
|
* ...now both threads are running, accessing the same server object.
|
|
*
|
|
* There is nothing wrong embedding the async function object itself in the
|
|
* class like this,
|
|
*
|
|
* class MyServer {
|
|
* public:
|
|
* MyServer()
|
|
* : m_thread1(this, &MyServer::thread1),
|
|
* : m_thread2(this, &MyServer::thread2) {
|
|
* }
|
|
*
|
|
* void thread1();
|
|
* void thread2();
|
|
*
|
|
* void start() {
|
|
* m_thread1.start();
|
|
* m_thread2.start();
|
|
* }
|
|
*
|
|
* private:
|
|
* AsyncFunc<MyServer> m_thread1;
|
|
* AsyncFunc<MyServer> m_thread2;
|
|
* };
|
|
*
|
|
*/
|
|
class AsyncFuncImpl {
|
|
public:
|
|
typedef void PFN_THREAD_FUNC(void *);
|
|
|
|
/**
|
|
* The global static to feed into pthread_create(), and this will delegate
|
|
* the work to AsyncFuncImpl::threadFuncImpl().
|
|
*/
|
|
static void *ThreadFunc(void *obj);
|
|
|
|
/**
|
|
* Called by AsyncFunc<T> so we can call func(obj) back on thread running.
|
|
*/
|
|
AsyncFuncImpl(void *obj, PFN_THREAD_FUNC *func);
|
|
~AsyncFuncImpl();
|
|
|
|
/**
|
|
* Starts this thread.
|
|
*/
|
|
void start();
|
|
|
|
/**
|
|
* Sends a cancellation request to the thread. NB: Do not use this unless
|
|
* the function is known to support cancellation and known to leave shared
|
|
* state in a consistent state (alternatively, the caller should proceed to
|
|
* shut down the process as well). Also, call waitForEnd following this call
|
|
* before proceeding as if the async func has stopped executing.
|
|
*/
|
|
void cancel();
|
|
|
|
/**
|
|
* Waits until this thread finishes running.
|
|
*/
|
|
bool waitForEnd(int seconds = 0);
|
|
|
|
/**
|
|
* Starts and waits until this thread finishes running.
|
|
*/
|
|
void run() {
|
|
start();
|
|
waitForEnd();
|
|
}
|
|
|
|
pthread_attr_t *getThreadAttr() {
|
|
return &m_attr;
|
|
}
|
|
|
|
static void SetThreadInitFunc(PFN_THREAD_FUNC* func, void *arg) {
|
|
s_initFunc = func;
|
|
s_initFuncArg = arg;
|
|
}
|
|
|
|
static void SetThreadFiniFunc(PFN_THREAD_FUNC* func, void *arg) {
|
|
s_finiFunc = func;
|
|
s_finiFuncArg = arg;
|
|
}
|
|
|
|
static PFN_THREAD_FUNC* GetThreadInitFunc() {
|
|
return s_initFunc;
|
|
}
|
|
|
|
static PFN_THREAD_FUNC* GetThreadFiniFunc() {
|
|
return s_finiFunc;
|
|
}
|
|
|
|
void setNoInit() { m_noInit = true; }
|
|
|
|
private:
|
|
Synchronizable m_stopMonitor;
|
|
|
|
void* m_obj;
|
|
PFN_THREAD_FUNC* m_func;
|
|
static PFN_THREAD_FUNC* s_initFunc;
|
|
static PFN_THREAD_FUNC* s_finiFunc;
|
|
static void* s_initFuncArg;
|
|
static void* s_finiFuncArg;
|
|
void* m_threadStack;
|
|
pthread_attr_t m_attr;
|
|
pthread_t m_threadId;
|
|
Exception* m_exception; // exception was thrown and thread was terminated
|
|
bool m_stopped;
|
|
bool m_noInit;
|
|
|
|
static const size_t m_stackSizeMinimum = 8388608; // 8MB
|
|
|
|
/**
|
|
* Called by ThreadFunc() to delegate the work.
|
|
*/
|
|
void threadFuncImpl();
|
|
};
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* We could have written AysncFunc<T> directly with those methods implemented
|
|
* inside AsyncFuncImpl class, but this way we reduce sizes of our code by
|
|
* only templatizing a very minimal piece of code, sharing everything inside
|
|
* AsyncFuncImpl by all AsyncFunc<T> classes.
|
|
*/
|
|
template<class T>
|
|
class AsyncFunc : public AsyncFuncImpl {
|
|
public:
|
|
AsyncFunc(T *obj, void (T::*member_func)())
|
|
: AsyncFuncImpl((void*)this, run_), m_obj(obj), m_memberFunc(member_func) {
|
|
}
|
|
|
|
static void run_(void *obj) {
|
|
AsyncFunc<T> *p = (AsyncFunc<T>*)obj;
|
|
(p->m_obj->*(p->m_memberFunc))();
|
|
}
|
|
|
|
private:
|
|
T *m_obj;
|
|
void (T::*m_memberFunc)();
|
|
};
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
}
|
|
|
|
#endif // incl_HPHP_CONCURRENCY_ASYNC_FUNC_H_
|