1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-03 10:40:21 +02:00
guile/libguile/fdes-finalizers.c
Andy Wingo 730cfd80e2 Devolve pairs.h
* libguile/_scm.h: Remove pairs.h.
* libguile/alist.c:
* libguile/array-handle.c:
* libguile/array-map.c:
* libguile/arrays.c:
* libguile/async.c:
* libguile/bitvectors.c:
* libguile/bytevectors.c:
* libguile/chars.c:
* libguile/continuations.c:
* libguile/control.c:
* libguile/debug.c:
* libguile/deprecation.c:
* libguile/dynwind.c:
* libguile/eq.c:
* libguile/eval.c:
* libguile/evalext.c:
* libguile/expand.c:
* libguile/fdes-finalizers.c:
* libguile/feature.c:
* libguile/filesys.c:
* libguile/fluids.c:
* libguile/foreign.c:
* libguile/fports.c:
* libguile/gc.c:
* libguile/generalized-arrays.c:
* libguile/goops.c:
* libguile/guardians.c:
* libguile/hash.c:
* libguile/hashtab.c:
* libguile/hooks.c:
* libguile/i18n.c:
* libguile/instructions.c:
* libguile/ioext.c:
* libguile/keywords.c:
* libguile/list.c:
* libguile/load.c:
* libguile/loader.c:
* libguile/memoize.c:
* libguile/modules.c:
* libguile/net_db.c:
* libguile/numbers.c:
* libguile/objprop.c:
* libguile/options.c:
* libguile/ports-internal.h:
* libguile/ports.c:
* libguile/posix.c:
* libguile/print.c:
* libguile/print.h:
* libguile/procprop.c:
* libguile/programs.c:
* libguile/random.c:
* libguile/rdelim.c:
* libguile/read.c:
* libguile/regex-posix.c:
* libguile/scmsigs.c:
* libguile/script.c:
* libguile/socket.c:
* libguile/sort.c:
* libguile/srcprop.c:
* libguile/srfi-1.c:
* libguile/srfi-13.c:
* libguile/srfi-14.c:
* libguile/srfi-60.c:
* libguile/stacks.c:
* libguile/stime.c:
* libguile/strings.c:
* libguile/strorder.c:
* libguile/struct.c:
* libguile/symbols.c:
* libguile/threads.c:
* libguile/throw.c:
* libguile/trees.c:
* libguile/values.c:
* libguile/vectors.c:
* libguile/vm.c:
* libguile/weak-list.h:
* libguile/weak-set.c:
* libguile/weak-table.c:
* libguile/weak-vector.c: Add pairs.h.
2018-06-18 22:03:13 +02:00

134 lines
3.6 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Copyright (C) 2016, 2018 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "libguile/_scm.h"
#include "libguile/pairs.h"
#include "libguile/boolean.h"
#include "libguile/extensions.h"
#include "libguile/eval.h"
#include "libguile/hashtab.h"
#include "libguile/list.h"
#include "libguile/numbers.h"
#include "libguile/fdes-finalizers.h"
/* Table of fdes finalizers and associated lock. */
static scm_i_pthread_mutex_t fdes_finalizers_lock =
SCM_I_PTHREAD_MUTEX_INITIALIZER;
static SCM fdes_finalizers;
SCM_DEFINE (scm_add_fdes_finalizer_x, "add-fdes-finalizer!", 2, 0, 0,
(SCM fd, SCM finalizer),
"Add a finalizer that will be called when @var{fd} is closed.")
#define FUNC_NAME s_scm_add_fdes_finalizer_x
{
SCM h;
/* Check type. */
scm_to_uint (fd);
scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
h = scm_hashv_create_handle_x (fdes_finalizers, fd, SCM_EOL);
scm_set_cdr_x (h, scm_cons (finalizer, scm_cdr (h)));
scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (scm_remove_fdes_finalizer_x, "remove-fdes-finalizer!", 2, 0, 0,
(SCM fd, SCM finalizer),
"Remove a finalizer that was previously added to the file\n"
"descriptor @var{fd}.")
#define FUNC_NAME s_scm_remove_fdes_finalizer_x
{
SCM h;
/* Check type. */
scm_to_uint (fd);
scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
h = scm_hashv_get_handle (fdes_finalizers, fd);
if (scm_is_true (h))
scm_set_cdr_x (h, scm_delq1_x (finalizer, scm_cdr (h)));
scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
struct fdes_finalizer_data
{
SCM finalizer;
SCM fd;
};
static SCM
do_run_finalizer (void *data)
{
struct fdes_finalizer_data *fdata = data;
return scm_call_1 (fdata->finalizer, fdata->fd);
}
void
scm_run_fdes_finalizers (int fd)
{
SCM finalizers;
struct fdes_finalizer_data data;
data.fd = scm_from_int (fd);
scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
finalizers = scm_hashv_ref (fdes_finalizers, data.fd, SCM_EOL);
if (!scm_is_null (finalizers))
scm_hashv_remove_x (fdes_finalizers, data.fd);
scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
for (; !scm_is_null (finalizers); finalizers = scm_cdr (finalizers))
{
data.finalizer = scm_car (finalizers);
scm_internal_catch (SCM_BOOL_T, do_run_finalizer, &data,
scm_handle_by_message_noexit, NULL);
}
}
static void
scm_init_fdes_finalizers (void)
{
#include "libguile/fdes-finalizers.x"
}
void
scm_register_fdes_finalizers ()
{
fdes_finalizers = scm_c_make_hash_table (0);
scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
"scm_init_fdes_finalizers",
(scm_t_extension_init_func) scm_init_fdes_finalizers,
NULL);
}