-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
76 lines (66 loc) · 2.35 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <phpcpp.h>
#include <pthread.h>
#include <string>
/**
* tell the compiler that the get_module is a pure C function
*/
extern "C" {
struct callback_struct {
Php::Value callback;
int isWork;
};
void *callback(void *arguments){
struct callback_struct *args = (struct callback_struct *)arguments;
if (!args->callback.isCallable()) {
throw Php::Exception("Not a callable type.");
// printf("Error");
// return NULL;
}
args->callback();
pthread_exit(NULL);
return NULL;
}
Php::Value nukethread(Php::Parameters ¶ms)
{
// check whether the parameter is callable
if (!params[0].isCallable()) throw Php::Exception("Not a callable type.");
// perform the callback
struct callback_struct app;
app.callback = params[0];
app.isWork = 1;
pthread_t thread;
int status;
status = pthread_create(&thread, NULL, &callback,(void *)&app);
pthread_join(thread, NULL);
return NULL;
}
Php::Value nukethreadOnlyThread(Php::Parameters ¶ms)
{
// check whether the parameter is callable
if (!params[0].isCallable()) throw Php::Exception("Not a callable type.");
return params[0]();
}
/**
* Function that is called by PHP right after the PHP process
* has started, and that returns an address of an internal PHP
* strucure with all the details and features of your extension
*
* @return void* a pointer to an address that is understood by PHP
*/
PHPCPP_EXPORT void *get_module()
{
// static(!) Php::Extension object that should stay in memory
// for the entire duration of the process (that's why it's static)
static Php::Extension extension("vthread","Version 1.0. Author: SteveLee - Le Minh Hoang");
// @todo add your own functions, classes, namespaces to the extension
extension.add<nukethread>("nukethread", {
Php::ByVal("addFunc", Php::Type::Callable)
});
extension.add<nukethreadOnlyThread>("nukethreadOnlyThread", {
Php::ByVal("addFunc", Php::Type::Callable)
});
// return the extension
return extension;
}
}