mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* marshal.c (w_float): must distinguish -0.0 from 0.0.
* gc.c (gc_mark_all): tweak mark order for little bit better scan. * gc.c (rb_gc_mark): ditto. * gc.c (rb_gc): ditto. * enum.c (sort_by_i): slight performance boost. * gc.c (gc_mark_rest): should call gc_mark_children(), not gc_mark(). * gc.c (rb_gc_mark): may cause infinite looop. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1861 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
09a4937b2f
commit
186c8b592a
9 changed files with 301 additions and 83 deletions
22
ChangeLog
22
ChangeLog
|
@ -1,8 +1,20 @@
|
||||||
|
Tue Nov 27 02:15:25 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* marshal.c (w_float): must distinguish -0.0 from 0.0.
|
||||||
|
|
||||||
Mon Nov 26 20:57:24 2001 Akinori MUSHA <knu@iDaemons.org>
|
Mon Nov 26 20:57:24 2001 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
* ext/Setup*, ext/syslog/*: import the "syslog" module from the
|
* ext/Setup*, ext/syslog/*: import the "syslog" module from the
|
||||||
rough ruby project.
|
rough ruby project.
|
||||||
|
|
||||||
|
Mon Nov 26 16:14:42 2001 K.Kosako <kosako@sofnec.co.jp>
|
||||||
|
|
||||||
|
* gc.c (gc_mark_all): tweak mark order for little bit better scan.
|
||||||
|
|
||||||
|
* gc.c (rb_gc_mark): ditto.
|
||||||
|
|
||||||
|
* gc.c (rb_gc): ditto.
|
||||||
|
|
||||||
Mon Nov 26 16:54:59 2001 Usaku Nakamura <usa@ruby-lang.org>
|
Mon Nov 26 16:54:59 2001 Usaku Nakamura <usa@ruby-lang.org>
|
||||||
|
|
||||||
* win32/win32.c (mypopen): fixed that mypclose() didn't really close
|
* win32/win32.c (mypopen): fixed that mypclose() didn't really close
|
||||||
|
@ -11,11 +23,21 @@ Mon Nov 26 16:54:59 2001 Usaku Nakamura <usa@ruby-lang.org>
|
||||||
* win32/win32.c (CreateChild): set STARTF_USESTDHANDLES flag only
|
* win32/win32.c (CreateChild): set STARTF_USESTDHANDLES flag only
|
||||||
when some handles are passed.
|
when some handles are passed.
|
||||||
|
|
||||||
|
Mon Nov 26 16:31:28 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* enum.c (sort_by_i): slight performance boost.
|
||||||
|
|
||||||
Sun Nov 25 21:02:18 2001 Usaku Nakamura <usa@ruby-lang.org>
|
Sun Nov 25 21:02:18 2001 Usaku Nakamura <usa@ruby-lang.org>
|
||||||
|
|
||||||
* parse.y (str_extend): change types of second and third arguments
|
* parse.y (str_extend): change types of second and third arguments
|
||||||
from char to int.
|
from char to int.
|
||||||
|
|
||||||
|
Thu Nov 22 20:15:28 2001 TAMURA Takashi <sheepman@tcn.zaq.ne.jp>
|
||||||
|
|
||||||
|
* gc.c (gc_mark_rest): should call gc_mark_children(), not gc_mark().
|
||||||
|
|
||||||
|
* gc.c (rb_gc_mark): may cause infinite looop.
|
||||||
|
|
||||||
Thu Nov 22 00:28:13 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Thu Nov 22 00:28:13 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* parse.y (str_extend): should check nesting parentheses in #{}.
|
* parse.y (str_extend): should check nesting parentheses in #{}.
|
||||||
|
|
24
array.c
24
array.c
|
@ -1086,6 +1086,26 @@ rb_ary_sort(ary)
|
||||||
return ary;
|
return ary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
sort_inplace(ary)
|
||||||
|
VALUE ary;
|
||||||
|
{
|
||||||
|
qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE),sort_2);
|
||||||
|
return ary;
|
||||||
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_ary_sort_inplace(ary)
|
||||||
|
VALUE ary;
|
||||||
|
{
|
||||||
|
rb_ary_modify(ary);
|
||||||
|
if (RARRAY(ary)->len <= 1) return ary;
|
||||||
|
|
||||||
|
FL_SET(ary, ARY_TMPLOCK); /* prohibit modification during sort */
|
||||||
|
rb_ensure(sort_inplace, ary, sort_unlock, ary);
|
||||||
|
return ary;
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_ary_collect(ary)
|
rb_ary_collect(ary)
|
||||||
VALUE ary;
|
VALUE ary;
|
||||||
|
@ -1493,7 +1513,9 @@ rb_ary_cmp(ary, ary2)
|
||||||
{
|
{
|
||||||
long i, len;
|
long i, len;
|
||||||
|
|
||||||
ary2 = to_ary(ary2);
|
if (TYPE(ary2) != T_ARRAY) {
|
||||||
|
ary2 = to_ary(ary2);
|
||||||
|
}
|
||||||
len = RARRAY(ary)->len;
|
len = RARRAY(ary)->len;
|
||||||
if (len > RARRAY(ary2)->len) {
|
if (len > RARRAY(ary2)->len) {
|
||||||
len = RARRAY(ary2)->len;
|
len = RARRAY(ary2)->len;
|
||||||
|
|
24
enum.c
24
enum.c
|
@ -208,7 +208,23 @@ sort_by_i(i, memo)
|
||||||
VALUE i;
|
VALUE i;
|
||||||
NODE *memo;
|
NODE *memo;
|
||||||
{
|
{
|
||||||
VALUE e = rb_ary_new3(3, rb_yield(i), INT2NUM(memo->u3.cnt), i);
|
VALUE v, e;
|
||||||
|
|
||||||
|
v = rb_yield(i);
|
||||||
|
if (TYPE(v) == T_ARRAY) {
|
||||||
|
int j, len = RARRAY(v)->len;
|
||||||
|
|
||||||
|
e = rb_ary_new2(len+2);
|
||||||
|
for (j=0; j<len; j++) {
|
||||||
|
RARRAY(e)->ptr[j] = RARRAY(v)->ptr[j];
|
||||||
|
}
|
||||||
|
RARRAY(e)->ptr[j++] = INT2NUM(memo->u3.cnt);
|
||||||
|
RARRAY(e)->ptr[j] = i;
|
||||||
|
RARRAY(e)->len = len + 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
e = rb_ary_new3(3, v, INT2NUM(memo->u3.cnt), i);
|
||||||
|
}
|
||||||
rb_ary_push(memo->u1.value, e);
|
rb_ary_push(memo->u1.value, e);
|
||||||
memo->u3.cnt++;
|
memo->u3.cnt++;
|
||||||
return Qnil;
|
return Qnil;
|
||||||
|
@ -225,16 +241,16 @@ static VALUE
|
||||||
enum_sort_by(obj)
|
enum_sort_by(obj)
|
||||||
VALUE obj;
|
VALUE obj;
|
||||||
{
|
{
|
||||||
VALUE ary = rb_ary_new();
|
VALUE ary = rb_ary_new2(2000);
|
||||||
NODE *memo = rb_node_newnode(NODE_MEMO, ary, 0, 0);
|
NODE *memo = rb_node_newnode(NODE_MEMO, ary, 0, 0);
|
||||||
long i;
|
long i;
|
||||||
|
|
||||||
rb_iterate(rb_each, obj, sort_by_i, (VALUE)memo);
|
rb_iterate(rb_each, obj, sort_by_i, (VALUE)memo);
|
||||||
rb_gc_force_recycle((VALUE)memo);
|
rb_gc_force_recycle((VALUE)memo);
|
||||||
rb_iterate(rb_ary_sort_bang, ary, sort_by_sort_body, 0);
|
rb_ary_sort_inplace(ary);
|
||||||
for (i=0; i<RARRAY(ary)->len; i++) {
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
||||||
VALUE e = RARRAY(ary)->ptr[i];
|
VALUE e = RARRAY(ary)->ptr[i];
|
||||||
RARRAY(ary)->ptr[i] = rb_ary_entry(e, 2);
|
RARRAY(ary)->ptr[i] = RARRAY(e)->ptr[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ary;
|
return ary;
|
||||||
|
|
15
eval.c
15
eval.c
|
@ -7614,6 +7614,9 @@ rb_thread_schedule()
|
||||||
copy_fds(&exceptfds, &th->exceptfds, th->fd);
|
copy_fds(&exceptfds, &th->exceptfds, th->fd);
|
||||||
if (max < th->fd) max = th->fd;
|
if (max < th->fd) max = th->fd;
|
||||||
need_select = 1;
|
need_select = 1;
|
||||||
|
if (th->wait_for & WAIT_TIME) {
|
||||||
|
need_select = 2;
|
||||||
|
}
|
||||||
th->select_value = 0;
|
th->select_value = 0;
|
||||||
}
|
}
|
||||||
if (th->wait_for & WAIT_TIME) {
|
if (th->wait_for & WAIT_TIME) {
|
||||||
|
@ -7673,6 +7676,18 @@ rb_thread_schedule()
|
||||||
}
|
}
|
||||||
END_FOREACH_FROM(curr, th);
|
END_FOREACH_FROM(curr, th);
|
||||||
}
|
}
|
||||||
|
if (n == 0 && need_select == 2) {
|
||||||
|
if (now < 0.0) now = timeofday();
|
||||||
|
FOREACH_THREAD_FROM(curr, th) {
|
||||||
|
if ((th->wait_for & (WAIT_SELECT|WAIT_TIME)) && th->delay < now) {
|
||||||
|
th->status = THREAD_RUNNABLE;
|
||||||
|
th->wait_for = 0;
|
||||||
|
th->select_value = 0;
|
||||||
|
found = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
END_FOREACH_FROM(curr, th);
|
||||||
|
}
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
now = -1.0;
|
now = -1.0;
|
||||||
/* Some descriptors are ready.
|
/* Some descriptors are ready.
|
||||||
|
|
|
@ -3,12 +3,12 @@ require 'mkmf'
|
||||||
if /mswin32|mingw/ !~ RUBY_PLATFORM
|
if /mswin32|mingw/ !~ RUBY_PLATFORM
|
||||||
have_header("sys/stropts.h")
|
have_header("sys/stropts.h")
|
||||||
have_func("setresuid")
|
have_func("setresuid")
|
||||||
$CFLAGS << "-DHAVE_DEV_PTMX" if /cygwin/ === RUBY_PLATFORM
|
|
||||||
have_header("libutil.h")
|
have_header("libutil.h")
|
||||||
have_header("pty.h")
|
have_header("pty.h")
|
||||||
have_library("util", "openpty")
|
have_library("util", "openpty")
|
||||||
if have_func("openpty") or
|
if have_func("openpty") or
|
||||||
have_func("_getpty") or
|
have_func("_getpty") or
|
||||||
|
have_func("ptsname") or
|
||||||
have_func("ioctl")
|
have_func("ioctl")
|
||||||
create_makefile('pty')
|
create_makefile('pty')
|
||||||
end
|
end
|
||||||
|
|
|
@ -349,7 +349,7 @@ getDevice(master,slave)
|
||||||
int i,j;
|
int i,j;
|
||||||
char MasterName[DEVICELEN];
|
char MasterName[DEVICELEN];
|
||||||
|
|
||||||
#ifdef HAVE_DEV_PTMX
|
#ifdef HAVE_PTSNAME
|
||||||
char *pn;
|
char *pn;
|
||||||
void (*s)();
|
void (*s)();
|
||||||
|
|
||||||
|
@ -364,7 +364,7 @@ getDevice(master,slave)
|
||||||
if(unlockpt(i) != -1) {
|
if(unlockpt(i) != -1) {
|
||||||
if((pn = ptsname(i)) != NULL) {
|
if((pn = ptsname(i)) != NULL) {
|
||||||
if((j = open(pn, O_RDWR, 0)) != -1) {
|
if((j = open(pn, O_RDWR, 0)) != -1) {
|
||||||
#if defined I_PUSH
|
#if defined I_PUSH && !defined linux
|
||||||
if(ioctl(j, I_PUSH, "ptem") != -1) {
|
if(ioctl(j, I_PUSH, "ptem") != -1) {
|
||||||
if(ioctl(j, I_PUSH, "ldterm") != -1) {
|
if(ioctl(j, I_PUSH, "ldterm") != -1) {
|
||||||
#endif
|
#endif
|
||||||
|
@ -372,7 +372,7 @@ getDevice(master,slave)
|
||||||
*slave = j;
|
*slave = j;
|
||||||
strcpy(SlaveName, pn);
|
strcpy(SlaveName, pn);
|
||||||
return;
|
return;
|
||||||
#if defined I_PUSH
|
#if defined I_PUSH && !defined linux
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -385,10 +385,10 @@ getDevice(master,slave)
|
||||||
rb_raise(rb_eRuntimeError, "Cannot get Master/Slave device");
|
rb_raise(rb_eRuntimeError, "Cannot get Master/Slave device");
|
||||||
#else
|
#else
|
||||||
for (p = deviceNo; *p != NULL; p++) {
|
for (p = deviceNo; *p != NULL; p++) {
|
||||||
sprintf(MasterName ,MasterDevice,*p);
|
sprintf(MasterName,MasterDevice,*p);
|
||||||
if ((i = open(MasterName,O_RDWR,0)) >= 0) {
|
if ((i = open(MasterName,O_RDWR,0)) >= 0) {
|
||||||
*master = i;
|
*master = i;
|
||||||
sprintf(SlaveName ,SlaveDevice,*p);
|
sprintf(SlaveName,SlaveDevice,*p);
|
||||||
if ((j = open(SlaveName,O_RDWR,0)) >= 0) {
|
if ((j = open(SlaveName,O_RDWR,0)) >= 0) {
|
||||||
*slave = j;
|
*slave = j;
|
||||||
chown(SlaveName, getuid(), getgid());
|
chown(SlaveName, getuid(), getgid());
|
||||||
|
|
53
gc.c
53
gc.c
|
@ -427,26 +427,23 @@ init_mark_stack()
|
||||||
|
|
||||||
#define MARK_STACK_EMPTY (mark_stack_ptr == mark_stack)
|
#define MARK_STACK_EMPTY (mark_stack_ptr == mark_stack)
|
||||||
|
|
||||||
static int mark_all;
|
|
||||||
|
|
||||||
static void rb_gc_mark_children(VALUE ptr);
|
static void rb_gc_mark_children(VALUE ptr);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gc_mark_all()
|
gc_mark_all()
|
||||||
{
|
{
|
||||||
RVALUE *p, *pend;
|
RVALUE *p, *pend;
|
||||||
int i;
|
int i;
|
||||||
mark_all = 0;
|
|
||||||
while(!mark_all){
|
init_mark_stack();
|
||||||
mark_all = 1;
|
for (i = 0; i < heaps_used; i++) {
|
||||||
for (i = 0; i < heaps_used; i++) {
|
p = heaps[i]; pend = p + heaps_limits[i];
|
||||||
p = heaps[i]; pend = p + heaps_limits[i];
|
while (p < pend) {
|
||||||
while (p < pend) {
|
if ((p->as.basic.flags & FL_MARK) &&
|
||||||
if ((p->as.basic.flags & FL_MARK) &&
|
(p->as.basic.flags != FL_MARK)) {
|
||||||
(p->as.basic.flags != FL_MARK)) {
|
rb_gc_mark_children((VALUE)p);
|
||||||
rb_gc_mark_children((VALUE)p);
|
|
||||||
}
|
|
||||||
p++;
|
|
||||||
}
|
}
|
||||||
|
p++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -464,7 +461,7 @@ gc_mark_rest()
|
||||||
|
|
||||||
while(p != tmp_arry){
|
while(p != tmp_arry){
|
||||||
p--;
|
p--;
|
||||||
rb_gc_mark(*p);
|
rb_gc_mark_children(*p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -565,32 +562,28 @@ void
|
||||||
rb_gc_mark(ptr)
|
rb_gc_mark(ptr)
|
||||||
VALUE ptr;
|
VALUE ptr;
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
register RVALUE *obj = RANY(ptr);
|
register RVALUE *obj = RANY(ptr);
|
||||||
|
|
||||||
if (rb_special_const_p(ptr)) return; /* special const not marked */
|
if (rb_special_const_p(ptr)) return; /* special const not marked */
|
||||||
if (obj->as.basic.flags == 0) return; /* free cell */
|
if (obj->as.basic.flags == 0) return; /* free cell */
|
||||||
if (obj->as.basic.flags & FL_MARK) return; /* already marked */
|
if (obj->as.basic.flags & FL_MARK) return; /* already marked */
|
||||||
|
|
||||||
if (!mark_stack_overflow){
|
obj->as.basic.flags |= FL_MARK;
|
||||||
int ret;
|
|
||||||
CHECK_STACK(ret);
|
CHECK_STACK(ret);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
if (!mark_stack_overflow) {
|
||||||
if (mark_stack_ptr - mark_stack < MARK_STACK_MAX) {
|
if (mark_stack_ptr - mark_stack < MARK_STACK_MAX) {
|
||||||
*mark_stack_ptr = ptr;
|
*mark_stack_ptr = ptr;
|
||||||
mark_stack_ptr++;
|
mark_stack_ptr++;
|
||||||
return;
|
}
|
||||||
}else{
|
else {
|
||||||
mark_stack_overflow = 1;
|
mark_stack_overflow = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
obj->as.basic.flags |= FL_MARK;
|
|
||||||
|
|
||||||
if (mark_stack_overflow){
|
|
||||||
mark_all &= 0;
|
|
||||||
return;
|
|
||||||
}else{
|
|
||||||
rb_gc_mark_children(ptr);
|
rb_gc_mark_children(ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1175,8 +1168,8 @@ rb_gc()
|
||||||
while (!MARK_STACK_EMPTY){
|
while (!MARK_STACK_EMPTY){
|
||||||
if (mark_stack_overflow){
|
if (mark_stack_overflow){
|
||||||
gc_mark_all();
|
gc_mark_all();
|
||||||
break;
|
}
|
||||||
}else{
|
else {
|
||||||
gc_mark_rest();
|
gc_mark_rest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
230
lib/singleton.rb
230
lib/singleton.rb
|
@ -1,49 +1,195 @@
|
||||||
# Singleton module that ensures only one object to be allocated.
|
# The Singleton module implements the Singleton pattern - i.e.
|
||||||
|
#
|
||||||
|
# class Klass
|
||||||
|
# include Singleton
|
||||||
|
# # ...
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# * ensures that only one instance of Klass called ``the instance''
|
||||||
|
# can be created.
|
||||||
|
#
|
||||||
|
# a,b = Klass.instance, Klass.instance
|
||||||
|
# a == b # => true
|
||||||
|
# a.new # NoMethodError - new is private ...
|
||||||
|
#
|
||||||
|
# * ``The instance'' is created at instanciation time - i.e. the first call
|
||||||
|
# of Klass.instance().
|
||||||
|
#
|
||||||
|
# class OtherKlass
|
||||||
|
# include Singleton
|
||||||
|
# # ...
|
||||||
|
# end
|
||||||
|
# p "#{ObjectSpace.each_object(OtherKlass) {}}" # => 0
|
||||||
|
#
|
||||||
|
# * This behavior is preserved under inheritance.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# This achieved by marking
|
||||||
|
# * Klass.new and Klass.allocate - as private and
|
||||||
|
# * Klass.inherited(sub_klass) - modifying to ensure
|
||||||
|
# that the Singleton pattern is properly inherited.
|
||||||
|
#
|
||||||
|
# In addition Klass is provided with the class methods
|
||||||
|
# * Klass.instance() - returning ``the instance''
|
||||||
|
# * Klass._load(str) - returning ``the instance''
|
||||||
|
# * Klass._wait() - a hook method putting a second (or n-th)
|
||||||
|
# thread calling Klass.instance on a waiting loop if the first call
|
||||||
|
# to Klass.instance is still in progress.
|
||||||
|
#
|
||||||
|
# The sole instance method of Singleton is
|
||||||
|
# * _dump(depth) - returning the empty string
|
||||||
|
# The default Marshalling strategy is to strip all state information - i.e.
|
||||||
|
# instance variables from ``the instance''. Providing costume
|
||||||
|
# _dump(depth) and _load(str) method allows the (partial) resurrection
|
||||||
|
# of a previous state of ``the instance'' - see third example.
|
||||||
#
|
#
|
||||||
# Usage:
|
|
||||||
# class SomeSingletonClass
|
|
||||||
# include Singleton
|
|
||||||
# #....
|
|
||||||
# end
|
|
||||||
# a = SomeSingletonClass.instance
|
|
||||||
# b = SomeSingletonClass.instance # a and b are same object
|
|
||||||
# p [a,b]
|
|
||||||
# a = SomeSingletonClass.new # error (`new' is private)
|
|
||||||
|
|
||||||
module Singleton
|
module Singleton
|
||||||
def Singleton.included(klass)
|
def Singleton.included (klass)
|
||||||
klass.private_class_method(:new)
|
# should this be checked?
|
||||||
klass.instance_eval %{
|
# raise TypeError.new "..." if klass.type == Module
|
||||||
@__instance__ = nil
|
class << klass
|
||||||
def instance
|
def inherited(sub_klass)
|
||||||
if defined? @__allocating__
|
# @__instance__ takes on one of the following values
|
||||||
until @__instance__
|
# * nil - before (and after a failed) creation
|
||||||
sleep 0.5
|
# * false - during creation
|
||||||
end
|
# * sub_class instance - after a successful creation
|
||||||
elsif ! @__instance__
|
@__instance__ = nil
|
||||||
Thread.critical = true
|
def sub_klass.instance
|
||||||
@__allocating__ = true
|
unless @__instance__.nil?
|
||||||
Thread.critical = false
|
# is the extra flexiblity having the hook method
|
||||||
begin
|
# _wait() around ever useful?
|
||||||
@__instance__ = new
|
_wait() while false.equal?(@__instance__)
|
||||||
ensure
|
# check for instance creation
|
||||||
remove_instance_variable(:@__allocating__)
|
return @__instance__ if @__instance__
|
||||||
end
|
end
|
||||||
end
|
Thread.critical = true
|
||||||
return @__instance__
|
unless @__instance__
|
||||||
end
|
@__instance__ = false
|
||||||
}
|
Thread.critical = false
|
||||||
end
|
begin
|
||||||
|
@__instance__ = new
|
||||||
|
ensure
|
||||||
|
if @__instance__
|
||||||
|
define_method(:instance) {@__instance__ }
|
||||||
|
else
|
||||||
|
# failed instance creation
|
||||||
|
@__instance__ = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
Thread.critical = false
|
||||||
|
end
|
||||||
|
return @__instance__
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def _load(str)
|
||||||
|
instance
|
||||||
|
end
|
||||||
|
def _wait
|
||||||
|
sleep(0.05)
|
||||||
|
end
|
||||||
|
private :new, :allocate
|
||||||
|
# hook methods are also marked private
|
||||||
|
private :_load,:_wait
|
||||||
|
end
|
||||||
|
klass.inherited klass
|
||||||
|
end
|
||||||
|
private
|
||||||
|
def _dump(depth)
|
||||||
|
return ""
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if __FILE__ == $0
|
if __FILE__ == $0
|
||||||
class SomeSingletonClass
|
|
||||||
include Singleton
|
|
||||||
#....
|
|
||||||
end
|
|
||||||
|
|
||||||
a = SomeSingletonClass.instance
|
#basic example
|
||||||
b = SomeSingletonClass.instance # a and b are same object
|
class SomeSingletonClass
|
||||||
p [a,b]
|
include Singleton
|
||||||
a = SomeSingletonClass.new # error (`new' is private)
|
end
|
||||||
|
a = SomeSingletonClass.instance
|
||||||
|
b = SomeSingletonClass.instance # a and b are same object
|
||||||
|
p a == b # => true
|
||||||
|
begin
|
||||||
|
SomeSingletonClass.new
|
||||||
|
rescue NoMethodError => mes
|
||||||
|
puts mes
|
||||||
|
end
|
||||||
|
|
||||||
|
# threaded example with exception
|
||||||
|
Thread.abort_on_exception = true
|
||||||
|
class Ups < SomeSingletonClass
|
||||||
|
@__threads__= []
|
||||||
|
@__flip__ = nil
|
||||||
|
@@__index__ = nil
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
sleep(rand(0.1)/10.0)
|
||||||
|
Thread.current[:index] = @@__index__
|
||||||
|
end
|
||||||
|
class << self
|
||||||
|
def allocate
|
||||||
|
unless @__flip__
|
||||||
|
@__flip__ = true
|
||||||
|
raise "boom - allocation in thread ##{@@__index__} aborted"
|
||||||
|
end
|
||||||
|
super()
|
||||||
|
end
|
||||||
|
def instanciate_all
|
||||||
|
1.upto(5) do |@@__index__|
|
||||||
|
sleep(rand(0.1)/10.0)
|
||||||
|
@__threads__.push Thread.new {
|
||||||
|
begin
|
||||||
|
instance
|
||||||
|
rescue RuntimeError => mes
|
||||||
|
puts mes
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def join
|
||||||
|
@__threads__.each do |t|
|
||||||
|
t.join
|
||||||
|
puts "initialize called by thread ##{t[:index]}" if
|
||||||
|
t[:index]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
puts "There is(are) #{ObjectSpace.each_object(Ups) {}} Ups instance(s)"
|
||||||
|
# => The is(are) 0 Ups instance(s)
|
||||||
|
Ups.instanciate_all
|
||||||
|
Ups.join # => initialize called by thread # i - where i = 2 ... 5
|
||||||
|
p Marshal.load(Marshal.dump(Ups.instance)) == Ups.instance # => true
|
||||||
|
puts "There is(are) #{ObjectSpace.each_object(Ups) {}} Ups instance(s)"
|
||||||
|
# => The is(are) 1 Ups instance(s)
|
||||||
|
|
||||||
|
# Customized marshalling
|
||||||
|
class A
|
||||||
|
include Singleton
|
||||||
|
attr_accessor :persist, :die
|
||||||
|
def _dump(depth)
|
||||||
|
# this strips the @die information from the instance
|
||||||
|
Marshal.dump(@persist,depth)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def A._load(str)
|
||||||
|
instance.persist = Marshal.load(str)
|
||||||
|
instance
|
||||||
|
end
|
||||||
|
|
||||||
|
a = A.instance
|
||||||
|
a.persist = ["persist"]
|
||||||
|
a.die = "die"
|
||||||
|
|
||||||
|
stored_state = Marshal.dump(a)
|
||||||
|
# change state
|
||||||
|
a.persist = nil
|
||||||
|
a.die = nil
|
||||||
|
b = Marshal.load(stored_state)
|
||||||
|
p a == b # => true
|
||||||
|
p a.persist # => ["persist"]
|
||||||
|
p a.die # => nil
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -194,6 +194,10 @@ w_float(d, arg)
|
||||||
else if (isnan(d)) {
|
else if (isnan(d)) {
|
||||||
strcpy(buf, "nan");
|
strcpy(buf, "nan");
|
||||||
}
|
}
|
||||||
|
else if (d == 0.0) {
|
||||||
|
if (1.0/d < 0) strcpy(buf, "-0");
|
||||||
|
else strcpy(buf, "0");
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
/* xxx: should not use system's sprintf(3) */
|
/* xxx: should not use system's sprintf(3) */
|
||||||
sprintf(buf, "%.16g", d);
|
sprintf(buf, "%.16g", d);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue