Re: Extended Prefetching using Asynchronous IO - proposal and patch

From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: Claudio Freire <klaussfreire(at)gmail(dot)com>, John Lumby <johnlumby(at)hotmail(dot)com>
Cc: Greg Stark <stark(at)mit(dot)edu>, pgsql hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Extended Prefetching using Asynchronous IO - proposal and patch
Date: 2014-06-25 21:08:48
Message-ID: 53AB3A60.20708@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On 06/25/2014 09:20 PM, Claudio Freire wrote:
> On Tue, Jun 24, 2014 at 12:08 PM, John Lumby <johnlumby(at)hotmail(dot)com> wrote:
>>> The question is, if you receive the notification of the I/O completion
>>> using a signal or a thread, is it safe to release the lwlock from the
>>> signal handler or a separate thread?
>>
>> In the forthcoming new version of the patch that uses sigevent,
>> the originator locks a LWlock associated with that BAaiocb eXclusive,
>> and , when signalled, in the signal handler it places that LWlock
>> on a process-local queue of LWlocks awaiting release.
>> (No, It cannot be safely released inside the signal handler or in a
>> separate thread). Whenever the mainline passes a CHECK_INTERRUPTS macro
>> and at a few additional points in bufmgr, the backend walks this process-local
>> queue and releases those LWlocks. This is also done if the originator
>> itself issues a ReadBuffer, which is the most frequent case in which it
>> is released.
>
> I suggest using a semaphore instead.
>
> Semaphores are supposed to be incremented/decremented from multiple
> threads or processes already. So, in theory, the callback itself
> should be able to do it.

LWLocks are implemented with semaphores, so if you can increment a
semaphore in the signal handler / callback thread, then in theory you
should be able to release a LWLock. You'll need some additional
synchronization within the same process, to make sure you don't release
a lock in signal handler while you're just doing the same thing in the
main thread. I'm not sure I want to buy into the notion that
LWLockRelease must be generally safe to call from a signal handler, but
maybe it's possible to have a variant of it that is.

On Linux at least we use System V semaphores, which are (unsurpisingly)
not listed as safe for using in a signal handler. See list
Async-signal-safe functions in signal(7) man page. The function used to
increment a POSIX semaphore, sem_post(), is in the list, however.

- Heikki

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andres Freund 2014-06-25 21:17:53 Re: Extended Prefetching using Asynchronous IO - proposal and patch
Previous Message Jeff Janes 2014-06-25 19:46:22 Re: python modul pre-import to avoid importing each time

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2014-06-25 21:17:53 Re: Extended Prefetching using Asynchronous IO - proposal and patch
Previous Message Dean Rasheed 2014-06-25 20:48:57 Re: RLS Design