summaryrefslogtreecommitdiff
path: root/ext/dl/lib/dl/callback.rb
diff options
context:
space:
mode:
authorttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-02-04 14:01:22 +0000
committerttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-02-04 14:01:22 +0000
commit41cd3d868de66ff407dd6f3c9a4126ac1692575f (patch)
treeb62f1d471e4d5ad4502ec847ac1b4e1fc66670b0 /ext/dl/lib/dl/callback.rb
parentbda37095ca990568069aaf07dc6b3fa8ceebc327 (diff)
added missing files.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7884 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/dl/lib/dl/callback.rb')
-rw-r--r--ext/dl/lib/dl/callback.rb69
1 files changed, 69 insertions, 0 deletions
diff --git a/ext/dl/lib/dl/callback.rb b/ext/dl/lib/dl/callback.rb
new file mode 100644
index 00000000000..d0b2c7a819f
--- /dev/null
+++ b/ext/dl/lib/dl/callback.rb
@@ -0,0 +1,69 @@
+require 'dl'
+require 'thread'
+
+module DL
+ SEM = Mutex.new
+
+ def set_callback_internal(proc_entry, addr_entry, argc, ty, &cbp)
+ if( argc < 0 )
+ raise(ArgumentError, "arity should not be less than 0.")
+ end
+ addr = nil
+ SEM.synchronize{
+ ary = proc_entry[ty]
+ (0...MAX_CALLBACK).each{|n|
+ idx = (n * DLSTACK_SIZE) + argc
+ if( ary[idx].nil? )
+ ary[idx] = cbp
+ addr = addr_entry[ty][idx]
+ break
+ end
+ }
+ }
+ addr
+ end
+
+ def set_cdecl_callback(ty, argc, &cbp)
+ set_callback_internal(CdeclCallbackProcs, CdeclCallbackAddrs, argc, ty, &cbp)
+ end
+
+ def set_stdcall_callback(ty, argc, &cbp)
+ set_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, argc, ty, &cbp)
+ end
+
+ def remove_callback_internal(proc_entry, addr_entry, addr, ctype = nil)
+ index = nil
+ if( ctype )
+ addr_entry[ctype].each_with_index{|xaddr, idx|
+ if( xaddr == addr )
+ index = idx
+ end
+ }
+ else
+ addr_entry.each{|ty,entry|
+ entry.each_with_index{|xaddr, idx|
+ if( xaddr == addr )
+ index = idx
+ end
+ }
+ }
+ end
+ if( proc_entry[ctype][index] )
+ proc_entry[ctype][index] = nil
+ return true
+ else
+ return false
+ end
+ end
+
+ def remove_cdecl_callback(addr, ctype = nil)
+ remove_callback_internal(CdeclCallbackProcs, CdeclCallbackAddrs, addr, ctype)
+ end
+
+ def remove_stdcall_callback(addr, ctype = nil)
+ remove_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, addr, ctype)
+ end
+
+ alias set_callback set_cdecl_callback
+ alias remove_callback remove_cdecl_callback
+end