[HACKERS] A misconception about the meaning of 'volatile' in GetNewTransactionId?

Поиск
Список
Период
Сортировка
От Thomas Munro
Тема [HACKERS] A misconception about the meaning of 'volatile' in GetNewTransactionId?
Дата
Msg-id CAEepm=1nff0x=7i3YQO16jLA2qw-F9O39YmUew4oq-xcBQBs0g@mail.gmail.com
обсуждение исходный текст
Ответы Re: [HACKERS] A misconception about the meaning of 'volatile' in GetNewTransactionId?
Список pgsql-hackers
Hi hackers,

I was reading xact.c and noticed this block:
               /*                * Use volatile pointer to prevent code rearrangement;
other backends                * could be examining my subxids info concurrently,
and we don't want                * them to see an invalid intermediate state, such as
incrementing                * nxids before filling the array entry.  Note we are
assuming that                * TransactionId and int fetch/store are atomic.                */               volatile
PGPROC*myproc = MyProc;               volatile PGXACT *mypgxact = MyPgXact;
 
               if (!isSubXact)                       mypgxact->xid = xid;               else               {
          int                     nxids = mypgxact->nxids;
 
                       if (nxids < PGPROC_MAX_CACHED_SUBXIDS)                       {
myproc->subxids.xids[nxids]= xid;                               mypgxact->nxids = nxids + 1;
 

Isn't this insufficient on non-TSO systems like POWER and Arm?  It
uses volatile qualifiers as a compiler barrier, which is probably
enough for x86 and Sparc in TSO mode, but doesn't include a memory
barrier to prevent hardware reordering.

I think the thing to do here would be to forget about volatile, stick
pg_write_barrier() between those two writes, and stick
pg_read_barrier() between the reads in any code that might read nxids
and then scan xids concurrently, such as TransactionIdIsInProgress().

This is almost exactly the example from the section "Avoiding Memory
Order Bugs" in src/backend/storage/lmgr/README.barrier.

Thoughts?

-- 
Thomas Munro
http://www.enterprisedb.com



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: [HACKERS] convert EXSITS to inner join gotcha and bug
Следующее
От: Tom Lane
Дата:
Сообщение: Re: [HACKERS] A misconception about the meaning of 'volatile' in GetNewTransactionId?