Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Writing php extensions in golang

7,241 views

Published on

PHP BLT #1

Published in: Technology
  • Be the first to comment

Writing php extensions in golang

  1. 1. 2015/11/24 PHP BLT do_aki Writing php extensions in golang
  2. 2. @do_aki @do_aki http://do-aki.net/
  3. 3. https://speakerdeck.com/naruse/writing-extension-libraries-in-go
  4. 4. In case of PHP……?
  5. 5. golang buildmode • -buildmode=c-shared • build the main package (main function is dummy) • export symbol by “//export” https://golang.org/s/execmodes
  6. 6. cgo(call C function) package main /* #include <stdlib.h> */ import “C” func Random() int { return int(C.random()) }
  7. 7. cgo(export) package main import “C” //export go_func func go_func() int { return 1 }
  8. 8. php extension loading process 1. load shared library (.so / .dll) 2. fetch “get_module” or “_get_module” symbol 3. call fetched function and get pointer to zend_module_entry structure 4. register module using the zend_module_entry ref:php_load_extension in ext/standard/dl.c
  9. 9. export get_module package main /* #include “php.h” */ import “C” var gophp_module_entry C.zend_module_entry //export get_module func get_module() *C.zend_module_entry { gophp_module_entry.name = C.Cstring(“gophp”) gophp_module_entry.functions = … // fill member in gophp_module_entry return gophp_module_entry; }
  10. 10. export get_module package main /* #include “php.h” */ import “C” var gophp_module_entry C.zend_module_entry //export get_module func get_module() *C.zend_module_entry { gophp_module_entry.name = C.Cstring(“gophp”) gophp_module_entry.functions = … // fill member in gophp_module_entry return gophp_module_entry; }
  11. 11. constraints of c in go function -like macro Access union member
  12. 12. separate export function and function definition • err: multiple definition of ` xxx' • write definition in non_export.go (not include //export) • write declarations in export.go (include //export) • go build non_export.go export.go
  13. 13. github.com/do-aki/gophp_sample
  14. 14. but,but,but • segmentation fault in shutdown process sometimes…… Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7fffee4c2700 (LWP 8943)] 0x00007ffff04d6821 in ?? () (gdb) backtrace #0 0x00007ffff04d6821 in ?? () #1 0x00007ffff04a6213 in ?? () #2 0x000000c82002ce90 in ?? () #3 0x0000000000000000 in ?? () in go runtime? Umm…
  15. 15. fin.

×