Mercurial > hg > octave-nkf
view src/pt-fcn-handle.cc @ 13985:43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
* configure.ac: New --enable-atomic-refcount argument.
(octave_allocator): Fix USE_OCTAVE_ALLOCATOR variable assignment.
(OCTAVE_CONFIG_INCLUDED): New macro in config.h.
* oct-refcount.h (OCTREFCOUNT_ATOMIC_INCREMENT,
OCTREFCOUNT_ATOMIC_INCREMENT_POST, OCTREFCOUNT_ATOMIC_DECREMENT,
OCTREFCOUNT_ATOMIC_DECREMENT_POST): New macro, defined for MSVC and GCC
when USE_ATOMIC_REFCOUNT is defined.
(octave_refcount:operator++, octave_refcount::operator--): Use them.
(octave_refcount::operator count_type): Cast returned value to volatile.
(octave_refcount::direct): Remove unsafe member.
* Array.h (Array::make_unique): Delete rep if refcount reaches 0.
* Sparse.h (Sparse::make_unique): Delete rep if refcount reaches 0.
* Array.h (Array:~Array, Array::operator=): Delete rep only when refcount is
excatly 0.
* Array.cc (Array::clear): Likewise.
* Sparse.cc (Sparse::~Sparse, Sparse::operator=): Likewise.
* SparseCmplxQR.h (SparseCmplxQR::~SparseCmplxQR, SparseCmplxQR::operator=):
Likewise.
* SparseQR.h (SparseQR::~SparseQR, SparseQR::operator=): Likewise.
* sparse-base-chol.h (sparse_base_chol::~sparse_base_chol,
sparse_base_chol::operator): Likewise.
* dim-vector.h (oct-refcount.h): New included header.
(dim_vector::make_unique, dim_vector::resize): Use OCTREFCOUNT_ATOMIC_DECREMENT
macro and delete rep when refcount reaches 0.
(dim_vector::dim_vector): Use OCTREFCOUNT_ATOMIC_INCREMENT.
(dim_vector::operator=): Use OCTREFCOUNT_ATOMIC_INCREMENT and
OCTREFCOUNT_ATOMIC_DECREMENT.
(dim_vector::~dim_vector): Use OCTREFCOUNT_ATOMIC_DECREMENT.
* oct-mutex.h (oct-refcount.h): New included header.
(octave_base_mutex::count): Use octave_refcount class.
* gl-render.cc (oct-refcount.h): New included header.
* graphics.h.in (oct-refcount.h): Likewise.
(base_property::count, base_graphics_toolkit::count,
base_graphics_object::count, base_graphics_event::count): Use octave_refcount.
(property::~property, property::operator=): Delete rep only when refcountn is
excatly 0.
* oct-map.h (octave_fields::make_unique): Delete rep when refcount reaches 0.
* oct-stream.h (oct-refcount.h): New included header.
(octave_base_stream::count): Use octave_refcount class.
* ov.h (octave_value::make_unique): Delete rep when refcount reaches 0.
* symtab.h (oct-refcount.h): New included header.
(symbol_record_rep::count, fcn_info_rep::count): Use octave_refcount class.
* DLD-FUNCTIONS/urlwrite.cc (oct-refcount.h): New included header.
(curl_handle_rep::count): Use octave_refcount class.
author | Michael Goffioul <michael.goffioul@gmail.com> |
---|---|
date | Sat, 03 Dec 2011 15:19:42 +0000 |
parents | 2a8dcb5b3a00 |
children | 4e8f23ccadce |
line wrap: on
line source
/* Copyright (C) 2003-2011 John W. Eaton This file is part of Octave. Octave is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Octave; see the file COPYING. If not, see <http://www.gnu.org/licenses/>. */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <iostream> #include "error.h" #include "oct-obj.h" #include "ov-fcn-handle.h" #include "pt-fcn-handle.h" #include "pager.h" #include "pt-const.h" #include "pt-walk.h" #include "variables.h" void tree_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax, bool pr_orig_text) { print_raw (os, pr_as_read_syntax, pr_orig_text); } void tree_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax, bool pr_orig_text) { os << ((pr_as_read_syntax || pr_orig_text) ? "@" : "") << nm; } octave_value tree_fcn_handle::rvalue1 (int) { return make_fcn_handle (nm); } octave_value_list tree_fcn_handle::rvalue (int nargout) { octave_value_list retval; if (nargout > 1) error ("invalid number of output arguments for function handle expression"); else retval = rvalue1 (nargout); return retval; } tree_expression * tree_fcn_handle::dup (symbol_table::scope_id, symbol_table::context_id) const { tree_fcn_handle *new_fh = new tree_fcn_handle (nm, line (), column ()); new_fh->copy_base (*this); return new_fh; } void tree_fcn_handle::accept (tree_walker& tw) { tw.visit_fcn_handle (*this); } octave_value tree_anon_fcn_handle::rvalue1 (int) { // FIXME -- should CMD_LIST be limited to a single expression? // I think that is what Matlab does. tree_parameter_list *param_list = parameter_list (); tree_parameter_list *ret_list = return_list (); tree_statement_list *cmd_list = body (); symbol_table::scope_id this_scope = scope (); symbol_table::scope_id new_scope = symbol_table::dup_scope (this_scope); if (new_scope > 0) symbol_table::inherit (new_scope, symbol_table::current_scope (), symbol_table::current_context ()); octave_user_function *uf = new octave_user_function (new_scope, param_list ? param_list->dup (new_scope, 0) : 0, ret_list ? ret_list->dup (new_scope, 0) : 0, cmd_list ? cmd_list->dup (new_scope, 0) : 0); octave_function *curr_fcn = octave_call_stack::current (); if (curr_fcn) { // FIXME -- maybe it would be better to just stash curr_fcn // instead of individual bits of info about it? uf->stash_parent_fcn_name (curr_fcn->name ()); uf->stash_dir_name (curr_fcn->dir_name ()); symbol_table::scope_id parent_scope = curr_fcn->parent_fcn_scope (); if (parent_scope < 0) parent_scope = curr_fcn->scope (); uf->stash_parent_fcn_scope (parent_scope); if (curr_fcn->is_class_method () || curr_fcn->is_class_constructor ()) uf->stash_dispatch_class (curr_fcn->dispatch_class ()); } uf->mark_as_anonymous_function (); uf->stash_fcn_file_name (file_name); uf->stash_fcn_location (line (), column ()); octave_value ov_fcn (uf); octave_value fh (octave_fcn_binder::maybe_binder (ov_fcn)); return fh; } /* %!function r = f2 (f, x) %! r = f (x); %!function f = f1 (k) %! f = @(x) f2 (@(y) y-k, x); %!test %! assert ((f1 (3)) (10) == 7) %! %!shared g %! g = @(t) feval (@(x) t*x, 2); %!assert (g(0.5) == 1) %! %!shared f, g, h %! h = @(x) sin (x); %! g = @(f, x) h (x); %! f = @() g (@(x) h, pi); %!assert (f () == sin (pi)) */ octave_value_list tree_anon_fcn_handle::rvalue (int nargout) { octave_value_list retval; if (nargout > 1) error ("invalid number of output arguments for anonymous function handle expression"); else retval = rvalue1 (nargout); return retval; } tree_expression * tree_anon_fcn_handle::dup (symbol_table::scope_id, symbol_table::context_id) const { tree_parameter_list *param_list = parameter_list (); tree_parameter_list *ret_list = return_list (); tree_statement_list *cmd_list = body (); symbol_table::scope_id this_scope = scope (); symbol_table::scope_id new_scope = symbol_table::dup_scope (this_scope); if (new_scope > 0) symbol_table::inherit (new_scope, symbol_table::current_scope (), symbol_table::current_context ()); tree_anon_fcn_handle *new_afh = new tree_anon_fcn_handle (param_list ? param_list->dup (new_scope, 0) : 0, ret_list ? ret_list->dup (new_scope, 0) : 0, cmd_list ? cmd_list->dup (new_scope, 0) : 0, new_scope, line (), column ()); new_afh->copy_base (*this); return new_afh; } void tree_anon_fcn_handle::accept (tree_walker& tw) { tw.visit_anon_fcn_handle (*this); }