zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <psprint2@fastmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>, zsh-workers@zsh.org
Subject: Re: [PATCH] Closing descriptors in forked (pipe) command
Date: Sun, 19 Feb 2017 02:51:21 -0800	[thread overview]
Message-ID: <1487501481.310651.885737776.56C3E504@webmail.messagingengine.com> (raw)
In-Reply-To: <170218210200.ZM27007@torch.brasslantern.com>

[-- Attachment #1: Type: text/plain, Size: 1009 bytes --]

On Sat, Feb 18, 2017, at 09:02 PM, Bart Schaefer wrote:
> You/we certainly may wish to document that it is dangerous to assume
> that parent and subshell can both manipulate the tied parameter, but
> it would be incorrect to automatically close the file descriptor in
> either of them.

I've updated docs and added "zgdbmclear" builtin as we now know GDBM is
fine with concurrent access if only it can be synchronized. In Zsh this
is possible via zsystem flock. I think PM_UPTODATE is a great feature as
no other program can change the database. With zsystem flock and
zgdbmclear, one can write a concurrent, elastic program within Zsh (a
challenge, though).

Also added a test case, but tests haven't been commited:

 ztie -d db/gdbm -f $dbfile dbase
 dbase[testkey]=value1
 fun() { while read line; do echo $line; done }
 eval "dbase[testkey]=value2" | fun
 echo $dbase[testkey]
 zgdbmclear dbase testkey
 echo $dbase[testkey]
0:Test store in forked Zsh
>value1
>value2

Best regards,
Sebastian Gniazdowski

[-- Attachment #2: db_gdbm_update2.diff --]
[-- Type: text/plain, Size: 2367 bytes --]

diff --git a/Doc/Zsh/mod_db_gdbm.yo b/Doc/Zsh/mod_db_gdbm.yo
index 699e9ab..7063690 100644
--- a/Doc/Zsh/mod_db_gdbm.yo
+++ b/Doc/Zsh/mod_db_gdbm.yo
@@ -49,6 +49,15 @@ item(tt(zgdbmpath) var(parametername))(
 Put path to database file assigned to var(parametername) into tt(REPLY)
 scalar.
 )
+findex(zgdbmclear)
+cindex(database concurrent access)
+item(tt(zgdbmclear) var(parametername) var(keyname))(
+The tied database is enabled for concurrent access only within single Zsh
+instance. User must use tt(zsystem flock) to guard that access if it involves
+any writes. To refetch a key from database when its change in concurrent
+Zsh process is possible, user should use tt(zgdbmclear) passing name of
+database and name of the key to refetch.
+)
 findex(zgdbm_tied)
 cindex(database tied arrays, enumerating)
 item(tt(zgdbm_tied))(
diff --git a/Src/Modules/db_gdbm.c b/Src/Modules/db_gdbm.c
index 596a8ae..730abea 100644
--- a/Src/Modules/db_gdbm.c
+++ b/Src/Modules/db_gdbm.c
@@ -89,6 +89,7 @@ static struct builtin bintab[] = {
     BUILTIN("ztie", 0, bin_ztie, 1, -1, 0, "d:f:r", NULL),
     BUILTIN("zuntie", 0, bin_zuntie, 1, -1, 0, "u", NULL),
     BUILTIN("zgdbmpath", 0, bin_zgdbmpath, 1, -1, 0, "", NULL),
+    BUILTIN("zgdbmclear", 0, bin_zgdbmclear, 2, -1, 0, "", NULL),
 };
 
 #define ROARRPARAMDEF(name, var) \
@@ -260,6 +261,42 @@ bin_zgdbmpath(char *nam, char **args, Options ops, UNUSED(int func))
     return 0;
 }
 
+/**/
+static int
+bin_zgdbmclear(char *nam, char **args, Options ops, UNUSED(int func))
+{
+    Param pm;
+    char *pmname, *key;
+
+    pmname = *args++;
+    key = *args;
+
+    if (!pmname) {
+        zwarnnam(nam, "parameter name (whose path is to be written to $REPLY) is required");
+        return 1;
+    }
+
+    pm = (Param) paramtab->getnode(paramtab, pmname);
+    if(!pm) {
+        zwarnnam(nam, "no such parameter: %s", pmname);
+        return 1;
+    }
+
+    if (pm->gsu.h != &gdbm_hash_gsu) {
+        zwarnnam(nam, "not a tied gdbm parameter: %s", pmname);
+        return 1;
+    }
+
+    HashTable ht = pm->u.hash;
+    HashNode hn = gethashnode2( ht, key );
+    Param val_pm = (Param) hn;
+    if (val_pm) {
+        val_pm->node.flags &= ~(PM_UPTODATE);
+    }
+
+    return 0;
+}
+
 /*
  * The param is actual param in hash – always, because
  * getgdbmnode creates every new key seen. However, it

      parent reply	other threads:[~2017-02-19 10:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-18 12:45 Sebastian Gniazdowski
2017-02-19  5:02 ` Bart Schaefer
2017-02-19  8:04   ` Sebastian Gniazdowski
2017-02-19 10:51   ` Sebastian Gniazdowski [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1487501481.310651.885737776.56C3E504@webmail.messagingengine.com \
    --to=psprint2@fastmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).